Search code examples
phpphpmailerbulk-mail

Php Mailer - continue with next mail if sent() error occurs


I am new to use phpmailer for sending bulk mails. The execution stops and shows error if send() fails.

Is there any way to skip the error and continue with next email id. I am using email id's from database.


Solution

  • You Can Catch Exception Mechanism, to continue with next Mail with appropriate logic

    Posted by Phil PHPMAiler uses Exceptions. Try to adopt the following

    require_once '../class.phpmailer.php';
    
    $mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
    
    try {
      $mail->AddReplyTo('[email protected]', 'First Last');
      $mail->AddAddress('[email protected]', 'John Doe');
      $mail->SetFrom('[email protected]', 'First Last');
      $mail->AddReplyTo('[email protected]', 'First Last');
      $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
      $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
      $mail->MsgHTML(file_get_contents('contents.html'));
      $mail->AddAttachment('images/phpmailer.gif');      // attachment
      $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
      $mail->Send();
      echo "Message Sent OK\n";
    } catch (phpmailerException $e) {
      echo $e->errorMessage(); //Pretty error messages from PHPMailer
    } catch (Exception $e) {
      echo $e->getMessage(); //Boring error messages from anything else!
    }