1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Leo.xie
- * Date: 2017/7/28
- * Time: 9:47
- * Description:邮件发送服务
- */
- namespace app\common\service;
- use think\Log;
- class MailService{
- private $account = [
- ['account'=>'811329263@qq.com','key'=>'jwxrmfukwalobbhi'],
- ];
- /**
- * @param string $sendName 给自己作为发送者设置的姓名
- * @param string $themeName 邮件的标题【主题】名称
- * @param string $content 邮件的内容是什么
- * @param array $replyMail 要求邮件回复给谁
- * @param array $toMail 发送邮件给谁的列表[0=>['mail'=>'xx@qq.com','name'=>'xx']]
- * @return bool
- * @throws \Exception
- */
- public function send($sendName,$themeName,$content,$replyMail,$toMail=[]){
- if(!$sendName || !$themeName || !$content || !$toMail){
- HelperService::$_httpErr = "Mail params is error";
- return false;
- }
- vendor('phpmailer.class#phpmailer'); //下载phpmailer并include两个文件
- vendor('smtp.class#phpmailer');
- date_default_timezone_set('Asia/Shanghai');
- $random = rand(0,count($this->account)-1);
- $mail = new \PHPMailer(); //得到一个PHPMailer实例
- $mail->CharSet = "utf-8"; //设置采用utf-8中文编码(内容不会乱码)
- $mail->IsSMTP(); //设置采用SMTP方式发送邮件
- $mail->Host = "ssl://smtp.qq.com"; //设置邮件服务器的地址(若为163邮箱,则是smtp.163.com)
- $mail->Port = 465; //设置邮件服务器的端口,默认为25
- $mail->From = $this->account[$random]['account']; //设置发件人的邮箱地址
- $mail->FromName = "{$sendName}"; //设置发件人的姓名(可随意)
- $mail->SMTPAuth = true; //设置SMTP是否需要密码验证,true表示需要
- $mail->Username= $this->account[$random]['account']; //(后面有解释说明为何设置为发件人)
- $mail->Password = $this->account[$random]['key'];
- $mail->Subject = "{$themeName}"; //主题
- $mail->AltBody = "text/html"; // optional, comment out and test
- $mail->Body = "{$content}"; //内容
- $mail->IsHTML(true);
- $mail->WordWrap = 50; //设置每行的字符数
-
- if(empty($replyMail)){
- $replyMail = '811329263@qq.com';
- }
- $mail->AddReplyTo("{$replyMail}","from"); //设置回复的收件人的地址(from可随意)
- foreach($toMail as $mail_item){
- if(!$mail_item['mail'] || !$mail_item['name']){
- continue;
- }
- $mail->AddAddress($mail_item['mail'],$mail_item['name']); //设置收件的地址(to可随意)
- }
- try {
- return $mail->Send();
- }catch(\Exception $ex){
- Log::record('邮件发送失败:'.$ex->getMessage());
- HelperService::$_httpErr = $ex->getMessage();
- return false;
- }
- }
- }
|