1

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@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>';
        }

    }    
}

?>

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

  • 1
    If you're writing new code, **_please_ don't use the `mysql_*` functions**. They are old and broken, were deprecated in PHP 5.5 (which is so old it no longer even receives security updates), and completely removed in PHP 7. Use [`PDO`](https://secure.php.net/manual/en/book.pdo.php) or [`mysqli_*`](https://secure.php.net/manual/en/book.mysqli.php) with _prepared statements_ and _parameter binding_ instead. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Mar 06 '17 at 19:34
  • 1
    "I have tried adding it but seems that I am not choosing the right place to add it." Where have you tried putting it? – ChrisGPT was on strike Mar 06 '17 at 19:34
  • 1
    Be sure not to exceed the script max running time or you script will die before all mail were sent. Personnaly i would store those email in a DB and loop a script to send them. Or you could also use a web page with a javascript refresh to loop the script throught all emails. – Louis Loudog Trottier Mar 06 '17 at 19:37
  • what is the max script running time? I mean where is this checked? – user7433174 Mar 06 '17 at 20:19

1 Answers1

2

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.

Guillaume Sainthillier
  • 1,655
  • 1
  • 9
  • 13