Home Backend Development PHP Tutorial Simple use of PHP mail class library PHPMailer_PHP tutorial

Simple use of PHP mail class library PHPMailer_PHP tutorial

Jul 13, 2016 am 10:34 AM
phpmailer mail

最近需要用到发送邮件的功能,原本是用PHP自带的mail()函数发送的。php mail()这个方法非常简单、方便、易用,但是除了网易邮箱、QQ邮箱、GMAIL邮箱等常用的邮箱可以收到之外,经测试HOTMAIL、TOM、LIVE等邮箱是收不到此类邮件的。所以就转而使用PHPMailer这个强大的邮件发送类。

使用官方自带的一些例子,有些会报 Mailer Error: Could not instantiate mail function. 这个错误。参考了一些资料之后,还是自己写了一个方法。代码很简单,就不多解释了。

function mailto($nickname, $address, $id, $activation_code)
{
	date_default_timezone_set('PRC'); 
	include_once("class.phpmailer.php");
	
	$mail = new PHPMailer(); // defaults to using php "mail()"
	$mail->IsSMTP();
	$mail->Host = "smtp.163.com";			// SMTP 服务器  
	$mail->SMTPAuth = true;            		// 打开SMTP 认证  
	$mail->Username = "bkjia@163.com"; 	// 用户名
	$mail->Password = "yourpassword";          // 密码  
	
	//$body = file_get_contents('application/views/nmra/register.html');
	//$body = preg_replace('/\\\\/','', $body); //Strip backslashes
	$body = '<p><body style="margin: 10px;"></p>';
	$body .= '<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 14px; ">';
	$body .= '<div align="center"><img src="images/phpmailer.gif" style="height: 90px; width: 340px"></div>';
	$body .= '<p>'.$nickname.',您好。</p>';
	$body .= '<p>恭喜你成为帮客之家研究协会的第'.$id.'名会员。</p>';
	$body .= '<p>帮客之家社区(bangke Research Association)是一个程序猿、攻城狮、设计狮和开发者们技术交流、话题讨论的社区。希望在这里你能找到感兴趣的话题与志同道合的朋友。</p>';
	$body .= '请点击以下链接验证您的邮箱,请注意域名为bkjia.com:<a href="http://www.bkjia.com/librarys/accounts/activation/?code="'.$activation_code.'" target="_blank">http://www.bkjia.com/librarys/accounts/activation/?code='.$activation_code.'</a>';
	$body .= '<p>顺祝工作学习愉快,生活舒心。</p>';
	$body .= '</div></body>';
	//echo $body;

	$mail->AddReplyTo("bkjia@163.com","Gonn");
	$mail->SetFrom('bkjia@163.com', 'Gonn');
	$mail->AddReplyTo("bkjia@163.com","Gonn");
	$address = "252211974@qq.com";
	//$address = "bkjia@gmail.com";
	$mail->AddAddress($address, $nickname);
	
	$subject = "收到来自帮客之家的邮件";
	$mail->Subject = "=?UTF-8?B?".base64_encode($subject)."?=";
	// optional, comment out and test
	$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; 
	$mail->MsgHTML($body);
	
	//$mail->AddAttachment("images/phpmailer.gif");      // attachment
	//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
	
	if(!$mail->Send()) {
		//echo "Mailer Error: " . $mail->ErrorInfo;
	} 
	else {
		//echo "Message sent!";
	}
}
Copy after login

使用的时候只要引入两个PHP类,然后自己写个方法就OK了,两个类很小,发送邮件速度也很快。

PHPMailer 是一个功能强大的邮件类,其主要功能特点:

  1. 支持邮件 s/mime加密的数字签名
  2. 支持邮件多个 TOs, CCs, BCCs and REPLY-TOs
  3. 可以工作在任何服务器平台,所以不用担心WIN平台无法发送邮件的问题的
  4. 支持文本/HTML格式邮件
  5. 可以嵌入image图像
  6. 对于邮件客户端不支持HTML阅读的进行支持
  7. 功能强大的发送邮件调试功能debug
  8. 自定义邮件header
  9. 冗余SMTP服务器支持
  10. 支持8bit, base64, binary, and quoted-printable 编码
  11. 文字自动换行
  12. 支持多附件发送功能
  13. 支持SMTP服务器验证功能
  14. 在Sendmail, qmail, Postfix, Gmail, Imail, Exchange 等平台测试成功
  15. 提供的下载文件中,包括内容详细的说明文档及示例说明,所以不用担心难于上手的问题!
  16. PHPMailer 非常小巧、简单、方便、快捷

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/752342.htmlTechArticle最近需要用到发送邮件的功能,原本是用PHP自带的mail()函数发送的。php mail()这个方法非常简单、方便、易用,但是除了网易邮箱、QQ邮箱、...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Outlook emails lost from control panel in Windows 11 Outlook emails lost from control panel in Windows 11 Feb 29, 2024 pm 03:16 PM

Is the Outlook mail icon missing from Windows 11's Control Panel? This unexpected situation has caused confusion and concern among some individuals who rely on OutlookMail for their communication needs. Why don't my Outlook emails show up in Control Panel? There may be several possible reasons why there are no Outlook mail icons in Control Panel: Outlook is not installed correctly. Installing Office applications from the Microsoft Store does not add the Mail applet to Control Panel. The location of the mlcfg32.cpl file in Control Panel is missing. The path to the mlcfg32.cpl file in the registry is incorrect. The operating system is not currently configured to run this application

Word mail merge prints blank page Word mail merge prints blank page Feb 19, 2024 pm 04:51 PM

If you find that blank pages appear when printing a mail merge document using Word, this article will help you. Mail merge is a convenient feature that allows you to easily create personalized documents and send them to multiple recipients. In Microsoft Word, the mail merge feature is highly regarded because it helps users save time manually copying the same content for each recipient. In order to print the mail merge document, you can go to the Mailings tab. But some Word users have reported that when trying to print a mail merge document, the printer prints a blank page or doesn't print at all. This may be due to incorrect formatting or printer settings. Try checking the document and printer settings and make sure to preview the document before printing to ensure the content is correct. if

PHP sends emails asynchronously: avoid long waits for emails to be sent. PHP sends emails asynchronously: avoid long waits for emails to be sent. Sep 19, 2023 am 09:10 AM

PHP sends emails asynchronously: avoid long waits for emails to be sent. Introduction: In web development, sending emails is one of the common functions. However, since sending emails requires communication with the server, it often causes users to wait for a long time while waiting for the email to be sent. In order to solve this problem, we can use PHP to send emails asynchronously to optimize the user experience. This article will introduce how to implement PHP to send emails asynchronously through specific code examples and avoid long waits. 1. Understanding sending emails asynchronously

PHP methods and steps for sending emails to multiple people using PHPMailer PHP methods and steps for sending emails to multiple people using PHPMailer May 22, 2023 pm 06:10 PM

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 email tracking function: understand user behavior and feedback on emails. PHP email tracking function: understand user behavior and feedback on emails. Sep 19, 2023 am 08:51 AM

PHP email tracking function: understand user behavior and feedback on emails In modern society, email has become an indispensable part of people's daily life and work. For businesses, sending emails is one of the important ways to communicate with customers and promote products or services. However, after an email is sent, how do we know whether it was received, read, or how the user reacted to the content of the email? At this time, the email tracking function becomes particularly important. The email tracking function can help us understand user behavior and feedback on emails

PHP development practice: Use PHPMailer to send emails to users in the MySQL database PHP development practice: Use PHPMailer to send emails to users in the MySQL database Aug 05, 2023 pm 06:21 PM

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? How to send HTML mail with embedded images using PHP and PHPMAILER? Jul 22, 2023 am 11:29 AM

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? PHP and PHPMAILER: How to implement anti-spam function for email sending? Jul 22, 2023 am 11:46 AM

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

See all articles