此电子邮件发送代码效果很好,但当我尝试删除以下字段'to_names'时。代码停止工作。我是PHP编码的新手请有人帮我告诉我如何删除它仍然让我的代码运行良好。感谢。
<?php
$to_emails = $_POST['to_emails'];
$to_names = $_POST['to_names']; // The code stops working when i remove this line. Please help.
$from_email = $_POST['from_email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$explode_emails = array_map('trim', explode(",", $to_emails));
$explode_names = array_map('trim', explode(",",$to_names));
$recipients = array_combine($explode_emails, $explode_names);
$i=0;
foreach($recipients as $email => $name) {
$headers = 'From: ' . $from_email;
$headers .= "\r\nReply-To: " . $from_email;
$headers .= "\r\nX-Mailer: PHP/" . phpversion();
$finalMessage = $name . ",\r\n" . $message;
$success = mail($email, $subject, $finalMessage, $headers);
echo "<font color='#FF0000'>" . $i . " Emailed: " . $name . " : " . $email . "</font><br />";
$i++;
}
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=login.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
答案 0 :(得分:0)
我模拟了你的代码,试试这个:
<?php
$to_emails = $_POST['to_emails'];
//$to_names = $_POST['to_names']; // The code stops working when i remove this line. Please help.
$from_email = $_POST['from_email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$explode_emails = array_map('trim', explode(",", $to_emails));
//$explode_names = array_map('trim', explode(",",$to_names));
//$recipients = array_combine($explode_emails, $explode_names);
$recipients = $explode_emails;
$i=0;
foreach($recipients as $email) {
$headers = 'From: ' . $from_email;
$headers .= "\r\nReply-To: " . $from_email;
$headers .= "\r\nX-Mailer: PHP/" . phpversion();
$finalMessage = $email . ",\r\n" . $message;
$success = mail($email, $subject, $finalMessage, $headers);
echo "<font color='#FF0000'>" . $i . " Emailed: " . $email . "</font><br />";
$i++;
}
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=login.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
答案 1 :(得分:0)
代码:(未经测试)
if(!isset($_POST['to_emails'],$_POST['from_email'],$_POST['message'],$_POST['subject'])) {
$failure=true;
echo "Oops, insufficent mailing values posted";
}else{
foreach(explode(",",$_POST['to_emails']) as $index=>$email){
$email=trim($email);
$headers = 'From: ' . $_POST['from_email'];
$headers .= "\r\nReply-To: " . $_POST['from_email'];
$headers .= "\r\nX-Mailer: PHP/" . phpversion();
if(mail($email, $_POST['subject'], "\r\n{$_POST['message']}", $headers)){
echo "<font color='#FF0000'>{$index} Emailed: {$email}</font><br />";
}else{
$failure=true;
echo "Oops, something went wrong on email index $index to $email<br>";
}
}
}
echo "<meta http-equiv=\"refresh\" content=\"0;URL=",(!$failure?"login.php":"error.htm"),"\">";
$success
字符串仅适用于上一次迭代。array_map()
然后foreach()
)。将trim()
打包到foreach循环中。explode()
中的索引,而不是使用迭代递增实现计数器。答案 2 :(得分:-1)
$ to_names是否表示您发送给的人的用户名?如果是,则可以为变量$ to_names定义默认值。 $ to_names = $ _POST [&#39; to_names&#39;]?$ _ POST [&#39; to_names&#39;]:&#39;我的朋友&#39;;