php 发送邮件【示例场景:发送订单保险运单邮件给第三方平台】

<?php

namespace app\index\controller;

use think\Db;
use service\ApiReturn;
use service\EmailSend;

class TimedTask extends Base
{

    /**
     * Notes:发送邮件
     * @param $data
     * @param $user
     */
    public function index(){



        // 创建一个PHPExcel对象
        //excel数据
        $excelData = $_excelData = [];

        $condition = [] ;
        $condition[] = [ 'order.status' , '=' , '9' ] ;
        $condition[] = [ 'order.is_del' , '=' , '0' ] ;
        $data = Db::name('order')
            ->alias('order')
            ->leftJoin('order_address start_address','start_address.order_id=order.aid and start_address.type=1')
            ->leftJoin('order_address end_address','end_address.order_id=order.aid and end_address.type=3')
            ->where($condition)
            ->whereTime('order.end_time','yesterday')
            ->field('order.order_sn,order.order_type,order.order_source,order.user_id,order.user_name,order.userMobile,order.cityName,order.start_time,order.end_time,order.receive_time,order.create_time')
            ->field('start_address.name startaddress_name,start_address.subDetailAddress startaddress_detail')
            ->field('end_address.name endaddress_name,end_address.subDetailAddress endaddress_detail')
            ->select();
        foreach ($data as $key=>$value){
            $data[$key]['create_time'] = date('Y-m-d H:i:s',$value['create_time']) ;
            $data[$key]['receive_time'] = $value['receive_time'] ? date('Y-m-d H:i:s',$value['receive_time']) : '' ;
            $data[$key]['order_source'] = \app\common\model\Order::$order_source[$value['order_source']] ;
            $data[$key]['order_type'] = \app\common\model\Order::$orderTypes[$value['order_type']] ;
            $data[$key]['start_time'] = $value['start_time'] ? date('Y-m-d H:i:s',$value['start_time']) : '' ;
            $data[$key]['end_time'] = $value['end_time'] ? date('Y-m-d H:i:s',$value['end_time']) : '' ;
            $data[$key]['start_address'] = $value['startaddress_name'] . ' ' . $value['startaddress_detail'] ;
            $data[$key]['end_address'] = $value['endaddress_name'] . ' ' . $value['endaddress_detail'] ;
        }

        $date = date('n月d日',strtotime('yesterday')) ;
        $xlsName = "***{$date}货运险投保清单" ;//项目名称


        // 填充数据
        $indexFields = [
            ['order_sn', '订单号'],
            ['order_source', '订单来源'],
            ['user_name', '用户名称或ID'],
            ['userMobile', '联系电话'],
            ['order_type', '订单类型'],
            ['cityName', '服务城市'],
            ['start_address', '服务起始地址'],
            ['end_address', '服务结束地址'],
            ['create_time', '订单创建时间'],
            ['receive_time', '师傅接单时间'],
            ['start_time', '订单开始时间'],
            ['end_time', '订单结束时间'],

        ];

        //文件路径
        $filePath = '/www/wwwroot/***项目地址***/public/uploads/order_excel/'.$xlsName.'.xls';//此处填写文件地址的绝对路径

        $excelData = $data ;
        $_excelData[0]['list'] = $excelData;
        $excelData = array_values($_excelData);
        createExcel($xlsName,$indexFields,$excelData,$filePath);

        $title = "***{$date}货运险投保清单" ;//项目名称
        $res = EmailSend::sendEmail('***收件箱***',$title,$title,$filePath);

        var_dump($res);exit;
    }


}

调用发送邮件的方法↓↓↓

<?php
namespace service;

use think\Db;
// 发送邮件验证码
class EmailSend
{
    public static function sendEmail($user_email,$title,$content, $file='',$type = "163")
    {
        require_once "../extend/PHPMailer/class.phpmailer.php";

        switch ($type) {
            //163邮箱
            case "163":
                $Host = "smtp.163.com";
                $SMTPSecure = "ssl";
                $Port = "465";
                break;
            //腾讯企业邮箱
            case "exmail_qq":
                $Host = "smtp.exmail.qq.com";
                $SMTPSecure = "ssl";
                $Port = 465;
                break;
            //qq邮箱
            case "qq":
                $Host = "smtp.qq.com";
                $SMTPSecure = "ssl";
                $Port = 465;
                break;
            //阿里邮箱
            case "ali":
                $Host = "smtp.mxhichina.com";
                $SMTPSecure = "ssl";
                $Port = 465;
                break;
            default:
                $Host = "smtp.163.com";
                $SMTPSecure = "ssl";
                $Port = "465";

        }


        $mail = new \PHPMailer(); //实例化 
        $mail->IsSMTP(); // 启用SMTP
        $mail->SMTPDebug = 0;
        $mail->Host =   $Host; //SMTP服务器 以163邮箱为例子
        $mail->SMTPSecure = 'ssl';
        $mail->Port =  $Port;  //邮件发送端口
        $mail->SMTPAuth   = true;  //启用SMTP认证
        $mail->CharSet  = "UTF-8"; //字符集 
        $mail->Encoding = "base64"; //编码方式 

        //邮箱配置
        $email_config = Db::name('member_config')->where('name','email')->value('value');
        $email_config = json_decode($email_config,true);


        $mail->Username = $email_config['web_email'];  //你的邮箱
        $mail->Password = $email_config['web_email_pass'];  //你的密码
        $mail->Subject = $title; //邮件标题 
        $mail->From = $email_config['web_email'];  //发件人地址(也就是你的邮箱)
        $mail->FromName = "";  //发件人姓名
        $mail->AddAddress($user_email, "");//添加收件人(地址,昵称) 
        //$mail->AddAttachment($path,'投稿附件.'.$filetype);
        if($file){
            $excel = explode('.'.$file);
            $mail->AddAttachment($file,$excel[0]); // 添加附件,并指定名称
        }
        //$mail->AddAttachment('投稿附件.docx',$name); // 添加附件,并指定名称
        $mail->IsHTML(true); //支持html格式内容
        //$mail->AddEmbeddedImage("logo.jpg", "my-attach", "logo.jpg"); //设置邮件中的图片 
        $text = $content;
        $mail->Body = $text;
//        var_dump($mail);exit;

        //发送
        if(!$mail->Send()) {
            //echo "Mailer Error: " . $mail->ErrorInfo;
            return array('error' => $mail->ErrorInfo);
        }else{
            return true;
        }



        
    }

}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部