php 使用composer phpmailer发送qq邮件

该博客详细介绍了如何通过Composer安装PHPMailer库,并展示了使用PHPMailer官方DEMO发送QQ邮件的PHP代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文很详尽

参考:http://blog.youkuaiyun.com/baidu_30000217/article/details/51550259


使用composer安装phpmailer

composer require phpmailer/phpmailer


php代码,使用的phpmailer官方demo


<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.qq.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    //smtp登录的账号 这里填入字符串格式的qq号即可
    $mail->Username = '123@qq.com';                 // SMTP username
    //smtp登录的密码 使用生成的授权码(通过qq邮箱活得的授权码)
    $mail->Password = 'xxxxxxxxi';                           // SMTP password
    //设置使用ssl加密方式登录鉴权
    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    //设置ssl连接smtp服务器的远程服务器端口号,以前的默认是25,但是现在新的好像已经不可用了 可选465或587
    $mail->Port = 465;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('123@qq.com', 'Mailer');
    //设置收件人邮箱地址 该方法有两个参数 第一个参数为收件人邮箱地址
    // 第二参数为给该地址设置的昵称 不同的邮箱系统会自动进行处理变动 这里第二个参数的意义不大
    $mail->addAddress('123@qq.com', 'Joe User');     // Add a recipient
    //$mail->addAddress('ellen@example.com');               // Name is optional
    //答复
    $mail->addReplyTo('123@qq.com', 'Information');
    //$mail->addCC('cc@example.com');
    //$mail->addBCC('bcc@example.com');

    //Attachments 附件
    //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //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;
}


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值