简体   繁体   中英

Amazon AWS: Sending email via SES beyond painfully slow

We have a PHP-based app (running on a t2.medium instance) that sends emails (to opted-in users only) via SES and both are located in the same region. The app was launched earlier this year and the sending of emails worked properly for months. We recently switched to sending via mailgun (so we could get more information on a problem we were having), but we did not change any of our SES settings. (Note: Our account is approved to send 50k emails per hours - we are trying to send several hundred.)

I wrote a adjunct utility for our app, which also sends emails, and I decided to continue using SES for this utility. A simplified version of the code follows. Note that I kept the layout of this test program as close to the actual utility as possible (and it should be obvious that the utility makes a database call, etc.)

<?php
  require_once dirname(__FILE__) . '/PHPMailer-master/PHPMailerAutoload.php';

  $mail = new PHPMailer;

  $mail->isSMTP();
  $mail->Host = 'email-smtp.us-west-2.amazonaws.com';
  $mail->SMTPAuth = true;
  $mail->Username = 'my_user_name';
  $mail->Password = 'my_password';
  $mail->SMTPSecure = 'tls';

  $mail->From = 'from_sender';
  $mail->FromName = 'WebTeam';
  $mail->IsHTML(true);

  $oldt = microtime(true);

  while(true) {
    $first_name = 'first_name';
    $email = 'to_recipient';
    $strCnt = 'many';

    $subject = "Lots of great new things to buy";
    $body = "<p>" . $first_name . ",</p>";
    $body = $body . "<p>You have ' . $strCnt . ' new things to buy waiting for you. Don't let them slip by! ";
    $body = $body . "Click <a href='http://fake_url.com'>here</a> to see them!</p>";
    $body = $body . "<p>The Web Team</p>";

    $mail->addAddress($email);
    $mail->Subject = $subject;
    $mail->Body    = $body;

    $newt = microtime(true);
    echo 'email build done: ' .  $newt - $oldt . PHP_EOL;
    $oldt = $newt; 

    if(!$mail->send(true)) {
      echo 'error sending email: ' . $mail->ErrorInfo . PHP_EOL;
    } else {
      $newt = microtime(true);
      echo 'email sent: ' . $newt - $oldt . PHP_EOL . PHP_EOL;
      $oldt = $newt; 
    }

    $mail->ClearAllRecipients();  // added line
  }
?>

Quite simple!

But, here's the rub. When I ran this the first time, the first email took less than one second to send, the second one took 31 seconds, and the third one required 191 seconds. I then added the one more line of code and ran the program again. This time, the first email took 63 seconds to send. After about 20 minutes, I ran the program a third time. This time, the first three emails were sent in less than one second each, but the fourth one took 191 seconds. I then ran it a fifth time, and the first email took 135 seconds to send. (Do note that all of the emails were received.)

What the heck is going on? More importantly, how do I resolve the problem?

This is not SES being slow. This is a documented, deliberate limitation on EC2 itself, with two possible workarounds.

From the SES documentation:

Important

Amazon Elastic Compute Cloud (Amazon EC2) throttles email traffic over port 25 by default. To avoid timeouts when sending email through the SMTP endpoint from EC2, use a different port (587 or 2587) or fill out a Request to Remove Email Sending Limitations to remove the throttle.

http://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-connect.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM