发送方法

<?php

        $mail = new \App\Common\Lib\Mail\AliMail();

        list($res, $error) = 
                    $mail->setSubject('test email')
                    ->setHtmlBody('test body')
                    ->setToAdress([
                        'test@qq.com',
                    ])
                    ->sendMail();
        if ($res) {
            //发送成功
        } else {
            //发送失败
            var_dump($error);
        }

目录结构

公共类

<?php
/*
 * 阿里云邮件推送
 * 文档:https://help.aliyun.com/document_detail/29460.html?spm=a2c4g.11186623.6.610.243c3a35BSHQhe
 * git https://github.com/aliyun/openapi-sdk-php
 * region https://help.aliyun.com/document_detail/40654.html?spm=a2c4e.11153987.0.0.6d85366aW1vuZJ
 */
namespace App\Common\Lib\Mail;

include_once  APP_PATH . 'Common/Lib/Mail/aliyun-php-sdk-dm/aliyun-php-sdk-core/Config.php';

use Dm\Request\V20151123\SingleSendMailRequest;
use Think\Log;

class AliMail {
    //accessid
    protected $accessKeyId = '';
    //access secret
    protected $accessSecret = '';

    protected $client = null;
    //收件人
    protected $toAddress = null;
    //发件人邮箱,必须在阿里后台配好
    protected $accountName = '';
    //发件人昵称
    protected $fromAlias = '';

    protected $addressType = 1;
    //标签名
    protected $tagName = '';
    //是否需要回信
    protected $replyToAddress = 'false';
    //标题
    protected $subject = '';
    //内容
    protected $htmlBody = '';

    public function __construct()
    {

    }

    /*
     * 设置邮箱,可传单个字符串或者数组
     */
    public function setToAdress($mails = null){
        $this->toAddress = $mails;
        return $this;
    }

    /*
     * 设置标题
     */
    public function setSubject($subject = ''){
        $this->subject = $subject;
        return $this;
    }

    /*
     * 设置内容
     */
    public function setHtmlBody($htmlBody = ''){
        $this->htmlBody = $htmlBody;
        return $this;
    }
    /*
     * 设置标签,在阿里后台创建
     */
    public function setTagName($tagName = ''){
        $this->tagName = $tagName;
        return $this;
    }
    /*
     * 设置邮箱名称,可传单个字符串或者数组
     */
    public function setFromAlias($fromAlias = ''){
        $this->fromAlias = $fromAlias;
        return $this;
    }

    /*
     * 执行发送邮件
     */
    public function sendMail(){

        //检测发送
        list($res , $err) = $this->checkSend();

        if (!$res) {
            return [false,$err];
        }

        $request = new SingleSendMailRequest();

        $client = $this->getClient();

        //新加坡或澳洲region需要设置SDK的版本,华东1(杭州)不需要设置。
        //$request->setVersion("2017-06-22");

        $request->setAccountName($this->accountName);
        $request->setFromAlias($this->fromAlias);
        $request->setAddressType($this->addressType);
        $request->setTagName($this->tagName);
        $request->setReplyToAddress($this->replyToAddress);

        //判断多个或者单个邮件
        if (is_array($this->toAddress)) {

            foreach ($this->toAddress as $address) {
                $request->setToAddress($address);
            }

        } else {
            $request->setToAddress($this->toAddress);
        }

        //可以给多个收件人发送邮件,收件人之间用逗号分开,若调用模板批量发信建议使用BatchSendMailRequest方式
        //$request->setToAddress("邮箱1,邮箱2");
        $request->setSubject($this->subject);
        $request->setHtmlBody($this->htmlBody);

        try {
            //记录日志
            $log = [
                'toAddress' =>  $this->toAddress,
                'subject'   =>  $this->subject,
                'htmlBody'  =>  $this->htmlBody,
                'error'     =>  '发送成功!'
            ];
            Log::write("#### 发送邮件:".print_r($log,true));
            $response = $client->getAcsResponse($request);
            return [true,$response];
        }
        catch (\ClientException  $e) {
            //记录日志
            $log = [
                'toAddress' =>  $this->toAddress,
                'subject'   =>  $this->subject,
                'htmlBody'  =>  $this->htmlBody,
                'error' =>  $e->getErrorCode().''.$e->getErrorMessage()
            ];
            Log::write("#### 发送邮件:".print_r($log,true));
            return [false,$e->getErrorCode().''.$e->getErrorMessage()];
        }
        catch (\ServerException  $e) {
            //记录日志
            $log = [
                'toAddress' =>  $this->toAddress,
                'subject'   =>  $this->subject,
                'htmlBody'  =>  $this->htmlBody,
                'error' =>  $e->getErrorCode().''.$e->getErrorMessage()
            ];
            Log::write("#### 发送邮件:".print_r($log,true));
            return [false,$e->getErrorCode().''.$e->getErrorMessage()];
        }

    }

    /*
     * 获取发送客户端
     */
    public function getClient(){

        //新加坡或澳洲region需要设置服务器地址,华东1(杭州)不需要设置。
        //$iClientProfile::addEndpoint("ap-southeast-1","ap-southeast-1","Dm","dm.ap-southeast-1.aliyuncs.com");
        //$iClientProfile::addEndpoint("ap-southeast-2","ap-southeast-2","Dm","dm.ap-southeast-2.aliyuncs.com");

        $iClientProfile = \DefaultProfile::getProfile(
                        "cn-qingdao",
                                $this->accessKeyId,
                                $this->accessSecret
                            );
        $client = new \DefaultAcsClient($iClientProfile);
        $this->client = $client;
        return $client;
    }

    /*
     * 检测发送
     */
    protected function checkSend(){

        $res = false;

        if (empty($this->subject)) {
            return [$res,'请填写邮件标题'];
        }

        if (empty($this->htmlBody)) {
            return [$res,'请填写邮件内容'];
        }

        if (empty($this->toAddress)) {
            return [$res,'请填写收邮件地址'];
        }

        return [true];
    }

}

解决 laravel-admin between datetime 假如数据库是时间戳int类型无法筛选。

laravel-admin默认的between->datetime(),查询默认是datetime类型,但是假如数据库是时间戳类型就会报错,又不想改底层文件的话可以试试加自定义筛选功能...

阅读全文

php解析英文语句,自动分解。

参考:https://www.php.net/manual/en/function.str-split.php 最近碰到一个问题,客户的英文地址太长,超出接口api字段长度,所以需要解析下语句分解发送。 ...

阅读全文

记录一个laravel-excel导出表格值为0导出excel显示空的解决方法。

最近在使用laravel-excel导出表格的时候,发现假如字段值为0的情况下,导出的excel中直接显示为空,找到一个方法解决,如下. 在laravel-excel的config配置中...

阅读全文

欢迎留言