【问题标题】:PHP Contact Form - Sending email to multiple recipientsPHP 联系表 - 向多个收件人发送电子邮件
【发布时间】:2016-01-27 17:48:27
【问题描述】:

如何使此表单能够发送给多个收件人?目前它只允许我将其发送到 1 封电子邮件,当我尝试输入多个(例如“user1@example.com,user2@example.com”)时,它会返回无效的消息。我需要做什么来解决这个问题?

编辑:用户必须输入要发送到的电子邮件地址,但它仅适用于 1,这就是为什么我要寻求帮助,了解如何编辑代码以处理多个电子邮件/收件人和用逗号和空格隔开。

这里是代码

<?php

if(isset($_POST['email'])) {




$email_to = array($_GET["celebrant_emails"]);

$email_subject = "Email from website Contact Form";





function died($error) {

    // your error code can go here

    echo "We are very sorry, but there were error(s) found with the email you submitted. ";

    echo "These errors appear below.<br /><br />";

    echo $error."<br /><br />";

    echo "Please go <a href='http://celebrantsaustralia.asn.au/celebrants-trial.htm'>back</a> and fix these errors.<br /><br />";

    die();

}



// validation expected data exists

if(!isset($_POST['first_name']) ||

    !isset($_POST['last_name']) ||

    !isset($_POST['email']) ||

    !isset($_POST['comments']) ||

    !isset($_POST['celebrant_emails'])) {

    died('We are sorry, but there appears to be a problem with the email you submitted.');       

}



$first_name = $_POST['first_name']; // required

$last_name = $_POST['last_name']; // required

$email_from = $_POST['email']; // required

$comments = $_POST['comments']; // required

$celebrant_emails = $_POST['celebrant_emails']; // required



$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$email_from)) {

$error_message .= 'The Email Address you entered does not appear to be valid.<br />';

}

$string_exp = "/^[A-Za-z .'-]+$/";

if(!preg_match($string_exp,$first_name)) {

$error_message .= 'The First Name you entered does not appear to be valid.<br />';

}

if(!preg_match($string_exp,$last_name)) {

$error_message .= 'The Last Name you entered does not appear to be valid.<br />';

}

if(strlen($comments) < 2) {

$error_message .= 'The Message you entered does not appear to be valid.<br />';

}

if(!preg_match($email_exp,$celebrant_emails)) {

$error_message .= 'The Celebrant Email(s) you entered does not appear to be valid.<br />';

}

if(strlen($error_message) > 0) {

died($error_message);

}

$email_message = "Email details below.\n\n";



function clean_string($string) {

  $bad = array("content-type","bcc:","to:","cc:","href");

  return str_replace($bad,"",$string);

}



$email_message .= "First Name: ".clean_string($first_name)."\n";

$email_message .= "Last Name: ".clean_string($last_name)."\n";

$email_message .= "Email: ".clean_string($email_from)."\n";

$email_message .= "Message: ".clean_string($comments)."\n";





// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  

?>



<!-- include your own success html here -->

<html>
    <head>
        <title>Email sent!</title>
    </head>

    <body>
        <b>Thank you for contacting us. We will be in touch with you shortly.</b>
        <p>(Page will auto-direct in a moment, if it doesn't click <a href="http://website.com">here</a>)</p>
    </body>

</html>

<?php

}

?>

【问题讨论】:

  • 哦,我有它,所以用户将电子邮件放入文本框。
  • 您最好重新考虑这一点,因为如果不在内部使用,您的代码将被公开并被视为垃圾邮件/网络钓鱼。有人可以输入他们想要的任何电子邮件地址,然后您可能最终会被列入服务器黑名单。
  • 会有一个验证码。这是为这个网站celebrantsaustralia.asn.au/celebrants-trial.htm
  • 无论如何,Ryan,如果你继续这个项目,你可能会遇到一些严重的垃圾邮件法律问题。就像我说的;任何人都可以在您的表单中输入他们想要的任何电子邮件并将其发送到他们已知的电子邮件。真正的垃圾邮件发送者可以输入他们的电子邮件并通过网络钓鱼“你”。我正在努力把你从潜在的灾难中拯救出来。
  • 您还需要在问题中发布您的 HTML 表单。里面可能有些不对劲。

标签: php html forms email


【解决方案1】:

有很多方法可以做到这一点。

$email_to = "jhewitt@amleo.com,some@other.com,yet@another.net";

$email_to = 'Mary <mary@example.com>, Kelly <kelly@example.com>';

如果您需要将电子邮件添加为抄送或密送,请在您用作标题的变量中添加以下部分:

$headers .= "CC: sombodyelse@noplace.com".PHP_EOL;
$headers .= "BCC: hidden@special.com".PHP_EOL;

来源:PHP form send email to multiple recipients

也可以在这里查看:Example #1 Sending mail.

【讨论】:

  • 这不是我想要的......因为我的意思是用户在自己的文本框中输入电子邮件地址......每次你都不会发送到特定的电子邮件地址提交表格。
  • 这只是一个提示,您可以从中获取想法并继续前进。正如您所说的“用户在文本框中输入电子邮件地址”,您可以在文本框附近提及一个帮助,请输入多个或类似的逗号分隔的电子邮件 ID,甚至可以选择后处理文本框数据一旦你收到 $_POST['emails']
  • 我想说的是,用逗号分隔电子邮件是行不通的。
  • 这是我需要帮助的哈哈
  • @ryan-denton 你想要一个动态的电子邮件数组吗?
【解决方案2】:
 $email=$_POST['email'];   // write multiple emails with comma's    
 $emails=explode(',', $email); // explode email with comma's
     foreach($emails as $one_email)
     {
         $touser=$one_email;
            $subjectAdmin= "subject";
            $headersAdmin = "From: noreply@talentswype.com\r\n";
            $headersAdmin .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            $messageuser ='msg here'; 
     $emailSenduser = mail($touser,$subjectAdmin,$messageuser,$headersAdmin);
}

【讨论】:

    【解决方案3】:

    为每封电子邮件编写一个邮件函数。

    mail('example1@example.com', 'My Subject', $message);
    mail('example2@example.com', 'My Subject', $message);
    mail('example3@example.com', 'My Subject', $message);
    mail('example4@example.com', 'My Subject', $message);
    mail('example5@example.com', 'My Subject', $message);
    

    foreach($mail as $mails ){
         mail($mail, 'My Subject', $message);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-24
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      相关资源
      最近更新 更多