-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentSendCloud.php
More file actions
205 lines (191 loc) · 7.62 KB
/
Copy pathCommentSendCloud.php
File metadata and controls
205 lines (191 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* 评论回复邮件提醒插件,SendCloud版
*
* @package CommentSendCloud
* @author DEFE
* @version beta
* @link http://defe.me
*/
class CommentSendCloud implements Typecho_Plugin_Interface {
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate() {
Typecho_Plugin::factory('Widget_Feedback')->finishComment = array('CommentSendCloud', 'toSendCloud');
return _t('请对插件进行正确设置,以使插件顺利工作!');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate() { }
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form) {
// API_USER
$api_user = new Typecho_Widget_Helper_Form_Element_Text('api_user', NULL, NULL, _t('SendCloud发信API USER'), _t('使用SendCloud的API_USER'));
$api_user->input->setAttribute('class', 'mini');
$form->addInput($api_user);
// API_KEY
$api_key = new Typecho_Widget_Helper_Form_Element_Password('api_key', NULL, NULL, _t('SendCloud发信API KEY'), _t('SendCloud的API_KEY'));
$form->addInput($api_key);
// 发件人信箱
$send_from = new Typecho_Widget_Helper_Form_Element_Text('send_from', NULL, NULL, _t('发件人邮件地址'), _t('邮箱域名尽量与SendCloud中配置的发信域名一致'));
$form->addInput($send_from->addRule('email', _t('请填写正确的邮箱!')));
//收件邮箱
$mail = new Typecho_Widget_Helper_Form_Element_Text('mail', NULL, NULL,
_t('接收邮箱'),_t('接收邮件用的信箱,如为空则使用文章作者个人设置中的邮箱!'));
$form->addInput($mail->addRule('email', _t('请填写正确的邮箱!')));
// 模板名称
$template = new Typecho_Widget_Helper_Form_Element_Text('template', NULL, NULL, _t( '模板名称'), _t('请填入在SendCloud配置的博主模板名称'));
$template->input->setAttribute('class', 'mini');
$form->addInput($template);
$templateg = new Typecho_Widget_Helper_Form_Element_Text('templateg', NULL, NULL, _t( '模板名称'), _t('发送给评论者的模板'));
$templateg->input->setAttribute('class', 'mini');
$form->addInput($templateg);
$titleForOwner = new Typecho_Widget_Helper_Form_Element_Text('titleForOwner',null,"[%site%]:《%title%》有新的评论",
_t('博主接收邮件标题'));
$form->addInput($titleForOwner);
$titleForGuest = new Typecho_Widget_Helper_Form_Element_Text('titleForGuest',null,"[%site%]:您在《%title%》的评论有了回复",
_t('访客接收邮件标题'));
$form->addInput($titleForGuest);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form) {
}
/**
* 组合邮件内容
*
* @access public
* @param $post 调用参数
* @return void
*/
public static function toSendCloud($post) {
//$smtp=array();
$options = Typecho_Widget::widget('Widget_Options');
$set = Helper::options()->plugin('CommentSendCloud');
$site = $options->title;
$timezone = $options->timezone;
$cid=$post->cid;
$coid=$post->coid;
$created=$post->created;
$author=$post->author;
$authorId=$post->authorId;
$ownerId=$post->ownerId;
$mail=$post->mail;
$ip=$post->ip;
$title = $post->title;
$text=$post->text;
$permalink=$post->permalink;
$status=$post->status;
$parent=$post->parent;
$time = date("Y-m-d H:i:s",$created+$timezone);
$subject = '《' . $title . '》 有新回复!';
$xsmtpapi = json_encode(
array(
'to' => array($set->mail),
'sub' => array(
'%site%' => array(trim($site)),
'%title%' => array(trim($title)),
'%author%' => array(trim($author)),
'%mail%' => array(trim($mail)),
'%time%' => array(trim($time)),
'%permalink%' => array(trim($permalink)),
'%text%' => array(trim($text))
)
)
);
// 请求参数
$param = array(
'apiUser' => $set->api_user,
'apiKey' => $set->api_key,
'from' => $set->send_from,
'fromName' => $site,
'subject' =>$subject,
'xsmtpapi' => $xsmtpapi,
'templateInvokeName' => $set->template
);
self::sendMail($param);
if(0!=$parent && 'approved'==$status){
$db = Typecho_Db::get();
$original = $db->fetchRow($db->select('author', 'mail', 'text')
->from('table.comments')
->where('coid = ?', $parent));
$subject = '您在[' . $site . '] 的评论有了回复!';
$xsmtpapi = json_encode(
array(
'to' => array($original['mail']),
'sub' => array(
'%site%' => array(trim($site)),
'%title%' => array(trim($title)),
'%original_author%' => array(trim($original['author'])),
'%original_text%' => array(trim($original['text'])),
'%author%' => array(trim($author)),
'%time%' => array(trim($time)),
'%permalink%' => array(trim($permalink)),
'%text%' => array(trim($text))
)
)
);
// 请求参数
$param1 = array(
'apiUser' => $set->api_user,
'apiKey' => $set->api_key,
'from' => $set->send_from,
'fromName' => $site,
'subject' =>$subject,
'xsmtpapi' => $xsmtpapi,
'templateInvokeName' => $set->templateg
);
self::sendMail($param1);
}
}
/**
* 生成邮件内容并发送
*
* @access public
* @param string $param sendcloud邮件参数
* @return void
*
*
*/
public function sendMail($param){
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'http://api.sendcloud.net/apiv2/mail/sendtemplate');
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
// 执行请求
$result = curl_exec($ch);
// 获取错误代码
$errno = curl_errno($ch);
// 获取错误信息
$error = curl_error($ch);
if($result === false) {
file_put_contents('.'.__TYPECHO_PLUGIN_DIR__.'/CommentSendCloud_log.txt', $result.$error);
}
// 关闭curl
curl_close($ch);
}
}