PHP mail()仅发送给单个收件人

时间:2014-03-23 13:55:22

标签: php email

我正在为客户端更新几个非PHP相关页面,这些页面将表单输入发送到PHP。但是,如果没有对报告的代码客户端进行任何更改,则不会收到电子邮件。相反,他说它只发送电子邮件给其中一封电子邮件,而不是所有电子邮件.3。某些针对某些代码的错误代码是什么?

$email_to = "info@example.com, user@live.ca, user@live.ca"; 

    $email_subject = "VIP Access"."  [".date("Y-m-d @ h:m:s A")."]";

    $first_name   = $_POST["objFirstName"];
    $last_name = $_POST["objLastName"];
    $phone     = $_POST["objPhone"];
    $email_from = $_POST["objEmail"];

    $full_name = $first_name." ".$last_name;

    $message = "";

      function clean_string($string) {
        $bad = array("content-type", "bcc:", "to:", "cc:", "href");
        return str_replace($bad, "", $string);
      }


    $message .= "<html><body>\n";
    $message .= "<table rules='all' border='1' style='border-color:#000;' cellpadding='10' width='100%'>\n";
    $message .= "<tr style='background: #eee;'><td width='20%'><strong>Full Name:</strong> </td><td width='70%'>".clean_string($full_name)."</td></tr>\n";
    $message .= "<tr style='background: #eee;'><td width='20%'><strong>Email:</strong> </td><td width='70%'>".clean_string($email_from)."</td></tr>\n";
    $message .= "<tr style='background: #eee;'><td width='20%'><strong>Phone:</strong> </td><td width='70%'><a href='tel:".clean_string($phone)."'>".clean_string($phone)."</a></td></tr>\n";
    $message .= "</table>\n";
    $message .= "<img src='http://example.com/images/logo-trans.png' width='120' height='130' alt='Estate Brothers' style='text-align:center;'/>\n";
    $message .= "</body></html>\n";

    // create email headers
    $headers = "From: ".$email_from." via example.com\r\n"."Reply-To: ".$email_from."\r\n"."X-Mailer: PHP/".phpversion()."via example.com";
    $headers .= 'To: Example <info@example.com>, User <user@live.ca>, User two <usertwo@live.ca>' . "\r\n";
    $headers .= 'MIME-Version: 1.0'."\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";

    $mail_feed =  mail($email_to, $email_subject, $message, $headers);

2 个答案:

答案 0 :(得分:1)

此处有Acquite swiftmailer - &gt; http://swiftmailer.org/

require_once 'swiftmailer/lib/swift_required.php';

function new_mail($subject, $content)
{
    // Create the message
    $message = Swift_Message::newInstance();

    // Give the message a subject
    $message->setSubject($subject);

    // Set the From address with an associative array
    $message->setFrom(array('sender@example.com' => 'Sender')));

    // Set the To addresses with an associative array
    $message->setTo(array('info@example.com' => 'Example'));

    // Give it a body
    $message->setBody($content);

    $transport = Swift_MailTransport::newInstance();
    $mailer = Swift_Mailer::newInstance($transport);
    $result = $mailer->send($message);

}

new_mail('Subject', $messagecontentgoeshere);

如果您愿意,也可以用变量替换TO和FROM部分。因此,您可以在网站的任何地方重复使用整个功能。

答案 1 :(得分:0)

试试这些:

  1. 删除email_to变量中的所有空格(我知道RFC允许空格是允许的,但有些人遇到问题并删除空格为它们修复了它):

    $email_to = 'info@example.com,user@live.ca,user@live.ca';
    
  2. 确保您逃脱所有$_POST变量以避免注射。

  3. 您可以使用标题来执行TO而不是使用邮件(注意连接修复,就像其他答案所说的那样):

    $email_to = "info@example.com";
    ....
    $headers = "From: ".$email_from." via example.com\r\n"."Reply-To: ".$email_from."\r\n"."X-Mailer: PHP/".phpversion()."via example.com\r\n";
    $headers  .= 'To: user@live.ca, user@live.ca'."\r\n";
    $headers  .= 'MIME-Version: 1.0'."\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
    
  4. 另一种解决方案是多次发送电子邮件(确保您有其他标题,包括x-mailer丢失的\ r \ n):

    $email_to = array('info@example.com', 'user@live.ca', 'user@live.ca');
    ....
    $mail_feed = true;
    foreach ($email_to as $email) {
        $mail_feed =  $mail_feed && mail($email, $email_subject, $message, $headers);
    }