PHP 发送邮件

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('lvpeilin@dyspace.net', '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}";
}

原文

使用PHPMAILER实现PHP发邮件功能

posted @ 2019-12-30 14:11  GoodByeZ  阅读(301)  评论(0编辑  收藏  举报