0

我想知道人们如何处理向他们的用户群发送电子邮件。我正在使用 PHP,而 SendGrid 正在处理电子邮件。用例是一个时事通讯。

当我在测试脚本时给自己发送 50 封电子邮件时,已经花费了相当长的时间,我担心如果一次发送更多电子邮件,应用程序会超时。

我真的不想给自己发 100 封、1000 封、10000 封电子邮件,看看上限是多少。

如果有帮助,我将在中型 EC2 Linux 上运行它。

4

1 回答 1

1

有几种方法可以使用 SendGrid 完成此操作。

并行发送电子邮件

您目前可能正在以一系列方式发送电子邮件(即发送一封电子邮件,等待它发送,然后再发送下一封)。这意味着如果您需要 1 秒来发送一封电子邮件,那么您将需要 100 秒来发送一封电子邮件。

幸运的是,PHP 的 cURL 库允许您并行发送许多 POST 请求,而不是串联发送。

// An array of the emails you want to send to:
$emails = array("joe@example.org", "sue@example.com");

$credentials = array("api_user" => YOUR_SENDGRID_USERNAME, "api_key" => YOUR_SENDGRID_PASSWORD);

$multi_handle = curl_multi_init();
$handles = array();
// Loop through each email and create a cURL Handle for the email
// The cURL handle will POST an email to SendGrid with the proper info
foreach ($emails as $key => $email) {
    // Create an email data object with your credentials in it,
    // we'll POST this to SendGrid
    $email_data = $credentials;

    // Fill out all the information you want for the email
    $email_data = array(
        "to" => $email,
        "from" => "you@example.net",
        "subject" => generate_subject(),
        "html" => generate_html(),
        "text" => generate_text()
    );

    $handles[$key] = curl_init();
    curl_setopt($handles[$key], CURLOPT_URL, "https://api.sendgrid.com/api/mail.send.json");
    curl_setopt($handles[$key], CURLOPT_POST, 1);
    curl_setopt($handles[$key], CURLOPT_POSTFIELDS, $email_data);

    curl_setopt($handles[$key], CURLOPT_RETURNTRANSFER, true);
    curl_multi_add_handle($multi_handle, $handles[$key]);
}

$running = null;
// Run through each cURL handle and execute it.
do {
    curl_multi_exec($multi_handle, $running);
} while ($running);

curl_multi_close($multi_handle);

如果您要发送大量超独特的电子邮件,此方法效果很好,但是它仍然会导致来自您的服务器的一堆 POST 请求,这些请求很慢且消耗资源。往往有更好的方法。

使用 SMTPAPI 发送电子邮件

SendGrid 有一个允许您使用该参数SMTPAPI通过一个 POST 请求发送多达 10,000 封电子邮件。您可以使用标签to使这些电子邮件具有半唯一性。substitution

为此,您将执行以下操作:

// An array of the emails you want to send to:
$emails = array("joe@example.org", "sue@example.com");

// Encode it into the SendGrid SMTPAPI Format
$smtpapi = array( "to" =>  $emails );

$params = array(
    "api_user"  => YOUR_SENDGRID_USERNAME,
    "api_key"   => YOUR_SENDGRID_PASSWORD,
    "to"        => "you@example.com",
    "subject"   => "testing from curl",
    "html"      => "testing body",
    "text"      => "testing body",
    "from"      => "you@example.com",
    "x-smtpapi" => json_encode($smtpapi);
  );


$request =  "https://api.sendgrid.com/api/mail.send.json";

// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// obtain response
$response = curl_exec($session);
curl_close($session);

但是,使用SendGrid 的 PHP 库可能会更好

$emails = array("joe@example.org", "sue@example.com");

$sendgrid = new SendGrid(YOUR_SENDGRID_USERNAME, YOUR_SENDGRID_PASSWORD);
$email    = new SendGrid\Email();
$email->setTos($emails)->
       setFrom('me@bar.com')->
       setSubject('Subject goes here')->
       setText('Hello World!')->
       setHtml('<strong>Hello World!</strong>');

$sendgrid->send($email);

排队

最后,您可以利用队列并从不同进程发送所有电子邮件以加快执行时间,有关如何执行此操作的信息在此 StackOverflow 响应中。

于 2014-09-13T16:13:05.403 回答