MENU

【PHP】阿里云邮箱推送-单邮件发送函数

January 17, 2020 • Read: 105 • 程序源码

阿里云【邮件推送】是有官方的SDK的,拿来也能直接用,但某些时候我仅仅需要其中的邮件发送,好吧;这玩意就是从官方SDK中提出来的,方便扔到各种地方。

PHP > 7.0,使用方法:配置号 $opt 参数扔进去就行...

function sendMailByAli($to, $subject, $content, $opt = []): array
{
    //     [
    //     'id'      => 'LTAI4FwDaXbQXQ4dfCzZnfuu',
    //     'secret'  => 'UYMC8zKTUQ5kLBDcVLOvqlv1tHhlFf',
    //     'account' => 'report@qzone.club',
    //     'alias'   => 'QQ空间俱乐部',
    //      ]

    $id            = $opt['id'];
    $secret        = $opt['secret'];
    $account       = $opt['account']; //发信地址
    $apiUrl        = 'https://dm.aliyuncs.com/';
    $percentEncode = function ($str) {
        $res = urlencode($str);
        $res = preg_replace('/\+/', '%20', $res);
        $res = preg_replace('/\*/', '%2A', $res);
        $res = preg_replace('/%7E/', '~', $res);
        return $res;
    };
    $toSign        = function ($paras, $secret) use ($percentEncode) {
        ksort($paras);
        $queryStr = '';
        foreach ($paras as $key => $value) {
            $queryStr .= '&' . $percentEncode($key) . '=' . $percentEncode($value);
        }
        $signStr = 'POST' . '&%2F&' . $percentEncode(substr($queryStr, 1));

        return base64_encode(hash_hmac('sha1', $signStr, $secret . "&", true));
    };

    $paras = [
        'Format'           => 'JSON',
        'Version'          => '2015-11-23',
        'AccessKeyId'      => $id,
        'SignatureMethod'  => 'HMAC-SHA1',
        'Timestamp'        => gmdate('Y-m-d\TH:i:s\Z'),
        'SignatureVersion' => '1.0',
        'SignatureNonce'   => microtime(true),
        'AccountName'      => $account,
        'AddressType'      => 1,
        'ReplyToAddress'   => 'true',
        'TextBody'         => $content,
        'Subject'          => $subject,
        'ToAddress'        => $to,
        'Action'           => 'SingleSendMail',
        'ClickTrace'       => 1,
        'FromAlias'        => 'QQ空间俱乐部',
    ];

    $paras['Signature'] = $toSign($paras, $secret);

    return getCurl($apiUrl, ['post' => $paras, 'detail' => true]);
}


function getCurl($url, $opt = [])
{
    $cookie = '';
    if (is_array($opt['cookie'])) {
        foreach ($opt['cookie'] as $k => $v) {
            $cookie .= $k . '=' . $v . '; ';
        }
    }

    $cookie = (mb_substr($cookie, 0, mb_strlen($cookie) - 2));

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_NOBODY, $opt['nobody']);
    curl_setopt($ch, CURLOPT_HEADER, $opt['header'] ?? false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $opt['headers'] ?? []);
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, $opt['rtime'] ?? 10000);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $opt['ctime'] ?? 10000);
    curl_setopt($ch, CURLOPT_REFERER, $opt['refer'] ?? 'https://user.qzone.qq.com/');
    curl_setopt($ch, CURLOPT_USERAGENT, $opt['UA'] ?? "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");

    if (isset($opt['post']) && is_array($opt['post'])) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($opt['post']));
    }
    if (isset($opt['proxy']) && is_array($opt['proxy'])) {
        curl_setopt($ch, CURLOPT_PROXY, $opt['proxy']['ip']);
        curl_setopt($ch, CURLOPT_PROXYPORT, $opt['proxy']['port']);
    }
    $res   = curl_exec($ch);
    $error = curl_error($ch);
    $code  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($opt['detail']) {
        return ['code' => $code, 'error' => $error, 'response' => $res,];
    }

    return $res;
}
Last Modified: June 13, 2020