itajackass
itajackass

Reputation: 488

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

Upvotes: 4

Views: 2562

Answers (1)

klee500
klee500

Reputation: 46

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)

Upvotes: 3

Related Questions