如何删除此字段'to_names'仍然可以使代码正常工作?

时间:2017-11-13 00:31:19

标签: php html email post

此电子邮件发送代码效果很好,但当我尝试删除以下字段'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\">";
    }
    ?>

3 个答案:

答案 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;;