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

时间:2022-10-23 20:37:48

I have a database filled with ~400 email addresses; all these people were supposed to receive an email with some data unique to their account. I created a php script, which iterated through these email addresses. However, not all people received an email.

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

Is there any way I can do this better? You can find the script below.

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

$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 个解决方案

#1


0  

I recommend that you do not use the PHP mail() function. I found that it produced problems with certain email providers, such as Hotmail and GMAIL.

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

I would use the PHPMailer library - see Github link here https://github.com/PHPMailer/PHPMailer

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

Here's an example of how it works:

这是一个如何工作的例子:

<?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' ");

    }


 ?>

Using this library and classes has worked well for me so far and seems to work without any exception with all email providers.

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

#1


0  

I recommend that you do not use the PHP mail() function. I found that it produced problems with certain email providers, such as Hotmail and GMAIL.

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

I would use the PHPMailer library - see Github link here https://github.com/PHPMailer/PHPMailer

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

Here's an example of how it works:

这是一个如何工作的例子:

<?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' ");

    }


 ?>

Using this library and classes has worked well for me so far and seems to work without any exception with all email providers.

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