使用PHPMailer发送邮件实例代码总结
PHPMailer发送邮件现在php开发者比较常用的一个邮件发送组件了,利用它我们几乎不需要考虑任何问题,只要简单的把代码放网上把邮箱用户名密码与stmp改一下就可以发邮件了.
PHPMailer是别人封装好的一个发送邮件的库,用起来很方便,其支持mail、sendmail和smtp的方式可以到https://code.google.com/a/apache-extras.org/p/phpmailer/downloads/list去下载最新版本的,下面通过gmail smtp发送邮件为例来说明smtp使用方法.
<?php function sendMail($subject, $body, $to, $ccs = array()) { require_once './class.phpmailer.php'; $mail = new PHPMailer(); //设定邮件编码,默认ISO-8859-1,也可以直接去源代码中修改 $mail->CharSet = 'UTF-8'; // 使用smtp的方式发送 $mail->IsSMTP(); //smtp服务器需要认证 $mail->SMTPAuth = TRUE; //安全协议 gmail 是采用ssl的 $mail->SMTPSecure = "ssl"; //smtp服务器 $mail->Host = 'smtp.gmail.com'; //smtp服务器端口,普通是25 $mail->Port = 465; //smtp 认证用户名和密码 $mail->Username = 'yourgmailaccount@gmail.com'; $mail->Password = "yourpassword"; //发件人地址和名字,名字可以省略 $mail->SetFrom('yourgmailaccount@gmail.com', 'display name'); // 邮件标题 $mail->Subject = $subject; // 邮件内容,支持HTML格式 $mail->MsgHTML($body); // 收件人地址 $mail->AddAddress($to); // 抄送人 foreach ($ccs as $cc) { $mail->AddCC($cc); } if (!$mail->Send()) { echo "error info:" . $mail->ErrorInfo; } } ?>
上面是核心代码,下面我们综合一下实例,按如下示例编写代码即可实现php在线发送邮件.
一:前台表单,代码如下:
<html> <body> <h3 id="phpmailer-nbsp-Unit-nbsp-Test">phpmailer Unit Test</h3> 请你输入<font color="#FF6666">收信</font>的邮箱地址: <form name="phpmailer" action="send.php" method="post"> <input type="hidden" name="submitted" value="1"/> 邮箱地址: <input type="text" size="50" name="address" /> <br/> <input type="submit" value="发送"/> </form> </body> </html>
二:后台PHP程序,代码如下:
<?php require ("class.phpmailer.php"); //下载的文件必须放在该文件所在目录 $mail = new PHPMailer(); //建立邮件发送类 $address = $_POST['address']; $mail->IsSMTP(); // 使用SMTP方式发送 $mail->Host = "mail.xxxxx.com"; // 您的企业邮局域名 $mail->SMTPAuth = true; // 启用SMTP验证功能 $mail->Username = "user@xxxx.com"; // 邮局用户名(请填写完整的email地址) $mail->Password = "******"; // 邮局密码 $mail->From = "user@xxxx.com"; //邮件发送者email地址 $mail->FromName = "您的名称"; $mail->AddAddress("$address", ""); //收件人地址,可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名") //$mail->AddReplyTo("", ""); //$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件 //$mail->IsHTML(true); // set email format to HTML //是否使用HTML格式 $mail->Subject = "PHPMailer测试邮件"; //邮件标题 $mail->Body = "Hello,这是测试邮件"; //邮件内容 $mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //附加信息,可以省略 if (!$mail->Send()) { echo "邮件发送失败. <p>"; echo "错误原因: " . $mail->ErrorInfo; exit; } echo "邮件发送成功"; ?>
永久地址:
转载随意~请带上教程地址吧^^

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

AI Hentai Generator
Generate AI Hentai for free.

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 use Flask-Mail to send emails With the development of the Internet, email has become an important tool for people to communicate. When developing web applications, sometimes we need to send emails in specific scenarios, such as sending a welcome email after a user successfully registers, or sending a password reset email when a user forgets their password, etc. Flask is a simple and flexible Python Web framework, and Flask-Mail is an extension library for sending emails under the Flask framework. This article will introduce how to

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.

Python connects to the Alibaba Cloud interface to implement the email sending function. Alibaba Cloud provides a series of service interfaces, including email sending services. By connecting to the Alibaba Cloud interface through a Python script, we can quickly send emails. This article will show you how to use Python scripts to connect to the Alibaba Cloud interface and implement the email sending function. First, we need to apply for the email sending service on Alibaba Cloud and obtain the corresponding interface information. In the Alibaba Cloud Management Console, select the email push service and create a new email
