本文介绍了thinkphp5+swoole实现异步邮件群发(smtp方式),分享给大家,具体如下:
1、环境说明
- 阿里云centos7
- thinkphp5.0.11
- swoole2.0.8
2、tp实现邮件发送
在项目下建立如下的文件目录:
其中sendmail.php是我们实际调用的发送邮件的文件。以下是主要代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
namespace app\library\utils\mail;
use app\library\utils\mail\phpmailer;
use app\library\utils\mail\smtp;
use think\log;
error_reporting (e_strict);
date_default_timezone_set( 'asia/shanghai' );
class sendmail
{
static function postmail( $to , $subject = '' , $body = '' ){
$mail = new phpmailer();
$mail ->charset = config( 'mail.charset' );
$mail ->issmtp();
$mail ->smtpdebug = config( 'mail.smtpdebug' );
$mail ->smtpauth = config( 'mail.smtpauth' );
$mail ->smtpsecure = config( 'mail.smtpsecure' );
$mail ->host = config( 'mail.host' );
$mail ->port = config( 'mail.port' );
$mail ->username = config( 'mail.username' );
$mail ->password = config( 'mail.password' );
$mail ->setfrom(config( 'mail.from' ), config( 'mail.name' ));
$mail ->subject = $subject ;
$mail ->msghtml( $body );
$address = $to ;
$mail ->addaddress( $address , '' );
if (! $mail ->send()) {
log::write( 'send to ' . $to . 'error info:' . $mail ->errorinfo);
return false;
} else {
return true;
}
}
}
|
里面的config配置项,我们放在项目底下的config.php文件中,具体配置内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//邮箱设置
'mail' =>[
'charset' => 'utf-8' ,
'smtpdebug' =>0, // 启用smtp调试功能 0关闭
'smtpauth' =>true, // 启用 smtp 验证功能
'smtpsecure' => 'ssl' , // 安全协议
'host' => 'smtp.163.com' , // smtp 服务器
'port' =>465, // smtp服务器的端口号
'username' => '**********' , // smtp服务器用户名
'password' => '**********' , // smtp服务器密码
'from' => '*********@163.com' , // 发件人邮箱
'name' => 'blue' , // 发件人邮箱
]
|
2.1这里我们只是实现了smtp协议发送的demo,所以在封装的层面上没有做到很全面,不是很灵活。图中其他两个文件(phpmailer.php和smtp.php)是phpmailer邮件发送类的核心文件,为了简化调用,抽取了出来,详细的用法和参数设置,可以参考gayhub的使用说明phpmailer
2.2 注意点
在此步骤中,我们需要注意几点:1是你设置的邮件发送的账号是否已经开启smtp并且找对对应的安全协议和端口号。2、当前服务器是否支持smtp服务,这边很多时候会受一些socket函数的影响,遇到问题的时候,我们应该把smtpdebug参数设置为1,然后根据debug信息去细心调试。3、发出的邮件有些会被放入垃圾箱,注意查收。
2.3 调用
建立如下的文件目录结构:
在index.php中调用发送邮件的方法,具体代码如下
1
2
3
4
5
6
7
|
public function sendmail(){
if (sendmail::postmail( '937069176@qq.com' , 'test' , '123' )){
echo 'send success' ;
} else {
echo 'send fail' ;
}
}
|
2.4 调用结果
我们可以在qq邮箱的垃圾箱中找到我们刚刚发送的一封邮件
3、结合swoole实现异步群发3.1安装swoole
swoole扩展安装的详细步骤官网上面都有,不再赘述,
安装完swoole之后,建议为自己的ide加上swoole的代码提示,配置ide提示swoole代码的传送门
3.2实现异步群发
我们先实现异步的服务端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/**
* description:服务端
*/
public function syncsend(){
$serv = new \swoole_server( '0.0.0.0' ,8082);
$serv ->set( array ( 'task_worker_num' => 4));
$serv ->on( 'receive' , function ( $serv , $fd , $from_id , $data ) {
$task_id = $serv ->task( $data );
echo "开始投递异步任务 id=$task_id\n" ;
});
$serv ->on( 'task' , function ( $serv , $task_id , $from_id , $data ) {
echo "接收异步任务[id=$task_id]" .php_eol;
for ( $i = 0 ; $i <20; $i ++){
if (sendmail::postmail( '937069176@qq.com' , 'test' , $data )){
echo 'send' . $i . ' success' . "\n" ;
} else {
echo 'send' . $i . ' fail' . "\n" ;
}
}
$serv ->finish( '' );
});
$serv ->on( 'finish' , function ( $serv , $task_id , $data ) {
echo "异步任务[id=$task_id]完成" .php_eol;
});
$serv ->start();
}
|
在服务端,我们用了一个20的loop来模拟了群发,实际换成不同的email地址就可。
下面我们实现客户端
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/**
* description:客户端
*/
public function index()
{
$client = new \swoole_client(swoole_sock_tcp, swoole_sock_sync);
$ret = $client ->connect( "127.0.0.1" , 8082);
if ( empty ( $ret )){
echo 'error!connect to swoole_server failed' ;
} else {
$client ->send( 'blue' ); //这里只是简单的实现了发送的内容
}
}
|
3.3开启服务端
这里必须要用cli方式,我们进入项目的根目录。
执行
1
|
netstat -apn | grep 8082
|
先监测一下8082端口是否被占用,如果占用则
1
|
kill -9 pid(进程号)
|
杀死进程
然后我们执行
1
|
php public /index.php index/index/syncsend
|
这条命令的作用是用cli模式来使我们的8082端口处于监听状态。且保持这个终端处于活跃状态,这样方便我们查看发送的结果。
接着我们可以用cli或者浏览器访问的方式来访问我们的客户端
1
|
php public /index.php index/index/index
|
当我们执行一次的时候客户端的时候,我们可以在服务端的终端看到屏幕上面出现如下的画面:
说明我们已经正常群发了。
下面,我们就来测试异步,因为163服务器会对ip进行检测,如果在某个时间发送也别频繁的话,会出现发送失败的情况,所以我们测试的时候,采用连续执行4次客户端的方式
下面是执行的结果:
从图中发送的顺序来看,我们可以很容易的判断,我们已经实现了异步的发送。
邮箱里面也接收到了刚刚发送的邮件。
4、后记~
swoole是一种想要熟悉和熟练使用的扩展,但是限于网络编程知识的不足,所以还是要多多测试和学习,demo中有不足的地方,还请指出qaq
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://segmentfault.com/a/1190000011539703