PHP邮件发送功能的每个循环内的AJAX时间限制

时间:2018-05-03 17:45:37

标签: php jquery email pear amazon-ses

我已经构建了一个使用PEAR Mail的相当基本的邮件程序,并通过Amazon SES向相对较小(<100)的人员列表发送电子邮件。

我的JavaScript看起来像这样:

$('#mailer_final_form').on('submit',function(e){
    e.preventDefault();
    $('#forms_wrap').slideUp();
    $('#sender').slideDown();
    var url = "/mailer?action=send";
    var totalEmailrec = $('.email-recipients').length;
    var totalEmailrecp = totalEmailrec+1;
    $('#total_togo').html(totalEmailrec);

    var totalSent = 1;

    $('.email-recipients').each(function(){
    var thisRecipient = $(this).val();
        $.ajax({
         type: "POST",
         url: url,
         data: $('#mailer_final_form').serialize()+'&recipient='+thisRecipient,
         success: function(data)
         {
            $('#total_sent').html(totalSent++);
            $('#return_values').append(data);
                if (totalSent==totalEmailrecp) {
                    $('#loader_image').fadeOut();
                    $('#return_values').append('<br><b>Finished!</b>');
                }
         }
     });

    });

});

不幸的是,有时邮件程序崩溃并且它没有告诉我原因,这是我的PHP:

if ($_GET['action']=="send") {

    set_include_path('/opt/cpanel/ea-php56/root/usr/share/pear');

    $mailer_from = $_POST['sender'];
    $mailer_subject = $_POST['email_subject'];
    $mailer_body = $_POST['email_body'];        
    $salutation_type = $_POST['salutation_type'];       
    $recipient = $_POST['recipient'];
    $recipient = explode('||',$recipient);
    $recipient_name = trim($recipient[0]);
    $recipient_email = trim($recipient[1]);

    if (filter_var($recipient_email, FILTER_VALIDATE_EMAIL)) {

    define('USERNAME','my_ses_id');  
    define('PASSWORD','my_ses_pw');  
    define('HOST', 'email-smtp.us-west-2.amazonaws.com');  
    define('PORT', '587');     

    require_once 'Mail.php';

    define('SENDER', "$mailer_from");

    if (strpos($recipient_name, '@') !== false) {
    define('RECIPIENT', "$recipient_email");
    } else {
        define('RECIPIENT', "$recipient_name <$recipient_email>");
    }

    define('SUBJECT', "$mailer_subject");

    switch ($salutation_type) {
        case 1: // dear ***
            if (strpos($recipient_name, '@') !== false) {
                $salutation = "Dear recipient,<br><br>";
            } else {
                $salutation = "Dear $recipient_name,<br><br>";
            }
        break;
        case 2: // no salutation
        $salutation = "";
        break;
    }

    define('BODY',"
    <html>
    <head>
    <title>$mailer_subject</title>
    </head>
    <body style='background:#eee;padding:40px;width:100%;'>
    $mailer_template_header
    $salutation
    $mailer_body
    $mailer_template_footer
    </body>
    </html>");

    $headers = array ('MIME-Version'=>'1.0','Content-type'=>'text/html;charset=UTF-8','From' => SENDER,'To' => RECIPIENT,'Subject' => SUBJECT);
    $smtpParams = array ('debug' => false,'host' => HOST,'port' => PORT,'auth' => true,'username' => USERNAME, 'password' => PASSWORD);

    $mail = Mail::factory('smtp', $smtpParams);
    $mail->send(RECIPIENT, $headers, BODY); 

    if (PEAR::isError($mail)) { "<span class='emails-sent'>$recipient_name ($recipient_email) -".$mail->getMessage()."</span><br>"; } else {
    echo "<span class='emails-sent'>$recipient_name ($recipient_email) - OK</span><br>";
    }

    } else { echo "<span class='emails-sent'>$recipient_name ($recipient_email) - INVALID EMAIL ADDRESS / NOT SENT</span><br>"; }
}

我想做的是,以某种方式阻止邮件程序崩溃,并设置某种超时以跳过任何不想工作的电子邮件尝试(或至少得到正确的错误显示为什么那个失败了)。

我已经不知疲倦地尝试使用jQuery的setInterval和各种计时器循环,但这似乎不想在每个主循环中工作(为每个电子邮件地址执行邮件AJAX调用)。

有人可以建议一种方式(最好使用jQuery),跳过一个电子邮件地址,在该特定地址上出现“由于超时而xyz失败”消息吗?或者也许在我的PHP中向我展示如何将相同的错误返回给邮件程序的界面超时?

0 个答案:

没有答案