mail()函数不向所有人发送电子邮件

时间:2014-09-04 08:35:31

标签: php email

我的数据库中有大约400个电子邮件地址;所有这些人都应该收到一封电子邮件,其中包含一些他们帐户独有的数据。我创建了一个php脚本,它遍历这些电子邮件地址。但是,并非所有人都收到了电子邮件。

有什么方法可以做得更好吗?你可以在下面找到这个脚本。

$sql = "SELECT * FROM Email WHERE 1;";
$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result)) {

    $surname = $row['surname'];
    $email = $row['email'];
    $ticket = $row['ticket_code'];

    $now = date('m/d/Y h:i:s a', time());

    $to      = $email;
    $subject = 'Your ticket code';
    $message = 'Hi,
    You were selected to be a part of our charity event. You can find your ticket code below. See you then!
    '.$ticket;
    $headers = 'From: our@email.com' . "\r\n" .
        'Reply-To: our@email.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);

    $lqs = "UPDATE Email SET `email_send`='$now' WHERE `ticket_code`='$ticket'";
    $resulter = mysql_query($lqs);

}

1 个答案:

答案 0 :(得分:0)

我建议您不要使用PHP mail()函数。我发现它产生了某些电子邮件提供商的问题,例如Hotmail和GMAIL。

我会使用PHPMailer库 - 请参阅这里的Github链接https://github.com/PHPMailer/PHPMailer

以下是其工作原理的示例:

<?php

    require 'PHPMailer/_lib/class.phpmailer.php';

    $sql = mysql_query("SELECT * FROM Email");

    while($row = mysql_fetch_assoc($sql)) {

    $mail = new PHPMailer();

    $mail->isMail();  // Set mailer to use Mail

    $mail->From = ''; // email sender
    $mail->FromName = ''; // name of sender
    $mail->AddAddress('', '');    // pass in the recipient email, and recipient name
    $mail->AddReplyTo('', ''); // reply email address, reply name

    $mail->WordWrap = 50;     // Set word wrap to 50 characters
    $mail->isHTML(true);      // Set email format to HTML

    // subject
    $mail->Subject = ""; // email subject

    //message
    $mail->Body    = "      

    <!doctype html>

    <html lang="en">
    <head>
    <meta charset="utf-8">

    </head>
    <body>


    </body>
    </html>

    ";

    // Send email to recipient

    if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;

    $ticket = $row['ticket_code'];
    $now = date('m/d/Y h:i:s a', time());

    // update email table
    $update = mysql_query (" 
    UPDATE Email SET `email_send`='$now'
    WHERE `ticket_code`='$ticket' ");

    }


 ?>

到目前为止,使用这个库和类对我来说效果很好,似乎对所有电子邮件提供商都没有任何例外。

相关问题