0

I have a web app using PHP, MySQL, and PHPMailer to send emails. The issue I am having is PHPMailer will send some of the emails, but not always. I'm thinking maybe it is because different list of emails have data issues? Below is my code that loops through the email addresses and send some, but not all the emails. I have tried to catch errors, but I can't see any in the logs.

How can I debug what is going on, and why some don't send?

    else if($current_version == 'PROD'){                                        
    date_default_timezone_set('America/New_York');
    require 'PHPMailer-master/PHPMailerAutoload.php';


    $pageURL = 'xxxxxx';

    $subject = "xxxxx ";
    $body = "xxxxxx \r\n";
    $body .= "xxxxx \r\n \r\n";

    $body_html = str_replace("\r\n","<br/>",$body);

    try {

        $mail = new PHPMailer();
        $mail->isSMTP();
        //Enable SMTP debugging
        // 0 = off (for production use)
        // 1 = client messages
        // 2 = client and server messages
        $mail->SMTPDebug = 0;
        $mail->Debugoutput = 'html';

        $mail->Host = 'smtp.gmail.com';
        //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
        $mail->Port = 587;
        //Set the encryption system to use - ssl (deprecated) or tls
        $mail->SMTPSecure = 'tls';
        $mail->SMTPAuth = true;
        $mail->Username = "xxx@xxxx.com";
        $mail->Password = "xxxx";
        $mail->setFrom('xxxxx@xxxx.com', 'xxxx');
        $mail->addReplyTo('xxxx@xxxx.com', 'xxxx');

        $mail->Subject = $subject;
        $mail->Body = $body_html;
        $mail->AltBody = $body;

        $sql_email_list = "SELECT email FROM tbl_email 
                            WHERE distro_name='xxxxs'
                                AND is_active='Y' ";
        $result_email_list = mysql_query($sql_email_list);
        $num_email_list = mysql_num_rows($result_email_list);
        $lost_email_list = mysql_result($result_email_list,0,'email');
        $lost_email_array = explode(";",$lost_email_list);
        foreach ($lost_email_array as $key => $val){
            $mail->AddAddress($val);
            if($carrier_email_flag_global == 'ON'){
                $mail->send();
                $mail->ClearAllRecipients();
                $mail->ClearAttachments();
                echo '<hr>Email Sent to: '.$val;
            }
        }

        $sql_email_one = "SELECT table2.email 
                            FROM table1
                                INNER JOIN table2 ON table1.cntct = table2.cntct_id
                            WHERE id='$id' ";
        $result_email_one = mysql_query($sql_email_one);
        $num_email_one = mysql_num_rows($result_email_one);
        for($iiz=0;$iiz<$num_email_one;$iiz++){
            $one_email = mysql_result($result_email_one,$iiz,'email');
            $mail->AddAddress($one_email);  
            if($carrier_email_flag_global == 'ON'){
                $mail->send();
                $mail->ClearAllRecipients();
                $mail->ClearAttachments();
                echo '<hr>Email Sent to: '.$val;
            }                                               
        }



    } catch (phpmailerException $e) {
        echo $e->errorMessage(); //Pretty error messages from PHPMailer
        echo '<hr>ERROR 001';
    } catch (Exception $e) {
        echo $e->getMessage(); //Boring error messages from anything else!
        echo '<hr>ERROR 002';
    }
}
sweaty
  • 369
  • 2
  • 4
  • 18
  • you have too many different things in one big `try{}catch{}` block, either use multiple try-catch blokcs for mail and sql queries or use just one for `$mail->Send`. Also note that phpmailer (and php's mail function in general) can indeed return success yet the email may never be sent. Because it is handled by the underlying system. `$mail->send` returning success only means that the mail request is succesfuly queued in the OS mailing queue thats all. It does NOT mean the email was indeed succesfuly sent! – Nikos M. Dec 18 '15 at 17:19
  • 1
    Base your code on [the mailing list example provided with PHPMailer](https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps). If you're going to use try/catch, you need to tell PHPMailer to throw exceptions by passing `true` to the constructor, otherwise you'll get nothing, especially since you're not checking return values. – Synchro Dec 18 '15 at 17:46
  • Make sure `setFrom('xxxxx@xxxx.com',...` is a valid email address on your server. I would also supect `if($carrier_email_flag_global == 'ON'){` - try it without that as a test. – Steve Dec 19 '15 at 06:01
  • `$mail->Host = 'smtp.gmail.com';` should be the mail service on your server - typically `mail.yourserver.com`or many recipients will block it before it is even born. Some notes on mailing: http://stackoverflow.com/questions/33948622/php-mail-function-server-and-localhost-not-working/33948920#33948920 – Steve Dec 19 '15 at 06:16
  • http://stackoverflow.com/questions/18101769/php-mailer-smtp-gmail-authentication all may not be as gmaily as you think it is...also http://stackoverflow.com/questions/13574166/phpmailer-send-gmail-smtp-timeout?rq=1 – Steve Dec 19 '15 at 06:28
  • Thanks All. I will scope down the Try Catch. Steve, I am checking through the links you provided. I did replace relevant user name and passwords wth xxxxxx just to keep it clean. – sweaty Dec 21 '15 at 20:20

0 Answers0