thinkphp5发送邮件

一、使用composer安装phpmailer

  composer require phpmailer/phpmailer

下载好之后,放在项目根目录下,然后需要再新建一个extend文件夹

在这里有必要说明一下:vendor目录与extend目录的区别:

1、vendor的是通过composer的方法进行自动引入到第三方扩展库vendor目录里的,

2、extend是通过手动的方法直接把第三方扩展库或者自己写的封装库直接引入到extend目录里,

二、封装邮件服务类

把这个邮件服务类封装在根目录下的extend文件夹下的custom文件夹下,命名为Mailer.php,其内容如下:

<?php
namespace custom;
use PHPMailer\PHPMailer\PHPMailer;

/**
 * 邮件服务类
 */

class Mailer extends PHPMailer{
	function __construct(){
		date_default_timezone_set('PRC');             			// 默认时区设置
 
		$this->CharSet = config('mail.charset');          		// 邮件编码设置
		$this->isSMTP();                      					// 启用SMTP服务
		$this->SMTPDebug = config('mail.smtp_debug');       	// Debug模式级别
		$this->Debugoutput = config('mail.debug_output');     // Debug输出类型
		$this->Host = config('mail.host');             			// SMTP服务器地址
		$this->Port = config('mail.port');             			// 端口号
		$this->SMTPAuth = config('mail.smtp_auth');        		// SMTP登录认证
		$this->SMTPSecure = config('mail.smtp_secure');      		// SMTP安全协议
		$this->Username = config('mail.username');         			// SMTP登录邮箱
		$this->Password = config('mail.password');         			// SMTP登录密码
		$this->setFrom(config('mail.from'), config('mail.from_name'));      // 发件人邮箱和名称
		$this->addReplyTo(config('mail.reply_to'), config('mail.reply_to_name')); // 回复邮箱和名称
	}
 
	/**
	 * 发送邮件
	 * @param [type] $toMail   收件人地址
     * @param [type] $toName   收件人名称
	 * @param [type] $subject   邮件主题
     * @param [type] $content   邮件内容,支持html
     * @param [type] $attachment 附件列表。文件路径或路径数组
     * @return [type]       成功返回true,失败返回错误消息
     */
	function sendMail($toMail, $toName, $subject, $content, $attachment = null){
		$this->addAddress($toMail, $toName);
		$this->Subject = $subject;
		$this->msgHTML($content);
		 
		if($attachment) { // 添加附件
			if(is_string($attachment)){
				is_file($attachment) && $this->AddAttachment($attachment);
			}else if(is_array($attachment)){
				foreach ($attachment as $file) {
					is_file($file) && $this->AddAttachment($file);
				}
			}   
		}
	 
		if(!$this->send()){ // 发送
		  return $this->ErrorInfo;
		}else{
		  return true;
		}
	}
}
?>

三、发送邮件

如何使用extend文件夹下的第三方类库呢?在入口文件index.php中定义一下extend的路径即可

为了灵活性,将mail的配置信息,写在配置文件中,方便后台动态修改

	
		/**************邮件配置信息***********/
		'mail'=>[
			'charset' => 'utf-8',         // 邮件编码
			'smtp_debug' => 0,           // Debug模式。0: 关闭,1: 客户端消息,2: 客户端和服务器消息,3: 2和连接状态,4: 更详细
			'debug_output' => 'html',       // Debug输出类型。`echo`(默认),`html`,或`error_log`
			'host' => 'smtp.qq.com',       // SMTP服务器地址
			'port' => 465,             // 端口号。默认25
			'smtp_auth' => true,          // 启用SMTP认证
			'smtp_secure' => 'ssl',        // 启用安全协议。''(默认),'ssl'或'tls',留空不启用
			'username' => '1459543371@qq.com', // SMTP登录邮箱
			'password' => 'tskxldzhaoahohibh',     // SMTP登录密码。qq邮箱使用客户端授权码,QQ邮箱用独立密码
			'from' => '1459543371@qq.com',     	// 发件人邮箱
			'from_name' => 'liwuming',         		// 发件人名称
			'reply_to' => '',           		// 回复邮箱的地址。留空取发件人邮箱
			'reply_to_name' => '',         		// 回复邮箱人名称。留空取发件人名称
		],

在这里需要说明一下:我用的是qq邮件服务

1)首要确定开启邮箱的POP3/SMTP服务,如何查看呢?

登录邮箱的个人中心-->【设置】-->【账户】-->【POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务】


2)因为是第三方登录邮件服务器,所以不能直接输入密码,在开启POP3/SMTP服务时,邮箱会自动分配一个动态密码,用这个密码才可以使得php连接上邮件服务器


现在所有工作以准备完毕,可以牛刀小试一下,看是否可以发送邮件了呢?

namespace app\Admin\controller;

use think\Db;
use think\Cache;
use custom\Mailer;

class Index extends Base{
		
	public function index(){
		//测试发送邮件
		$mail = new Mailer();
		
		$HTML = <<<HTML
	<table style='margin-left:280px;width:600px;height:300px;border-collapse:collapse;font-size: 12px;line-height: 24px;color: #333;font-family: Microsoft YaHei;'>
		<tr>
			<td  style='height:30px;line-height:30px;border-collapse:collapse;'>
				<img style='vertical-align:middle;' src='https://hwid1.vmall.com/CAS/up/logos/logoForUP.png'>
			</td>
		</tr>
		
		<tr>
			<td  style='height:25px;line-height:25px;'>
				1459543371 , 您好!
			</td>
		</tr>
		
		<tr>
			<td  style='height:25px;line-height:25px;'>
				为确保是您本人操作,请在邮件验证码输入框输入下方验证码:<br/>
				725087
			</td>
		</tr>
		
		<tr>
			<td  style='height:25px;line-height:25px;'>
				请勿向任何人泄露您收到的验证码。

			</td>
		</tr>
		
		<tr>
			<td  style='height:25px;line-height:25px;'>
				此致<br/>
				华为
			</td>
		</tr>
	</table>
HTML;
		
		$ok = $mail->sendMail('1459543371@qq.com', 'mingc', 'thinkphp5邮件测试',$HTML, 'd:/logo/xiaomi.jpg');
		var_dump($ok);
		
		return $this->fetch('index');
	}
}

登录到qq邮箱,果然收到了来自寡人发送给自己的邮件,看了之后,十分高兴,

因为这个邮件和华为发送的邮件几乎一模一样的,


我的大作,欢迎来踩 www.iis7.com/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值