New thread for sending email

Hi all,

I used to develop web application using Java. For example in user registration to our web app, we usually send the activation code via email. In Java, usually I create a new thread and put the email sending process in this new thread so it does not blocked the rest of the code execution. I am relatively new to PHP, what should we do it in Yii/PHP?

Thank you for your help.

Kind regards,

Daniel

You could add your emails to a queue (whatever that is, e.g. a db table) and use a separate process (or a cron job) to process that queue. One implementation for that is available from PEAR, but never used it though.

Alternatively you can create a console command (or pure php, whatever you like) and run it as background process (so it won’t block php execution).

Example:


exec('/usr/bin/php -f /var/www/index.php sendmail "to@address.com" "title" "body"  > /dev/null &');

You may put that into a php class so you can simply do:




$mail = new Mail;

// set receiver, subject, ...

$mail->send();