3

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. 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:

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.

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.

So header("Location: " . $homepage) can be executed immediately.

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

  3. I can't use a cronjob queue method cause my hosting not provide a free service

  4. i'd like a solution working on IIS windows server and a Linux based server

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 :(

Sorry for my english

itajackass
  • 346
  • 1
  • 2
  • 15
  • is the email the same to each admin? If so just send one email with all the different email addresses in the "to" field – ADyson Feb 18 '18 at 11:29
  • no... $body is personalized for each admin. but this is only an example... my goal is to implement an async delivery method in all case. i don't search for a workaround for using phpmailer using multiple address at the same time... – itajackass Feb 18 '18 at 11:40
  • https://stackoverflow.com/questions/36171222/async-curl-request-in-php might help, with or without the cURL part. Also https://blog.programster.org/php-async-curl-requests – ADyson Feb 18 '18 at 15:08

1 Answers1

2

you could implement a queue and process this queue (asynchronously) with a curl call.

Instead of sending the emails directly from function send_email_to_admin(), insert a new dataset in a dedicated SQL table EmailQueue. Next you write a recursive function that processes this queue (all emails waiting to be send) until the table EmailQueue is empty.

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

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

// 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. For other options also see Methods for asynchronous processes in 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.

EDIT2: added header("Content-Encoding: none"); as proposed by itajackass (refer to comments)

klee500
  • 36
  • 6
  • Wow this is a great answer. I'll try it on my localhost. I don't know now if in my hosting server cURL is available so...tell me if is a good idea to insert a check like if( function_exists('curl_version')) . If is true, do your method posted. If false...continue using my original script... Is it good to prevent any compatibility? – itajackass Feb 18 '18 at 12:45
  • @itajackass I think you can tell if curl is available by looking at the phpinfo() for the server – ADyson Feb 18 '18 at 15:07
  • Klee, can you clarify where the async part of this is? cURL in PHP is not async by default – ADyson Feb 18 '18 at 15:10
  • @ADyson yes I can visually do it...but if I use a variable check...i can write a "universal" script work for both situation...is not true? – itajackass Feb 18 '18 at 15:45
  • @ADyson, as the cURL call timeouts after 1 second, it then gets detached from the the calling script (if cURL call did not complete before). Please refer to the mentioned article for more details and other options: http://davenewson.com/posts/2012/methods-for-asynchronous-processes-in-php-5-4.html – klee500 Feb 18 '18 at 15:51
  • @klee500 I see mentioned article...i think is necessary also header("Content-Encoding: none") to prevent a gzip enabled option – itajackass Feb 18 '18 at 20:07
  • Hi, this is me @itajackass with my other account. I tried the script but unfortunately the script never run "send_queued_emails()". It stop before. If I comment //ob_end_flush() and //flush() the script go until the end.... – Giuseppe Lodi Rizzini Feb 23 '18 at 17:00
  • Hi, i solved the problem. It was only a typing error PHP not show in the recursive script! Thanks! – itajackass Feb 24 '18 at 12:44
  • Instead of using a database to store the data, is it not possible to send those data with POST in the cURL request? – MichaelJorgensenDK Jun 11 '19 at 13:13
  • @user2192013: sure you could post this data via cURL instead of "caching" it in a database. However, this approach might be a security issue, as `send_queued_emails.php` would then act like an anonymous mail relay (if the URL is reachable from the internet, it could be used to send SPAM). – klee500 Jun 12 '19 at 14:19