Search code examples
phpemailjoomladelayphpmailer

How do I delay sending mails with PHPMailer for multiple days?


I'm trying to create a mailing system for our library. People receive an e-mail when they requested a book. However, I want them to receive an e-mail one day before the hand-in date as well.

So I'm not looking for a sleep script code, I'm looking for a code that can maybe store the data on the server until the hand-in date arrived.

I'm using Joomla 3.5.1 for our website, this is the code for the PHPMailer:

jimport('joomla.mail.mail');

try{
$mail             = new PHPMailer();

$body             = $mailmsgadmin;

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "example.host.com";         // SMTP server
$mail->SMTPDebug  = 2;                      // enables SMTP debug information (for testing)
                                            // 1 = errors and messages
                                            // 2 = messages only
$mail->SMTPAuth   = true;                   // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "example.host.com";         // sets the SMTP server
$mail->Port       = 25;                    // set the SMTP port for the GMAIL server
$mail->Username   = "email"; // SMTP account username
$mail->Password   = "password";         // SMTP account password

$mail->SetFrom('email', 'First Last');

$mail->AddReplyTo($mailuseremail, $mailusername);

$mail->Subject    = $mailsubject;

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";

$mail->MsgHTML($body);

$mail->AddAddress("Users e-mail","Users name");

if(!$mail->Send()) {
  JError::raiseWarning( 'SOME_ERROR_CODE', $mail->ErrorInfo);
}
} catch (Exception $e) {JError::raiseWarning( 'SOME_ERROR_CODE', $e->getMessage());}

Solution

  • Instead of trying to get PHPMailer delay the sending, add your email details to a database table. And then write a cron job that checks the table for emails that need to be sent.