如何在此脚本中发送的每封电子邮件之间添加等待?

时间:2017-03-06 19:33:14

标签: php phpmailer sendmail

我使用phpmailer将以下代码作为邮件发送脚本的一部分。

<?php

$email_content = ob_get_contents(); 
ob_get_clean();

 //$sql = mysql_query("SELECT email FROM oc_customer");
 //sendMail($sql , 'email' , $email_content);

 $sql = mysql_query("SELECT email FROM oc_customer");
 sendMail($sql , 'email' , $email_content);

//$sql = mysql_query("SELECT * FROM test WHERE Subscribe=1");
//sendMail($sql , 'email' , $email_content);

function sendMail ($sql_response , $column_name , $email_content)
{
    while($row = mysql_fetch_array($sql_response))
    {
        $mail = new PHPMailer;

        // $mail->SMTPDebug = 3;                                 // Enable verbose debug output
        $mail->isSMTP();                                        // Set mailer to use SMTP
        $mail->Mailer = "smtp";
        $mail->Host = 'smtp.gmail.com';                         // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                                 // Enable SMTP authentication
        $mail->Username = 'email@gmail.com';              // SMTP username
        $mail->Password = 'password';                          // SMTP password
        $mail->SMTPSecure = 'TLS';   
        // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 465;                                      // TCP port to connect to


        $mail->setFrom('info@mydomain.com', 'mydomain.com');
        $mail->addAddress($row[$column_name] , 'Subscriber'); 
        // $mail->addAddress($row[$column_name] , 'Subscriber');     // Add a recipient
        // $mail->addAddress('ellen@example.com');               // Name is optional
        // $mail->addReplyTo('info@example.com', 'Information');
        // $mail->addCC('cc@example.com');
        // $mail->addBCC('bcc@example.com');

        // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
        // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->CharSet = "UTF-8";

        $mail->Subject = 'PROMO sólo Hoy';

        $body = $email_content;
        $body .= '<!DOCTYPE html>
        <html>
        <head>
            <title></title>
        </head>
        <body>
            <center><p style="color:gray;font-size:10px;"> Para no recibir mas promos , haga click <a href="mydomain.com/unsubscribe.php">aquí</a></p></center>
        </body>
        </html>'; 

        $mail->Body = $body ;
        // $mail->AltBody = 'Aprovecha la promo del día.';




        $mail->WordWrap = 50;

        if(!$mail->send()) 
        {
            echo 'Message could not be sent.' . 'Mailer Error: ' . $mail->ErrorInfo;
        } 
        else 
        {
            echo 'Message has been sent to ' .  $row[$column_name] . '<br>';
        }

    }    
}

?>

如何添加sleep()以便在每封电子邮件之间暂停20秒?我试过添加它,但似乎我没有选择合适的地方添加它。感谢帮助

1 个答案:

答案 0 :(得分:2)

你可以在这个地方加上你的睡眠:

if(!$mail->send()) 
{
    echo 'Message could not be sent.' . 'Mailer Error: ' . $mail->ErrorInfo;
} 
else 
{
    echo 'Message has been sent to ' .  $row[$column_name] . '<br>';
    sleep(20);
}

邮件成功发送后将触发sleep功能。