如何设置电子邮件的时间延迟php Mail()

时间:2017-03-21 15:32:07

标签: php

这是我的php脚本,是否可以在邮件发送之前添加一些秒延迟时间?例如,每次我使用联系表格时,我希望邮件在延迟20秒后发送。

    <?php

// Debugging tools. Only turn these on in your development environment.

error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");

// Special mail settings that can make mail less likely to be considered spam
// and offers logging in case of technical difficulties.

ini_set("mail.log", "/tmp/mail.log");
ini_set("mail.add_x_header", TRUE);

//variables
$name = strip_tags(htmlspecialchars($_POST['Name']));
$email_address = strip_tags(htmlspecialchars($_POST['Email']));
$message = strip_tags(htmlspecialchars($_POST['Message']));

// Create the email and send the message
$to = ''; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form:  $name";
$email_body = "You have received a new message from example... contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nMessage:\n$message";
$headers = "From: noreply@example.xyz \n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";  

if(mail($to,$email_subject,$email_body,$headers)){ echo "Mail sent!";} else{ echo "Error, check your logs."; }
return true; 

1 个答案:

答案 0 :(得分:0)

mail函数在php中阻塞。您不能在脚本中设置相同的内容。更好的实现是在该队列上有一个redis队列和推送邮件请求。完成后,您可以拥有一个工作节点,该节点从redis队列解析数据并在后台使用mail函数。这样,脚本就变得非阻塞了。

还有像rabbitmq和zmq这样的redis的其他替代品。