本文作者:admin

Thinkphp5 Composer安装和压缩包安装 PHPMailer实现邮件发送

admin 2022-08-30 14:17:34
Thinkphp5 Composer安装和压缩包安装 PHPMailer实现邮件发送摘要: 方法一:...
电信鸢尾卡
类型:免费包邮
特点:39元260G支持结转黄金速率
联通黑牛卡
类型:免费包邮
特点:29元135G全国流量+100分钟
移动金香卡
类型:免费包邮
特点:29元155G首月免月租
流量卡营业厅
全网营业厅超市
免费包邮,应有尽有

方法一:

下载PHPMailer类包,放入ThinkPHP的Vendor目录,
官网下载地址:https://github.com/PHPMailer/PHPMailer/

Thinkphp框架extend文件夹中新建文件phpmailer,加入上面3个文件,

namespace phpmailer;

在application下创建extra目录,创建 email.php文件,内容如下:

return [
   ‘EMAIL_SMTP’=>’smtp.163.com’,
   ‘EMAIL_ADDRESS’=>’###@163.com’,
   ‘EMAIL_NICKNAME’=>’###网’,
   ‘EMAIL_LOGINNAME’=>”,
   ‘EMAIL_PASSWORD’=>”,
    ‘EMAIL_PORT’ => ’25’    //如果是阿里云,换成 465端口
];
在application下 common.php添加代码
最顶:
use phpmailer\PHPMailer;
方法: 
 function sendEmail($email, $content, $title){ 
 $mail = new PHPMailer(); //3.设置属性,告诉我们的服务器,谁跟谁发送邮件 
 $mail -> IsSMTP(); //告诉服务器使用smtp协议发送 
$mail->SMTPSecure = ‘ssl’;//设置使用ssl加密方式登录鉴权 
//阿里云 开启ssl 方式 端口:465
 $mail -> SMTPAuth = true; //开启SMTP授权 
 $mail -> Host = config(’email.EMAIL_SMTP’); //告诉我们的服务器使用163的smtp服务器发送 
 $mail -> From = config(’email.EMAIL_ADDRESS’); //发送者的邮件地址 
 $mail -> FromName = config(’email.EMAIL_NICKNAME’); //发送邮件的用户昵称 
 $mail -> Username = config(’email.EMAIL_LOGINNAME’); //登录到邮箱的用户名 
 $mail -> Password = config(’email.EMAIL_PASSWORD’); //第三方登录的授权码,在邮箱里面设置 //编辑发送的邮件内容 
 $mail -> Port = config(’email.EMAIL_PORT’); 
 $mail -> IsHTML(true); //发送的内容使用html编写 
 $mail -> CharSet = ‘utf-8’; //设置发送内容的编码 
 $mail -> Subject = $title;//设置邮件的标题 
 $mail -> MsgHTML($content); //发送的邮件内容主体 
 $mail -> AddAddress($email); //收人的邮件地址 //调用send方法,执行发送 
 $result = $mail -> Send(); 
 if($result){
 return true; 
 }
else{ return $mail -> ErrorInfo; } 
}
到这里基本完成了,不过有两个坑:
第一没打开extend目录,就是找不到PHPMailer ,查看一下项目index.php中的是否有:
define(‘EXTEND_PATH’, __DIR__ .’/extend/’);

放法二:

composer  require  phpmailer/phpmailer

然后在common.php里引入:

use PHPMailer\PHPMailer\PHPMailer;

/**

* @function    sendEmail

* @intro        发送邮件(带附件)

* @param $email     接收邮箱

* @param $title     邮件标题

* @param $from_name     发件人

* @param $content     邮件内容

* @param $attachmentFile     附件 (string | array)

* @return  array

*/

function sendEmail($email=”, $title=”, $from_name=”, $content=”, $attachmentFile=”){

date_default_timezone_set(‘PRC’);

//Create a new PHPMailer instance

$mail                       = new PHPMailer;

//Tell PHPMailer to use SMTP

$mail->isSMTP();

//Enable SMTP debugging

// 0 = off (for production use)

// 1 = client messages

// 2 = client and server messages

$mail->SMTPDebug            = 0;

//Ask for HTML-friendly debug output

$mail->Debugoutput          = ‘html’;

//charset

$mail->CharSet              = ‘UTF-8’;

//Set the hostname of the mail server

$mail->Host                 = “”;//请填写你的邮箱服务器

//Set the SMTP port number – likely to be 25, 465 or 587

$mail->Port                 = 25;//端口号

//Whether to use SMTP authentication

$mail->SMTPAuth             = true;

//Username to use for SMTP authentication

$mail->Username             = “”;//发件邮箱用户名

//Password to use for SMTP authentication

$mail->Password             = “”;//发件邮箱密码

//Set who the message is to be sent from

$mail->setFrom(‘发件邮箱用户名’, $from_name);

//Set an alternative reply-to address(用户直接回复邮件的地址)

$mail->addReplyTo(‘发件邮箱用户名’, $from_name);

//Set who the message is to be sent to

$mail->addAddress($email);

//Set the subject line

$mail->Subject              = $title;

//Read an HTML message body from an external file, convert referenced images to embedded,

//convert HTML into a basic plain-text alternative body

$mail->msgHTML($content);

//Replace the plain text body with one created manually

$mail->AltBody              = ”;

if(is_array($attachmentFile)){

for ($i=0; $i < count($attachmentFile); $i++) {

$mail->addAttachment($attachmentFile[$i],’Filename’.$i);//这里可以是多维数组,然后循环附件的文件和名称

}

}else{

if($attachmentFile !=”){

//Attach an image file

$mail->addAttachment($attachmentFile, ‘Filename’);

}

}

//send the message, check for errors

if (!$mail->send()) {

$status                 = 0;

$data                   = “邮件发送失败” . $mail->ErrorInfo;;

} else {

$status                 = 1;

$data                   = “邮件发送成功”;

}

return [‘status’=>$status,’data’=>$data];//返回值(可选)

}

在需要发邮件的地方调用这个方法:

sendEmail($email, ‘邮件标题’, ‘发件人简称’, $content, Env::get(‘root_path’).’public/uploads/file/’.$file, ‘file.jpg’);

正规流量卡免费办理,长按二维码进入

阅读
分享