Sometimes we want to send emails but the timing is not that important.

For example a welcome email can be delayed but a reset password email should not.

This is important because php's mail() function can take some time to respond and that delay will make the user wait which is not good.

This function queues the email.

<?php

/**
 * This function schedules an email by passing -odd paramaters to sendmail.
 * Those emails will be processed depending on: /etc/sysconfig/sendmail (using 1h)
 * if sendmail is running all the time.
 * 
 * @param string $email recipient
 * @param string $subject - subject
 * @param string $message - the message
 * @return bool
 * @author Slavi Marinov | http://orbisius.com
 */
function send_email($email, $subject, $message) {
	$host = empty($_SERVER['HTTP_HOST']) ? `hostname -f` : $_SERVER['HTTP_HOST'];
	$headers = "From: $host Mailer <mailer@$host>\r\n" .
		'Reply-To: help@$host' . "\r\n" .
		'X-Mailer: PHP/' . phpversion();

	// -odd -> tells sendmail to queue the email
	$status = mail($email, $subject, $message, $headers, '-odd');

	return $status;
}

?>

Referral Note: When you purchase through a referral link (if any) on this page, we may earn a commission.
If you're feeling thankful, you can buy me a coffee or a beer