Phpmailer 和异步发送电子邮件

标签 php asynchronous

我在问这里之前阅读了一些其他问题,因为其他答案没有回应我的问题。

我有一个用 php 定制的 cms。例如,如果我插入一个新的付款,脚本会向所有管理员用户发送一个通知:

function insert_payment() {
// CODE TO INSERT PAYMENT INSIDE MYSQL DB
$sql_payment = "INSERT INTO payments ($amount, ...) VALUES (?, ...);"
...
// NOTIFY ALL ADMINS
foreach ( $array_emails as $email ) {
   send_email_to_admin($email, $subject, $body);
  }


  // redirect to dashboard
  header("Location: " . $homepage);

这是一个 send_email_to_admin() 函数的例子:

function send_email_to_admin($email, $subject, $body) {
       // return example:
       // $result = array(
       //      "Error" = true or false
       //      "DateTimeOfSent" = datetime
       //      "Email" = string
       //)
       // SAVE RESULTS IN MYSQL DB ( I need to register results to be sure email are sent without errors...table can be then seen to a specific pages under admin panel of cms)

       $mail = new PHPMailer;
       ...
       ...
       if(!$mail->send()) {
  $result = array("Error" => true, "DateTimeOfSent" => date("Y-m-d"), "Email" => $mail);
} else {
  $result = array("Error" => false, "DateTimeOfSent" => date("Y-m-d"), "Email" => $mail);
}

       $sql_result = "INSERT INTO SentResult ("Error", "DateTimeSent", "Email", ...) VALUES ( $result['Error'], $result['DateTimeOfSent'], $result['Email'] )"
       ...
       //end function
}

现在,如果我有 1 或 2 个管理员是可以的...但是如果我有很多管理员,时间间隔不利于等待每个发送的结果。

如果可能的话,我想将 foreach 循环传递给一个子进程,该子进程可以异步处理 MYSQL 中结果的整个发送和保存循环。

所以 header("Location: ". $homepage) 可以立即执行。

一些附加信息:

  1. 我使用的是托管服务器,所以我无法安装包和库

  2. 我只能使用PHP配置默认提供的功能

  3. 我无法使用 cronjob 队列方法,因为我的主机不提供免费服务

  4. 我想要一个在 IIS windows 服务器和基于 Linux 的服务器上工作的解决方案

我想要一个基于我的代码的小脚本示例,因为我从未在 php 中使用过异步方法,而且我对此一无所知:(

对不起我的英语

最佳答案

您可以实现一个队列并使用 curl 调用(异步)处理该队列。

不是直接从函数 send_email_to_admin() 发送电子邮件,而是在专用 SQL 表 EmailQueue 中插入一个新数据集。接下来,您编写一个递归函数来处理此队列(所有等待发送的电子邮件),直到表 EmailQueue 为空。

插入付款:

...
// NOTIFY ALL ADMINS
foreach ( $array_emails as $email ) {
   queue_email($email, $subject, $body);
}

curl_process_email_queue();
...

进行 CURL 调用,以从父脚本 (source) 分离:

function curl_process_email_queue() {
  $c = curl_init();
  curl_setopt($c, CURLOPT_URL, $url/send_queued_emails.php);
  curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);  // Follow the redirects (needed for mod_rewrite)
  curl_setopt($c, CURLOPT_HEADER, false);         // Don't retrieve headers
  curl_setopt($c, CURLOPT_NOBODY, true);          // Don't retrieve the body
  curl_setopt($c, CURLOPT_RETURNTRANSFER, true);  // Return from curl_exec rather than echoing
  curl_setopt($c, CURLOPT_FRESH_CONNECT, true);   // Always ensure the connection is fresh

  // Timeout super fast once connected, so it goes into async.
  curl_setopt( $c, CURLOPT_TIMEOUT, 1 );

  return curl_exec( $c );
}

队列邮箱:

function queue_email($email, $subject, $body) {
    $sql = "INSERT INTO emailQueue ("email", "subject", "body") VALUES ($email, $subject, $body)";
    ...
};

单独的 PHP send_queued_emails.php 脚本由 cURL 通过 URL 调用,实际上发送排队的电子邮件(递归地,直到队列为空):

<?php

// close connection early, but keep executing script
// https://stackoverflow.com/a/141026/5157195
ob_end_clean();
header("Connection: close");
ignore_user_abort(true);
ob_start();
echo('Some status message');
$size = ob_get_length();
header("Content-Length: $size");
header("Content-Encoding: none");
ob_end_flush();
flush();
// connection is closed at this point

// start actual processing here
send_queued_emails();

function send_queued_emails() {
    // avoid concurrent access
    $sql = 'START TRANSACTION'; 
    mysqli_query($sql);

    // read one item from the queue
    $sql = 'SELECT "id", email", "subject", "body" FROM emailQueue LIMIT 1';
    $result = mysqli_query($sql);

    // if no more datasets are found, exit the function
    if (!$result || (mysqli_num_rows($result) == 0))
      return; 

    // mail the queried data
    $mail = new PHPMailer;
    ...

    // optionally write the result back to database
    $sql_result = 'INSERT INTO SentResult ... ';
    mysqli_query($sql);

    // delete the email from the queue
    $sql = 'DELETE FROM emailQueue WHERE "id"=...';
    mysqli_query($sql);

    // commit transaction
    $sql = 'COMMIT'; 
    mysqli_query($sql);

    // recursively call the function
    send_queued_emails();
};

要提高可靠性,您可能需要使用 transactions , 以防止脚本 send_queued_emails.php 的并发调用问题。对于其他选项,另请参阅 Methods for asynchronous processes in PHP 5.4 .

编辑:按照本 thread 中的建议添加“尽早关闭连接,但继续执行脚本” .这应该使您甚至可以为 cURL 调用设置更高的超时。

EDIT2:根据 itajackass 的建议添加了 header("Content-Encoding: none");(请参阅评论)

关于Phpmailer 和异步发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48850703/

相关文章:

php - 重置实体管理器的最佳方法

php - 输入文件的表单编辑

php - 如何在 Yii 中创建具有两个用户级别的登录模块

php - 使用 ColdFusion 将 CSV 转换为多维数组

node.js:导出嵌套匿名异步函数中包含的变量

php - 基于所选选项值的表单输入值

c# - 尝试使用异步方法访问 Thread.CurrentPrincipal 时出现 ObjectDisposedException

c# - 如何从 C# 中的异步任务 <bool> 函数获取 bool 结果 - 错误 : Cannot implicitly convert type `void' to `bool'

javascript - 我怎么知道最后一次异步何时完成?

c# - 如何取消异步调用?