2017-03-21 91 views
-1

這是我的php腳本,可以在郵件發送之前添加幾秒鐘的時間延遲嗎?例如,每次我使用聯繫表單時,我都希望郵件在20秒後發送。如何在電子郵件中設置時間延遲php Mail()

<?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 [email protected] - 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: [email protected] \n"; // This is the email address the generated message will be from. We recommend using something like [email protected] 
$headers .= "Reply-To: $email_address"; 

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

回答

0

mail函數在php中被阻塞。腳本中不能設置相同的值。更好的實現是在該隊列上有一個redis隊列和推送郵件請求。一旦完成,您可以有一個工作節點來分析來自redis隊列的數據,並在後臺使用mail函數。這樣腳本本質上變得非阻塞。

還有其他一些替代redis的方法,比如rabbitmq和zmq。