Thinkphp5+PHPMailer实现发送邮件

PHP发送邮件需要开启下面几个服务:

SMTP:简单邮件传输协议,用来发送邮件

POP3:简单的邮件访问协议,单向的,本地对邮件的操作不会同步到邮件服务器,用来接收邮件

IMAP:是一种比POP3更复杂的邮件访问协议,是双向的,本地对邮箱的操作会同步到邮箱服务器,用来接收邮件

发送之前需要拥有自己的邮件服务器,测试的时候其实用自己申请的免费邮箱就行,不需要自己搭建服务器了,可能要配置邮箱的SMTP服务,大部分公共邮箱(163、qq等)为了安全默认是关闭的,需要手动开启。

网易邮箱配置

QQ邮箱配置

基本的介绍完之后,开始说怎么用(以QQ邮箱为例)。

一、开启服务

登陆QQ邮箱,顶部有设置按钮,点击账户,拉到最后,开启第一行的 POP3/SMTP 服务,开启之后会有一串密码,复制下来一会要用。

二、下载安装PHPMailer

1、从 github 上下载: https://github.com/PHPMailer/PHPMailer/

2、使用 composer 安装:

  composer require phpmailer/phpmailer

如果只是发送邮件,只留phpmailer.php和smtp.php就行。在vendor文件夹下新建phpmailer文件夹,把下载的文件放到这个文件夹下,打开composer.json文件,在autoload下加入一行代码 phpmailer 是命名空间名,vendor/phpmailer是对应的文件名

"autoload": {
	"psr-4": {
		"app\\": "application",
		"phpmailer\\": "vendor/phpmailer"
	}
},

分别打开PHPMailer.php和SMTP.php,修改其命名空间,为 “ namespace phpmailer; ”,两个文件都要修改,要不然会找不到文件

打开cmd,进入到项目根目录,使用composer命令加载第三方类库,执行composer dump-autoload

三、控制器代码
<?php
    namespace app\index\controller;
    use think\Controller;
    use phpmailer\PHPMailer;
    use phpmailer\Exception;
    
    class Sendmail extends Controller
    {
	    // 
	    public function index()
	    {
		    $toemail = '*****@126.com';//这里写的是收件人的邮箱
		    $mail=new Phpmailer();
		    $mail->isSMTP();// 使用SMTP服务(发送邮件的服务)
		    $mail->CharSet = "utf8";// 编码格式为utf8,不设置编码的话,中文会出现乱码
		    $mail->Host = "smtp.qq.com";// 发送方的SMTP服务器地址
		    $mail->SMTPAuth = true;// 是否使用身份验证
		    $mail->Username = "12*****186@qq.com";// 申请了smtp服务的邮箱名(自己的邮箱名)
		    $mail->Password = "hcstaffeplbcjgii";// 发送方的邮箱密码,不是登录密码,是qq的第三方授权登录码,要自己去开启(之前叫你保存的那个密码)
		    $mail->SMTPSecure = "ssl";// 使用ssl协议方式,
		    $mail->Port = 465;// QQ邮箱的ssl协议方式端口号是465/587
		    $mail->setFrom("12*****186@qq.com","测试发件人");// 设置发件人信息,如邮件格式说明中的发件人,
		    $mail->addAddress($toemail,'测试收件人');// 设置收件人信息,如邮件格式说明中的收件人
		    $mail->addReplyTo("12*****186@qq.com","Reply");// 设置回复人信息,指的是收件人收到邮件后,如果要回复,回复邮件将发送到的邮箱地址
		    //$mail->addCC("xxx@163.com");// 设置邮件抄送人,可以只写地址,上述的设置也可以只写地址(这个人也能收到邮件)
		    //$mail->addBCC("xxx@163.com");// 设置秘密抄送人(这个人也能收到邮件)
		    //$mail->addAttachment("bug0.jpg");// 添加附件
		    $mail->Subject = "这是一个测试邮件";// 邮件标题
		    $mail->Body = '<h1>这里是邮件内容</h1>' . date('Y-m-d H:i:s');// 邮件正文
		    //$mail->AltBody = "This is the plain text纯文本";// 这个是设置纯文本方式显示的正文内容,如果不支持Html方式,就会用到这个,基本无用**
		       if(!$mail->send()){// 发送邮件
		       echo "Message could not be sent.";
		       echo "Mailer Error: ".$mail->ErrorInfo;// 输出错误信息
		    }else{
		    echo '';
		    return '发送成功';
		    }
	    }
    
    }
四、运行

sendmail.bat

@echo off
	start http://网址/sendmail.php
exit
五、运行结果

发件人邮箱

收件人邮箱

//网址/sendmail.php
exit

五、运行结果

发件人邮箱

在这里插入图片描述

收件人邮箱

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
class PHPMailer { ///////////////////////////////////////////////// // PROPERTIES, PUBLIC ///////////////////////////////////////////////// /** * Email priority (1 = High, 3 = Normal, 5 = low). * @var int */ public $Priority = 3; /** * Sets the CharSet of the message. * @var string */ public $CharSet = 'iso-8859-1'; /** * Sets the Content-type of the message. * @var string */ public $ContentType = 'text/plain'; /** * Sets the Encoding of the message. Options for this are * "8bit", "7bit", "binary", "base64", and "quoted-printable". * @var string */ public $Encoding = '8bit'; /** * Holds the most recent mailer error message. * @var string */ public $ErrorInfo = ''; /** * Sets the From email address for the message. * @var string */ public $From = 'root@localhost'; /** * Sets the From name of the message. * @var string */ public $FromName = 'Root User'; /** * Sets the Sender email (Return-Path) of the message. If not empty, * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode. * @var string */ public $Sender = ''; /** * Sets the Subject of the message. * @var string */ public $Subject = ''; /** * Sets the Body of the message. This can be either an HTML or text body. * If HTML then run IsHTML(true). * @var string */ public $Body = ''; /** * Sets the text-only body of the message. This automatically sets the * email to multipart/alternative. This body can be read by mail * clients that do not have HTML email capability such as mutt. Clients * that can read HTML will view the normal Body. * @var string */ public $AltBody = ''; /** * Sets word wrapping on the body of the message to a given number of * characters. * @var int */ public $WordWrap = 0; /** * Method to send mail: ("mail", "sendmail", or "smtp"). * @var string */ public $Mailer = 'mail'; /** * Sets the path of the sendmail program. * @var string */ public $Sendmail = '/usr/sbin/sendmail'; /** * Path to PHPMailer plugins. Useful if the SMTP class * is in a different directory than the PHP include path. * @var string */ public $PluginDir = ''; /** * Sets the email address that a reading confirmation will be sent. * @var string */ 一个可以收发邮件的工具类

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

下页、再停留

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值