search
HomeBackend DevelopmentPHP TutorialSimple use of PHP mail class library PHPMailer_PHP tutorial

最近需要用到发送邮件的功能,原本是用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="/static/imghwm/default1.png"  data-src="images/phpmailer.gif"  class="lazy"   style="max-width:90%" alt="Simple use of PHP mail class library PHPMailer_PHP tutorial" ></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!";
	}
}

使用的时候只要引入两个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
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software