PHP表单将电子邮件发送给多个收件人
我有一些PHP代码,用来发送表单到一个特定的电子邮件地址。 不过,我想在PHP中包含更多的电子邮件地址,以便发送它。 我怎样才能做到这一点?
<?php if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "email@email.com"; $email_subject = "MVP Nomination"; function died($error) { // your error code can go here echo "We are very sorry, but there were error(s) found with the form you submitted. "; echo "These errors appear below.<br /><br />"; echo $error."<br /><br />"; echo "Please go back and fix these errors.<br /><br />"; die(); } // validation expected data exists if(!isset($_POST['username']) || !isset($_POST['body'])|| !isset($_POST['email'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $username = $_POST['username']; // required $body = $_POST['body']; // required $email_from = $_POST['email']; // 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,$username)) { $error_message .= 'The Username you entered does not appear to be valid.<br />'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "Name: ".clean_string($username)."\n"; $email_message .= "Comments: ".clean_string($body)."\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); } header("Location: ThankYou.html"); //Redirect to Thank You HTML page after email is sent ?>
谢谢。
这将工作:
$email_to = "jhewitt@amleo.com,some@other.com,yet@another.net";
如果您需要将电子邮件添加为CC或BCC,请将以下部分添加到您使用的variables中作为标题:
$headers .= "CC: sombodyelse@noplace.com".PHP_EOL; $headers .= "BCC: hidden@special.com".PHP_EOL;
问候
使用逗号分隔值如下。
$email_to = 'Mary <mary@example.com>, Kelly <kelly@example.com>'; @mail($email_to, $email_subject, $email_message, $headers);
或运行一个电子邮件地址的foreach
//list of emails in array format and each one will see their own to email address $arrEmail = array('Mary <mary@example.com>', 'Kelly <kelly@example.com>'); foreach($arrEmail as $key => $email_to) @mail($email_to, $email_subject, $email_message, $headers);
您可以将$email_to
添加到$email_to
variables,用逗号( ,
)分隔它们。 或者你可以添加新的字段到标题,即CC:
或BCC:
并把你的收件人在那里。 BCC
是最BCC
推荐的
如果我理解正确的尝试这一个
$headers = "Bcc: someone@domain.com";
要么
$headers = "Cc: someone@domain.com";