PHPMailer send mail (PHP send email)

Many websites will require you to enter your email address when registering, and its application scenarios are relatively wide, such as receiving verification codes for registered accounts, notifications of successful registration, login notifications, password retrieval verification notifications, etc. This article will introduce how to use PHP to send emails.

Open source project PHPMailer

The open source project PHPMailer is used, and 163 mailbox is used as the sender in this article.

There are 4 PHP files in total.

index.php

<?php 
use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\Exception; 

require './Exception.php'; 
require './PHPMailer.php'; 
require './SMTP.php'; 

$mail = new PHPMailer(true);
try { 
    
    //服务器配置 
    $mail->CharSet ="UTF-8";                     //设定邮件编码 
    $mail->SMTPDebug = 0;                        // 调试模式输出 
    $mail->isSMTP();                             // 使用SMTP 
    $mail->Host = 'smtp.163.com';                // SMTP服务器 
    $mail->SMTPAuth = true;                      // 允许 SMTP 认证 
    $mail->Username = '[email protected]';                // SMTP 用户名  即邮箱的用户名 
    $mail->Password = 'xxx';             // SMTP 密码  部分邮箱是授权码(例如163邮箱) 
    $mail->SMTPSecure = 'ssl';                    // 允许 TLS 或者ssl协议 
    $mail->Port = 465;                            // 服务器端口 25 或者465 具体要看邮箱服务器支持 

    $mail->setFrom('[email protected]', 'PHPMailer');  //发件人 
    $mail->addAddress('[email protected]', 'TANKING');  // 收件人 
    //$mail->addAddress('[email protected]');  // 可添加多个收件人 
    $mail->addReplyTo('[email protected]', 'PHPMailer'); //回复的时候回复给哪个邮箱 建议和发件人一致 
    //$mail->addCC('[email protected]');                    //抄送 
    //$mail->addBCC('[email protected]');                    //密送 

    //发送附件 
    // $mail->addAttachment('../xy.zip');         // 添加附件 
    // $mail->addAttachment('../thumb-1.jpg', 'new.jpg');    // 发送附件并且重命名 

    //Content 
    $mail->isHTML(true); // 是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容 
    $mail->Subject = '这里是邮件标题' . time(); 
    $mail->Body    = '<h1>这里是邮件内容</h1>' . date('Y-m-d H:i:s'); 
    $mail->AltBody = '如果邮件客户端不支持HTML则显示此内容'; 

    $mail->send(); 
    echo '邮件发送成功'; 
} catch (Exception $e) { 
    echo '邮件发送失败: ', $mail->ErrorInfo; 
}

code description

Some parameters involved in index.php:

SMTP username: sender's mailbox
SMTP password: sender's mailbox authorization code

How to obtain the authorization code of the sender's email address?

Take 163 mailbox as an example, log in your mailbox, enter the background, click Settings -> POP3/SMTP/IMAP

Follow the text prompts to get the authorization code

After the authorization code is obtained, it can be configured into the code to send emails.

how to use?

Directly visit index.php to send emails!

Source code download

https://github.com/likeyun/PHPMailer_SendEmail

Guess you like

Origin blog.csdn.net/weixin_39927850/article/details/128333239