php 发送邮件 tp5,ThinkPHP5.1发送邮件(PHPMailer带附件)

用think PHP5.1发送邮件,为方便项目前后台都可调用,我这边写在了common.php里(application/common.php)

先切换到项目根目录,然后composer安装sdk,

composer  requirephpmailer/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');

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值