简体   繁体   English

Phpmailer 和异步发送电子邮件

[英]Phpmailer and async sending email

I read some other question before ask here cause other answers don't response to my problem.我在这里问之前阅读了一些其他问题,因为其他答案没有回应我的问题。

I've a custom made cms in php.我在 php 中有一个定制的 cms。 For example if I insert a new payment, the script send to all admin user a notify:例如,如果我插入新付款,脚本会向所有管理员用户发送通知:

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);

} }

This is an example of send_email_to_admin() function:这是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
}

Now if I have 1 or 2 admins is ok...but if I have a lot of admins the time gap is not good for waiting a result for each sent.现在,如果我有 1 或 2 个管理员也可以……但是如果我有很多管理员,那么等待每个发送的结果的时间间隔并不好。

I'd like to pass the foreach loop to a child process if it possible that can process async the entire loop of SENDING and SAVING inside MYSQL the results.如果可能的话,我想将 foreach 循环传递给子进程,如果它可以在 MYSQL 结果中异步处理整个 SENDING 和 SAVING 循环。

So header("Location: " . $homepage) can be executed immediately.所以header("Location: " . $homepage)可以立即执行。

Some additional info:一些额外的信息:

  1. I'm using hosted server so i can't install packages and libraries我正在使用托管服务器,所以我无法安装包和库

  2. I can use only function provided by default PHP config我只能使用默认 PHP 配置提供的功能

  3. I can't use a cronjob queue method cause my hosting not provide a free service我无法使用 cronjob 队列方法,因为我的主机不提供免费服务

  4. i'd like a solution working on IIS windows server and a Linux based server我想要一个适用于 IIS Windows 服务器和基于 Linux 的服务器的解决方案

I'd like an little script example based on my code cause i never used a async method in php and i don't know nothing about it :(我想要一个基于我的代码的小脚本示例,因为我从未在 php 中使用过异步方法,我对此一无所知:(

Sorry for my english对不起我的英语不好

you could implement a queue and process this queue (asynchronously) with a curl call.您可以实现一个队列并使用 curl 调用(异步)处理该队列。

Instead of sending the emails directly from function send_email_to_admin() , insert a new dataset in a dedicated SQL table EmailQueue .不是直接从函数send_email_to_admin()发送电子邮件,而是在专用 SQL 表EmailQueue插入一个新数据集。 Next you write a recursive function that processes this queue (all emails waiting to be send) until the table EmailQueue is empty.接下来编写一个递归函数来处理这个队列(所有等待发送的电子邮件),直到表EmailQueue为空。

insert payment:插入付款:

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

curl_process_email_queue();
...

make CURL call, to detach from parent script ( source ):进行 CURL 调用,从父脚本( )分离:

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 );
}

queue email:队列电子邮件:

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

seperate PHP send_queued_emails.php script to be called via URL by cURL, that actualy sends the queued emails (recursively, until queue is empty):单独的 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();
};

To improve the reliability you may want to use transactions , to prevent issues for concurrent calls of the script send_queued_emails.php .为了提高可靠性,您可能需要使用事务,以防止脚本send_queued_emails.php并发调用出现问题。 For other options also see Methods for asynchronous processes in PHP 5.4 .对于其他选项,另请参阅PHP 5.4 中异步进程的方法

EDIT: added "close connection early, but keep executing script" as proposed in this thread .编辑:添加了“提前关闭连接,但继续执行脚本”,如该线程中的建议。 This should enable you to even set a higher timeout for the cURL call.这应该使您甚至可以为 cURL 调用设置更高的超时时间。

EDIT2: added header("Content-Encoding: none"); EDIT2:添加header("Content-Encoding: none"); as proposed by itajackass (refer to comments)由 itajackass 提出(请参阅评论)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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