PHP 发送邮箱

本文档详细介绍了如何使用PHPMailer库通过SMTP服务发送QQ邮件。首先,你需要在QQ邮箱中开启SMTP服务,然后在PHP环境中启用openssl扩展。接着,通过Composer安装PHPMailer库。在代码中设置SMTP服务器、端口、用户名、密码和加密方式等参数,最后编写发送邮件的PHP代码。如果成功,将显示'Message has been sent',否则会捕获并显示错误信息。
摘要由CSDN通过智能技术生成

1.QQ 邮箱开通 SMTP 服务

开通方法

2.PHP开启 openssl 扩展

3.安装 Github PHPMailer

composer require phpmailer/phpmailer

4.代码

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

require_once __DIR__ ."/vendor/autoload.php";

$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_CLIENT;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Host       = 'smtp.qq.com';                         // Set the SMTP server to send through
    $mail->Port       =  465;                                   // TCP port to connect to  , 465
    $mail->CharSet    = "UTF-8";
    $mail->FromName   = "lvpeilin";
    $mail->Username   = '422615924@qq.com';                     // SMTP username
    $mail->Password   = '****';                     // SMTP password, 授权码
    $mail->setFrom('422615924@qq.com', 'Dyspace');
    $mail->addAddress('422615924@qq.com', 'lvpeilin');     // Add a recipient

    // 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. Mailer Error: {$mail->ErrorInfo}";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值