Fork me on GitHub

PHPMailer发送邮件

1.composer require phpmailer/phpmailer

使用这个包

2.方法:

    private function sendMail($file)
    {
        try {
            $mail = new PHPMailer(); //实例化

            $mail->IsSMTP(); // 启用SMTP
            //$mail->SMTPDebug = SMTP::DEBUG_SERVER;
            $mail->Port = 25;  //邮件发送端口

            $mail->SMTPAuth = true;  //启用SMTP认证
            $mail->SMTPSecure = 'ssl';
            $mail->Port = 465;
            $mail->Host = "smtp.163.com";

            $mail->CharSet = "UTF-8"; //字符集
            $mail->Encoding = "base64"; //编码方式
            $mail->Username = "xxx@163.com";  //你的邮箱
            $mail->Password = "xxxx";  // 授权码
            $mail->Subject = "报警邮件"; // 邮件标题
            $mail->From = "xxx@163.com";  // 发件人地址(也就是你的邮箱)issmartyman@aliyun.com
            $mail->FromName = "xxx";   //发件人姓名

            $address = "xxx@aliyun.com"; // 收件人email
            $address2 = "xxx@qq.com"; // 收件人email
            $address3 = "xx@qq.com"; // 收件人email
            $address4 = "xxx@qq.com"; // 收件人email
            $mail->AddAddress($address, "myself");    //添加收件人1(地址,昵称)
            $mail->AddAddress($address2, "jack_chen");    //添加收件人2(地址,昵称)
            $mail->AddAddress($address3, "sunflower");    //添加收件人3(地址,昵称)
            $mail->AddAddress($address4, "zhoudi");    //添加收件人4(地址,昵称)
            $mail->AddAttachment($file,'掉线设备信息报表.xls'); // 添加附件,并指定名称
//        $mail->AddAttachment('xx1.xls','我的附件1.xls'); // 可以添加多个附件
//        $mail->AddAttachment('xx2.xls','我的附件2.xls'); // 可以添加多个附件
            $mail->IsHTML(true); //支持html格式内容
            //$mail->AddEmbeddedImage("logo.jpg", "my-attach", "logo.jpg"); //设置邮件中的图片

            $mail->Body = '设备在大量掉线,具体请查看附件'; // 邮件主体内容
            if (!$mail->send()) {
                echo "发送失败: " . $mail->ErrorInfo;
            } else {
                echo "成功";
            }
        } catch (\PHPMailer\PHPMailer\Exception $e) {
            Log::error($e->getMessage());
            throw new BusinessException('邮件发送失败');
        }
    }

3.特别宁谢

reference:  https://blog.csdn.net/weixin_38230961/article/details/94627469

posted @ 2021-08-19 17:11  我曾如此奢望  阅读(40)  评论(0编辑  收藏  举报