使用PHPMailer封装类发送邮件

本文已在本人博客https://www.nsxsg.com/archives/52首发

使用PHPMailer封装类发送邮件

近期,打算做一个邮箱注册,使用的是smtp服务。可是奇怪的是在本地调试是好的,一搬到阿里云的服务器上就不行了。想了好久没有答案,所以提了一下工单给阿里云,以下是阿里云的回复。

由于国际与国内均对垃圾邮件进行严格管控,我国《互联网信息服务管理办法》、《中国互联网协会反垃圾邮件规范》均对垃圾邮件进行说明与管理规范。鉴于服务器25端口被大量垃圾邮件充斥,严重影响广大用户正常使用。为了共同维护良好的网络环境,自即日起阿里云新购服务器不再提供25端口邮件服务,建议您尝试使用465加密端口发送邮件,或与邮件发信提供商咨询是否还有其他smtp发信端口。

好吧,原来是25端口不能用了。改呗,换端口,可是换完之后本地也不能用了。天哪,这是怎么回事。想了一想,原本是好的,但是换了端口就不行了,那么肯定是这个邮件发送类不支持其它端口。马上换个邮件发送类,在GitHub搜索PHPMailer, 一堆英文看不懂,尴尬了。好在看到了一个A Simple Example,行,照着改呗。下面就贴出这个简单的例子及我的一些中文注释。

<?php
	// Import PHPMailer classes into the global namespace
	// These must be at the top of your script, not inside a function
	// use PHPMailer\PHPMailer\PHPMailer;
	// use PHPMailer\PHPMailer\Exception;

	//Load composer’s autoloader
	// require ‘vendor/autoload.php’;
	//导入文件,文件内容自己去GitHub下载,地址:https://github.com/PHPMailer/PHPMailer
    require './PHPMailer.php';
    require './SMTP.php';


	mail = new PHPMailer(true);                              // Passing true enables exceptions
	try {
	    //Server settings(服务器设置)
	    mail->SMTPDebug = 2;                                 // Enable verbose debug output(启用详细调试输出)
	    mail->isSMTP();                                      // Set mailer to use SMTP(设置使用SMTP服务)
	    mail->Host = 'smtp.163.com';                         // Specify main and backup SMTP servers(SMTP服务器)
	    mail->SMTPAuth = true;                               // Enable SMTP authentication(启用SMTP验证)
	    mail->Username = '[email protected]';                    // SMTP username
	    mail->Password = 'abcde123456';                      // SMTP password
	    mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, ssl also accepted(启用TLS加密,ssl也被接受)
	    $mail->Port = 465;                                    // TCP port to connect to(要连接的TCP端口)


		//Recipients(收件人)
		$mail->setFrom('[email protected]', 'Mailer');            //Mailer随便写一个名字
		$mail->addAddress('[email protected]', 'Joe User');       // Add a recipient(添加一个收件人)
		$mail->addAddress('[email protected]');               // Name is optional(再添加一个收件人,其中Joe User及名字可以不写)
		$mail->addReplyTo('[email protected]', 'Information'); //抄送
		// $mail->addCC('[email protected]');
		// $mail->addBCC('[email protected]');
		
		//Attachments(附件)
		$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments(添加一个文件在服务器上的完整路径)
		// $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
		
		//Content(邮件内容)
		$mail->isHTML(true);                                  // Set email format to HTML
		$mail->Subject = 'Here is the subject';               // 邮件标题
		$mail->Body    = 'This is the HTML message body <b>in bold!</b>';               //邮件内容(可以使用html标签)
		// $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
		
		$mail->send();
		echo 'Message has been sent';
	} catch (Exception e) {
	    echo 'Message could not be sent.';
	    // echo 'Mailer Error: ' . mail->ErrorInfo;          // 具体报错信息,基本不用,歪果仁写的就是好,一用就成功
	}

各位大神,如果有好的见解,记得告诉我,让我完善完善。

猜你喜欢

转载自blog.csdn.net/qq_39014761/article/details/85156967
今日推荐