PHPMailer发送HTML内容、带附件的邮件实例
这篇文章主要介绍了PHPMailer发送HTML内容、带附件的邮件实例,发送的内容包含图片和文字,附件则发送的一个EXCEL表,需要的朋友可以参考下
PHPMailer是一个封装好的PHP邮件发送类,支持发送HTML内容的电子邮件,以及可以添加附件发送,并不像PHP本身mail()函数需要服务器环境支持,您只需要设置邮件服务器以相关信息就能实现邮件发送功能。
本文将结合实例代码,讲解如何设置和实现发送HTML以及带附件的邮件功能。
首先,您可以到PHPMailer获取最新的下载包,解压到WEB目录下。
然后建立一个sendmail.php的文件,载入PHPMailer类,并设置相关属性参数,如邮件服务器地址,发件人和收件人,邮件内容等等,详情请看代码:
复制代码 代码如下:
require_once('class.phpmailer.php'); //载入PHPMailer类
$mail = new PHPMailer(); //实例化
$mail->IsSMTP(); // 启用SMTP
$mail->Host = "smtp.163.com"; //SMTP服务器 以163邮箱为例子
$mail->Port = 25; //邮件发送端口
$mail->SMTPAuth = true; //启用SMTP认证
$mail->CharSet = "UTF-8"; //字符集
$mail->Encoding = "base64"; //编码方式
$mail->Username = "helloweba@163.com"; //你的邮箱
$mail->Password = "xxx"; //你的密码
$mail->Subject = "你好"; //邮件标题
$mail->From = "helloweba@163.com"; //发件人地址(也就是你的邮箱)
$mail->FromName = "月光光"; //发件人姓名
$address = "xyz@163.com";//收件人email
$mail->AddAddress($address, "亲");//添加收件人(地址,昵称)
$mail->AddAttachment('xx.xls','我的附件.xls'); // 添加附件,并指定名称
$mail->IsHTML(true); //支持html格式内容
$mail->AddEmbeddedImage("logo.jpg", "my-attach", "logo.jpg"); //设置邮件中的图片
$mail->Body = '你好, 朋友!
这是一封来自
target="_blank">jb51.net的邮件!
'; //邮件主体内容
//发送
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
从代码中可以看出,实例化PHPMailer后,我们指定使用SMTP方式来发邮件,设置SMTP邮件服务器,,并启用SMTP认证,如果您的邮件服务器不需要认证,则设置$mail->SMTPAuth=false,并且不需要密码就可以发送。然后设置字符集和编码支持中文字符,注意原版的PHPMailer包对中文字符的支持不太理想,所以您可以下载helloweba示例中的改进包。然后设置发件人和收件人,添加附件。注意附件原名最好不要用中文,可以在AddAttachment()指定中文名称。然后设置邮件html内容,最后就是发送,流程一目了然,
如果发送成功,将会收到如下邮件:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In web applications, it is often necessary to send emails to multiple recipients at once. PHP is a very popular web development language, and PHPMailer is a common PHP class library for sending emails. PHPMailer provides a rich interface, making sending emails in PHP applications more convenient and easy to use. In this article, we will introduce the methods and steps on how to use PHPMailer to send emails to multiple recipients. To download PHPMailer, you first need to go to the official website (

PHP development practice: Use PHPMailer to send emails to users in the MySQL database Introduction: In the construction of the modern Internet, email is an important communication tool. Whether it is user registration, password reset, or order confirmation in e-commerce, sending emails is an essential function. This article will introduce how to use PHPMailer to send emails and save the email information to the user information table in the MySQL database. 1. Install the PHPMailer library PHPMailer is

How to send HTML mail with embedded images using PHP and PHPMAILER? HTML email is a richer and more personalized form of email that can insert pictures, links and styles into the email. Embedded images refer to sending images directly as part of the email in the HTML email instead of sending them as attachments. In PHP, we can use PHPMAILER to send HTML emails with embedded images. PHPMAILER is a powerful PHP email sending library

PHP and PHPMAILER: How to implement anti-spam function for email sending? Introduction: In the Internet age, email has become an indispensable part of our daily life and work. However, with the popularity and use of email, the problem of spam has become increasingly serious, causing a lot of trouble to users. In order to solve this problem, this article will introduce how to use PHP and PHPMailer library to implement the anti-spam function of email sending. 1. Understand spam. Spam refers to those unsolicited

Mastering PHP and PHPMAILER: How to implement the automatic reply function for email sending? In modern society, email has become one of the important ways for people to communicate every day. Many websites or companies need to communicate with users through emails, and automatic reply to emails has become very important. This article will introduce how to use PHP and the PHPMailer library to implement the automatic reply function for email sending. Step 1: Get the user’s email information First, we need to get the user’s email information. On a website or application, use

PHP methods and precautions for sending attachment emails using the PHPMailer library. Email has become a very important way of communication in modern life. In many development projects, we need to use code to automatically send emails. At this time, the PHPMailer library is our best choice. PHPMailer is a library dedicated to sending emails in PHP. It can easily send emails, including HTML-formatted emails and attachments. This article will focus on how to send emails with attachments in the PHPMailer library.

CakePHP is a PHP open source framework based on the MVC model, designed to provide developers with an efficient, scalable, and easy-to-maintain Web application development environment. Among them, the mail function has always been one of the important components of Web applications. In order to facilitate developers to use the mail function, the PHPMailer class library has been encapsulated in CakePHP. PHPMailer is a commonly used email sending library that supports functions such as sending HTML emails, attachments, carbon copy, mail queue, and SMTP verification. Book

How to send HTML emails with images using PHP and PHPMailer? Email plays an important role in modern communication, but sending HTML emails with images may confuse some PHP developers. In this article, we will introduce how to use PHP and PHPMailer to send HTML emails with images. We'll provide code examples to help you better understand how to achieve this. First, we need to make sure that the PHPMailer library is installed in our project
