user7433174
user7433174

Reputation: 13

How to add wait between each email sent in this script?

I have the following code as part of our mail sending script using 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 protected]';              // 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('[email protected]', 'mydomain.com');
        $mail->addAddress($row[$column_name] , 'Subscriber'); 
        // $mail->addAddress($row[$column_name] , 'Subscriber');     // Add a recipient
        // $mail->addAddress('[email protected]');               // Name is optional
        // $mail->addReplyTo('[email protected]', 'Information');
        // $mail->addCC('[email protected]');
        // $mail->addBCC('[email protected]');

        // $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>';
        }

    }    
}

?>

How can I add sleep() so that it makes a 20 second pause between each email? I have tried adding it but seems that I am not choosing the right place to add it. THANKS FOR THE HELP

Upvotes: 1

Views: 825

Answers (1)

Guillaume Sainthillier
Guillaume Sainthillier

Reputation: 1685

You can add your sleep at this place :

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);
}

The sleep function will be triggered after the mail was successfully send.

Upvotes: 2

Related Questions