通过SMTP服务器发送HTML电子邮件

PHPMailer库:
我们将通过SMTP服务器发送PHPMailer发送电子邮件。
因此,包括PHPMailer库文件并初始化PHPMailer对象。

//Import PHPMailer classes into the global namespace 
use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\Exception; 

//Include PHPMailer library files 
require 'PHPMailer/Exception.php'; 
require 'PHPMailer/PHPMailer.php'; 
require 'PHPMailer/SMTP.php'; 

//Create an instance of PHPMailer class 
$mail = new PHPMailer;

请注意:我们无需单独下载PHPMailer库。
所有必需的PHPMailer库文件都包含在源代码中,我们可以在没有COMPOSER中安装PHPMailer。

SMTP配置:
指定SMTP服务器主机('$mail-> host'),用户名('$mail-> username'),密码('$mail-> password')和port('$mail-> password'),而port('$mail-> port')SMTP服务器凭据。

//SMTP configuration
$mail->isSMTP();
$mail->Host     = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@example.com';
$mail->Password = '**';
$mail->SMTPSecure = 'tls';
$mail->Port     = 587;

配置电子邮件:
指定一些基本的电子邮件设置(如,发件人电子邮件和名称,收件人电子邮件,主题,消息等)。
将“ISHTML()”设置为TRUE,用于将电子邮件发送为HTML格式。

//Sender info 
$mail->setFrom('sender@example.com', 'SenderName'); 
$mail->addReplyTo('reply@example.com', 'SenderName'); 

//Add a recipient 
$mail->addAddress('recipient@example.com'); 

//Add cc or bcc  
$mail->addCC('cc@example.com'); 
$mail->addBCC('bcc@example.com'); 

//Email subject 
$mail->Subject = 'Send Email via SMTP using PHPMailer'; 

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

//Email body content 
$mailContent = ' 
    <h2>Send HTML Email using SMTP Server in PHP</h2> 
    <p>It is a test email by onitroad, sent via SMTP server with PHPMailer using PHP.</p> 
    <p>Read the tutorial and download this script from <a href="https://www.onitroad.com/">onitroad</a>.</p>'; 
$mail->Body = $mailContent; 

//Send email 
if(!$mail->send()){ 
    echo 'Message could not be sent. Mailer Error: '.$mail->ErrorInfo; 
}else{ 
    echo 'Message has been sent.'; 
}
使用PHPMailer通过SMTP服务器发送电子邮件

从脚本发送电子邮件是Web应用程序中最常用的功能。
基本上,PHP Mail()函数用于从PHP脚本发送电子邮件。

使用PHP中使用邮件()函数发送电子邮件时,请从Web服务器发送邮件。
有时它可能会导致发送电子邮件的问题并无法将邮件发送给收件人。
使用SMTP,我们可以克服此问题,SMTP是从PHP脚本发送电子邮件的最推荐的方法。
通过SMTP发送电子邮件时,电子邮件将从邮件服务器发送而不是Web托管服务器发送。

使用SMTP在PHP中发送电子邮件的最简单方法是使用PHPMailer库。
PHPMailer提供了使用PHP通过SMTP服务器发送电子邮件的能力。
PHPMailer库的各种配置选项允许我们根据需求配置和自定义电子邮件发送功能。
我们可以使用PHPMailer发送单个或者多个附件的文本或者HTML电子邮件。
在本教程中,我们将展示如何使用PHPMailer在PHP中使用SMTP发送HTML电子邮件。

在示例脚本中,我们将在PHP中集成PHPMailer并使用PHPMailer库发送SMTP邮件。
使用以下示例代码使用PHP向HTML电子邮件发送HTML电子邮件。

使用附件发送HTML电子邮件

使用phpmailer类的addattachment()方法将附件添加到电子邮件中。
我们可以通过多次添加'addattachment()'方法将多个附件添加到电子邮件中。

//Add attachments
$mail->addAttachment('files/onitroad.pdf');
$mail->addAttachment('files/onitroad.docx');
$mail->addAttachment('images/onitroad.png', 'new-name.png'); //set new name

将电子邮件发送给多个收件人

添加addaddress()方法多次向多个收件人发送电子邮件。

//Add multiple recipients
$mail->addAddress('john.doe@gmail.com', 'John Doe');
$mail->addAddress('mark@example.com'); //name is optional

使用Gmail SMTP发送电子邮件

如果要使用Gmail SMTP发送电子邮件,则需要在Google帐户设置中进行一些更改。
按照以下步骤在PHPMailer库中使用Gmail SMTP。

  • 登录Google帐户。转到我的帐户页面。

  • 导航到安全页面并向下滚动到签名到Google部分»关闭2步验证

  • 向下滚动较少的安全应用程序访问部分,然后打开不太安全的应用程序访问。

现在,我们可以使用Gmail SMTP从PHP脚本发送电子邮件。

使用Gmail SMTP指定要发送电子邮件的Gmail帐户凭据(电子邮件地址和密码),SMTP主机和端口。

//SMTP configuration
$mail->isSMTP();
$mail->Host     = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'onitroad@gmail.com';
$mail->Password = '';
$mail->SMTPSecure = 'tls';
$mail->Port     = 587;
日期:2020-06-02 22:15:59 来源:oir作者:oir