发送方法

<?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];
    }

}

php 向函数传引用

当你要传递的值特别大时,使用传引用的方式 会减小内存的开销。或者当你需要改变传递参数的原值时,也可以使用此方式。 <?php /** * 测试 */ class Tes...

阅读全文

升级php7.2后,加密函数mcrypt_module_open()被废弃的解决方案。

方案1 直接输入 pecl install mcrypt-snapshot 此方案的好处是不用改代码 方案2 <?php class Encrypt{ //base64_encode(openssl_random_pseudo_bytes...

阅读全文

elasticsearch+kibana+filebeat+headPlugin配置参考

参考链接 filebat: https://www.elastic.co/cn/downloads/beats/filebeat https://www.elastic.co/cn/products/beats/filebeat Elastic Stack : https://www...

阅读全文

欢迎留言