杜郎俊赏

建站之阿里云邮件推送

评论被回复时,如果评论者填了邮箱,则向其发送通知邮件。使用阿里云的邮件推送。每日免费额度 200 封。

/** * 发送阿里云邮件 */class Util_Mail{ private static $url, $accessId, $accessSecret, $account, $name; public static function send($title, $content, $receiver = null) { if (empty($title) || empty($content)) { return false; } self::init(); if (is_null($receiver)) { $receiver = Common::getAdminMail(); } elseif (!filter_var($receiver, FILTER_VALIDATE_EMAIL)) { return false; } //计算签名 $encode = function ($string) { return str_replace(['+', '*', '%7E'], ['%20', '%2A', '~'], urlencode($string)); }; //阿里云dm $data = [ 'Format' => 'JSON', 'Version' => '2015-11-23', 'AccessKeyId' => self::$accessId, 'SignatureMethod' => 'HMAC-SHA1', 'Timestamp' => date('Y-m-d\TH:i:s\Z', time() - date('Z')), //utc 时间 'SignatureVersion' => '1.0', 'SignatureNonce' => uniqid(), 'Action' => 'SingleSendMail', 'AccountName' => self::$account, 'ReplyToAddress' => 'false', 'AddressType' => 0, 'ToAddress' => $receiver, 'FromAlias' => self::$name, 'Subject' => $title, 'HtmlBody' => $content, ]; ksort($data); $signature = ''; foreach ($data as $k => $v) { $signature .= '&' . $encode($k) . '=' . $encode($v); } $signature = 'POST&%2F&' . $encode(substr($signature, 1)); $data['Signature'] = base64_encode(hash_hmac('sha1', $signature, self::$accessSecret . '&', true)); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, self::$url); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_exec($ch); } private static function init() { if (empty(self::$url)) { $config = Yaf\Application::app()->getConfig()->get('dm'); self::$url = $config->get('url'); self::$accessId = $config->get('access_id'); self::$accessSecret = $config->get('access_secret'); self::$account = $config->get('account'); self::$name = $config->get('name'); } }}

标签:建站
日期:2017-05-18