<?php /** Created by PhpStorm. User: puresai Date: 2018/11/29 Time: 14:06 /
require dirname(DIR).'/vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception;
class Mailer { protected $server; protected $host = '127.0.0.1'; protected $port = 9502; // 进程名称 protected $taskName = 'swooleMailer'; // PID路径 protected $pidPath = '/run/swooleMail.pid'; // 设置运行时参数 protected $options = [ 'worker_num' => 4, //worker进程数,一般设置为CPU数的1-4倍 'daemonize' => true, //启用守护进程 'log_file' => '/data/logs/swoole.log', //指定swoole错误日志文件 'log_level' => 0, //日志级别 范围是0-5,0-DEBUG,1-TRACE,2-INFO,3-NOTICE,4-WARNING,5-ERROR 'dispatch_mode' => 1, //数据包分发策略,1-轮询模式 'task_worker_num' => 4, //task进程的数量 'task_ipc_mode' => 3, //使用消息队列通信,并设置为争抢模式 //'heartbeat_idle_time' => 600, //一个连接如果600秒内未向服务器发送任何数据,此连接将被强制关闭 //'heartbeat_check_interval' => 60, //启用心跳检测,每隔60s轮循一次 ]; // 邮件服务器配置 protected $mailConfig = [ 'smtp_host' => 'smtp.163.com', 'username' => '[email protected]', 'password' => 'xmw1015',// SMTP 密码/口令 'secure' => 'ssl', //Enable TLS encryption, ssl also accepted 'port' => 465, // tcp邮件服务器端口 ];
public function __construct($mailConfig, $options = []) { // 构建Server对象,监听端口 $this->server = new swoole_server($this->host, $this->port);
if (!empty($options)) { $this->options = array_merge($this->options, $options); } $this->server->set($this->options);
$this->mailConfig = $mailConfig;
// 注册事件 $this->server->on('Start', [$this, 'onStart']); $this->server->on('Connect', [$this, 'onConnect']); $this->server->on('Receive', [$this, 'onReceive']); $this->server->on('Task', [$this, 'onTask']); $this->server->on('Finish', [$this, 'onFinish']); $this->server->on('Close', [$this, 'onClose']);
// 启动服务 //$this->server->start(); }
protected function init() { // }
public function start() { // Run worker $this->server->start(); }
public function onStart($server) { // 设置进程名 cli_set_process_title($this->taskName); //记录进程id,脚本实现自动重启 $pid = "{$server->master_pid} {$server->manager_pid}"; file_put_contents($this->pidPath, $pid); }
//监听连接进入事件 public function onConnect($server, $fd, $from_id) { $server->send($fd, "Hello {$fd}!" ); }
// 监听数据接收事件 public function onReceive(swoole_server $server, $fd, $from_id, $data) { $res['result'] = 'success'; $server->send($fd, json_encode($res)); // 同步返回消息给客户端 $server->task($data); // 执行异步任务
}
/* @param $server swoole_server swoole_server对象 @param $task_id int 任务id @param $from_id int 投递任务的worker_id @param $data string 投递的数据 / public function onTask(swoole_server $server, $task_id, $from_id, $data) { $res['result'] = 'failed'; $req = json_decode($data, true); $action = $req['action']; echo date('Y-m-d H:i:s')." onTask: [".$action."]. "; switch ($action) { case 'sendMail': //发送单个邮件 $mailData = [ 'emailAddress' => $req['to'], //接收方,改成自己的邮箱可以测试接收邮件 'subject' => $req['subject'], 'body' => $req['body'], ]; $this->sendMail($mailData); break;
default: break; } }
/* @param $server swoole_server swoole_server对象 @param $task_id int 任务id @param $data string 任务返回的数据 */ public function onFinish(swoole_server $server, $task_id, $data) { // }
// 监听连接关闭事件 public function onClose($server, $fd, $from_id) { echo "Client {$fd} close connection "; }
public function stop() { $this->server->stop(); }
private function sendMail($mailData = []) { $mail = new PHPMailer(true);
try { $mailConfig = $this->mailConfig; $mail->isSMTP(); // TCP port to connect to $mail->Host = $mailConfig['smtp_host']; // SMTP服务 $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = $mailConfig['username']; // SMTP 用户名 $mail->Password = $mailConfig['password']; // SMTP 密码/口令 $mail->SMTPSecure = $mailConfig['secure']; // Enable TLS encryption, ssl also accepted $mail->Port = $mailConfig['port']; // TCP 端口
//Recipients $mail->setFrom('[email protected]', 'puresai'); $mail->addAddress('[email protected]'); // Name is optional $mail->addReplyTo('[email protected]', 'Information');
//Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body <b>in bold!</b>'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; }
try { $mailConfig = $this->mailConfig; $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = $mailConfig['smtp_host']; // SMTP服务 $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = $mailConfig['username']; // SMTP 用户名 $mail->Password = $mailConfig['password']; // SMTP 密码/口令 $mail->SMTPSecure = $mailConfig['secure']; // Enable TLS encryption, ssl also accepted $mail->Port = $mailConfig['port']; // TCP 端口
$mail->CharSet = "UTF-8"; //字符集 $mail->setFrom($mailConfig['username'], 'puresai'); //发件人地址,名称 $mail->addAddress($mailData['emailAddress'], '亲'); // 收件人地址和名称 //$mail->addCC('[email protected]'); // 抄送
if (isset($mailData['attach'])) { $mail->addAttachment($mailData['attach']); // 添加附件 }
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = $mailData['subject']; $mail->Body = $mailData['body'];
$mail->send(); return true; } catch (\Exception $e) { echo 'Message could not be sent. Mailer Error: '. $mail->ErrorInfo; return false; } } }
|