hen*_*dry 14 php email asynchronous amazon-web-services amazon-ses
我有PHP mail()使用ssmtp,它没有队列/假脱机,并且与AWS SES同步.
我听说我可以使用SwiftMail来提供一个假脱机,但是我无法像我目前那样找到一个简单的配方来使用它mail().
我希望用最少量的代码来提供异步邮件.我不在乎电子邮件是否发送失败,但是有一个日志会很好.
任何简单的提示或技巧?没有运行一个完整的邮件服务器?我在想sendmail包装器可能是答案,但我无法解决nohup.
mag*_*tik 16
你有很多方法可以做到这一点,但处理线程不一定是正确的选择.
from,to,message,sent(Boolean设置为true当你发送的电子邮件).<?php
class MailSpool
{
public static $mails = [];
public static function addMail($subject, $to, $message)
{
self::$mails[] = [ 'subject' => $subject, 'to' => $to, 'message' => $message ];
}
public static function send()
{
foreach(self::$mails as $mail) {
mail($mail['to'], $mail['subject'], $mail['message']);
}
}
}
//In your script you can call anywhere
MailSpool::addMail('Hello', 'contact@example.com', 'Hello from the spool');
register_shutdown_function('MailSpool::send');
exit(); // You need to call this to send the response immediately
Run Code Online (Sandbox Code Playgroud)
Kit*_*nde 10
您必须运行php-fpm才能使fastcgi_finish_request可用.
echo "I get output instantly";
fastcgi_finish_request(); // Close and flush the connection.
sleep(10); // For illustrative purposes. Delete me.
mail("test@example.org", "lol", "Hi");
Run Code Online (Sandbox Code Playgroud)
在完成对用户的请求后,将任意代码排队等待处理非常容易:
$post_processing = [];
/* your code */
$email = "test@example.org";
$subject = "lol";
$message = "Hi";
$post_processing[] = function() use ($email, $subject, $message) {
mail($email, $subject, $message);
};
echo "Stuff is going to happen.";
/* end */
fastcgi_finish_request();
foreach($post_processing as $function) {
$function();
}
Run Code Online (Sandbox Code Playgroud)
立即超时卷曲并让新请求处理它.我之前在共享主机上这样做很酷.(它永远不会酷)
if(!empty($_POST)) {
sleep(10);
mail($_POST['email'], $_POST['subject'], $_POST['message']);
exit(); // Stop so we don't self DDOS.
}
$ch = curl_init("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'email' => 'noreply@example.org',
'subject' => 'foo',
'message' => 'bar'
]);
curl_exec($ch);
curl_close($ch);
echo "Expect an email in 10 seconds.";
Run Code Online (Sandbox Code Playgroud)
小智 5
将AWS SES与PHPMailer一起使用.
这种方式非常快(每秒数百条消息),并且所需的代码不多.
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'ssl://email-smtp.us-west-2.amazonaws.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'blah'; // SMTP username
$mail->Password = 'blahblah'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 443;
Run Code Online (Sandbox Code Playgroud)
不确定我是否正确解释了你的问题,但我希望这会有所帮助.
| 归档时间: |
|
| 查看次数: |
10133 次 |
| 最近记录: |