php用qq邮箱发送邮件,PHP下利用PHPMailer配合QQ邮箱实现发邮件

今天尝试了下利用php实现邮件的发送。在这里是利用PHPMailer和QQ邮箱实现的。

1 QQ邮箱配置

首先进入QQ邮箱开启STMP服务。设置>账户>

bf423c261a20?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

开启STMP服务

开启前面两个服务,记录下对应的授权码。这里我们用到的是IMAP/STMP服务的授权码。

2 下载安装PHPMailer

可以在github下载PHPMailer直接下载。当然也可以使用composer安装

composer require phpmailer/phpmailer

3 编写发送邮件

public function sendMail(){

$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

$mail->Username = '1751987128@qq.com'; // SMTP username

$mail->Password = 'myfyyzvcjuwccifi'; // SMTP password

$mail->CharSet = 'UTF-8';

$mail->Port = 587; // TCP port to connect to

$mail->setFrom('1751987128@qq.com', 'ache');

$mail->addAddress('1871236783@qq.com', '小仙女'); // Add a recipient

//Content

$mail->isHTML(true); // Set email format to HTML

$mail->Subject = '小仙女你好';

$mail->Body = '小仙女再见';

$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

$mail->send();

echo '邮箱有惊喜';

} catch (Exception $e) {

echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;

}

}

这样就完成了邮件的发送代码的编写。下面是收到的邮件截图。

bf423c261a20?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

效果图

到这里就完成简单的邮件发送。当然也可以编写一个前端页面来自定义邮件内容。也可以编写邮件模版,来发送邮件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值