php 发送SMTP邮件

PHP 发送smtp 邮件

*使用composer 引入 PHPMailer\PHPMailer *
端口号要改成 465 (默认25 端口被封)

public function sendEmail()
    {
        ### 邮箱的配置
        $config = require __DIR__ . '/../config/smtp.php';
       
        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
        try {
            //Server settings
            $mail->SMTPDebug = 0;                                 // Enable verbose debug output
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = $config['host'];                        // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            //smtp登录的账号 
            $mail->Username = $config['username'];                // SMTP username
            //smtp登录的密码
            $mail->Password = $config['password'];                // SMTP password
            //设置使用ssl加密方式登录鉴权
            $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 465;                                    // TCP port to connect to
            $mail->CharSet='UTF-8';                          // 设置编码格式
            //Recipients
            $mail->setFrom($config['username'], $config['sign']);
            //设置收件人邮箱地址 该方法有两个参数 第一个参数为收件人邮箱地址
            // 第二参数为给该地址设置的昵称 不同的邮箱系统会自动进行处理变动 这里第二个参数的意义不大
            $mail->addAddress('1305286751@qq.com', '测试');         // Add a recipient
            //答复
            $mail->addReplyTo('1305286751@qq.com', '测试');
            // 抄送地址
            //$mail->addCC('cc@example.com');
            //$mail->addBCC('bcc@example.com');

            //Attachments 附件(服务器上文件放置地址)
            $mail->addAttachment(WEBPATH.'public/uploadfile/'.get_attachment('608')['attachment'],'code.text');
            //Content
            $mail->isHTML(true);                                    // Set email format to HTML
            $mail->Subject = $config['subject'];
            $mail->Body = $config['body'].'<b>'.$config['sign'].'</b>';
            $mail->AltBody = $config['altBody'];
            $mail->send();
            echo 'Message has been sent';
        } catch (Exception $e) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        }
    }

效果图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值