Skip to content

Commit

Permalink
add email comment support
Browse files Browse the repository at this point in the history
  • Loading branch information
morizunzhu committed Jul 6, 2020
1 parent 92c6850 commit 0d38683
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mail_key
168 changes: 165 additions & 3 deletions Plugin.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Handsome主题美化插件
* <strong style='color:red'>Handsome主题美化插件</strong>
*
* @package Sky.Mo
* @author Alomerry
Expand All @@ -27,7 +27,8 @@ public static function activate()

Typecho_Plugin::factory('Widget_Archive')->header = array(__CLASS__, 'header');
Typecho_Plugin::factory('Widget_Archive')->footer = array(__CLASS__, 'footer');

Typecho_Plugin::factory('Widget_Feedback')->finishComment = array('SkyMo_Plugin', 'parseComment');
Typecho_Plugin::factory('Widget_Comments_Edit')->finishComment = array('SkyMo_Plugin', 'parseComment');
return "插件启动成功";
}

Expand Down Expand Up @@ -152,6 +153,60 @@ public static function config(Typecho_Widget_Helper_Form $form)
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}



/**
* 触发邮件 API
*
* @access public
* @param $comment 调用参数
* @return void
*/
public static function parseComment($comment)
{
$cfg = array(
'siteTitle' => $options->title,
'timezone' => $options->timezone,
'cid' => $comment->cid,
'coid' => $comment->coid,
'created' => $comment->created,
'author' => $comment->author,
'authorId' => $comment->authorId,
'ownerId' => $comment->ownerId,
'mail' => $comment->mail,
'ip' => $comment->ip,
'title' => $comment->title,
'text' => $comment->text,
'permalink' => $comment->permalink,
'status' => $comment->status,
'parent' => $comment->parent,
);
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
$id = $db->query($db->insert($prefix.'mail')->rows(array(
'content' => base64_encode(serialize($cfg)),
'sent' => '1' ,
'log' => $cfg['author'] . "发表了评论,通知邮件错误:"
)));
try {
// 请根据实际 accesskey 和 secretkey 进行开发,以下只作为演示 sdk 使用
$accesskey = "5f00d72defb9a35396a26416";
$secretkey = "a69a74300141472199b9358becad1bcc";

$emailSender = new EmailSender($accesskey, $secretkey);

// 普通单发
$result = $emailSender->sendSingleEmail(0,"[email protected]","[email protected]",false,"","就当一次路过丶","6666","111","");
// $rsp = json_decode($result);
} catch (\Exception $e) {


}

}



/**
* 修改页面底部信息
*
Expand All @@ -161,7 +216,7 @@ public static function personalConfig(Typecho_Widget_Helper_Form $form){}
public static function footer()
{
// 你能留下我的信息, 我会很高兴的 ^_^
echo "<script>console.log('%c SkyMo v1.1 %c by Alomerry | alomerry.com ','color:#444;background:#eee;padding:5px 0;','color:#eee;background:#448bff;padding:5px 0;');</script>";
echo "<script>console.log('%c SkyMo v1.1 %c by Alomerry | www.alomerry.com ','color:#444;background:#eee;padding:5px 0;','color:#eee;background:#448bff;padding:5px 0;');</script>";

$SkyMo = Helper::options()->plugin('SkyMo');
$path = self::STATIC_DIR;
Expand Down Expand Up @@ -291,4 +346,111 @@ public function input($name = NULL, array $options = NULL)
}

protected function _value($value) {}
}

class EmailSender {
var $url;
var $accesskey;
var $secretkey;

function __construct($accesskey, $secretkey) {
$this->url = "https://live.kewail.com/directmail/v1/singleSendMail";
$this->accesskey = $accesskey;
$this->secretkey = $secretkey;
}

function getRandom() {
return rand(100000, 999999);
}

function sendCurlPost($url, $dataObj) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($dataObj));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen(json_encode($dataObj))));
$ret = curl_exec($curl);
if (false == $ret) {
// curl_exec failed
$result = "{ \"result\":" . -2 . ",\"errmsg\":\"" . curl_error($curl) . "\"}";
} else {
$rsp = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if (200 != $rsp) {
$result = "{ \"result\":" . -1 . ",\"errmsg\":\"". $rsp . " " . curl_error($curl) ."\"}";
} else {
$result = $ret;
}
}
curl_close($curl);
return $result;
}

/**
* 普通单发,明确指定内容,如果有多个签名,请在内容中以【】的方式添加到信息内容中,否则系统将使用默认签名
* @param int $type 邮件类型,0事务投递,其他值的为商业投递量,默认是0
* @param string $toEmail 邮件接收地址
* @param string $fromEmail 发送邮件地址,管理控制台中配置的发信地址(登陆后台查看发信地址)
* @param boolean $needToReply 是否显示回复邮件地址,如果为true是的时候,replyEmail必填,false的时候replyEmail可以为空
* @param string $replyEmail 回复邮件地址
* @param string $fromAlias 发信人昵称,可填空串
* @param string $htmlBody 邮件正文
* @param string $subject 邮件主题
* @param string $ext 扩展字段,可填空串
* @return string json string { "result": xxxxx, "errmsg": "xxxxxx" ... },被省略的内容参见协议文档
*/

function sendSingleEmail($type,$toEmail,$fromEmail,$needToReply,$replyEmail,$fromAlias,$htmlBody,$subject,$ext){
/*
请求包体
{
"sig": "D9544A3D290571425C8C5094542A76C2A19A84745F1DDFFA72F112A58E563517",
"ext": "",
"replyEmail": "[email protected]",
"fromAlias": "张三",
"htmlBody": "test email",
"needToReply": true,
"subject": "测试邮件",
"clickTrace": "0",
"time": 1519378109,
"type": 0,
"toEmail": "[email protected]",
"fromEmail": "[email protected]"
}
应答包体
{
"result": 0,
"errmsg": "OK",
"surplus": 19,
"sequenceId": "5aa1deb00cf2deb46f411ee4"
}
*/

$random = $this->getRandom();
$curTime = time();
$wholeUrl = $this->url . "?accesskey=" . $this->accesskey . "&random=" . $random;

// 按照协议组织 post 包体
$data = new \stdClass();
$data->sig = hash("sha256",
"secretkey=".$this->secretkey."&random=".$random."&time=".$curTime."&fromEmail=".$fromEmail, FALSE);
$data->ext = "";
$data->replyEmail = $replyEmail;
$data->fromAlias = $fromAlias;
$data->htmlBody = $htmlBody;
$data->needToReply = $needToReply;
$data->subject = $subject;
$data->clickTrace = "0";
$data->time = $curTime;
$data->type = $type;
$data->toEmail = $toEmail;
$data->fromEmail = $fromEmail;

return $this->sendCurlPost($wholeUrl, $data);
}

}

0 comments on commit 0d38683

Please sign in to comment.