MailCloud.class.php
2.9 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
<?php
/**
* MailCloud.class.php
* 邮件发送服务
*/
namespace Com;
class MailCloud
{
/** 用户名 */
protected $_account = '';
/** 密码 */
protected $_passwd = '';
/** 发送人邮箱 */
protected $_from = '';
/** 发送人名字 */
protected $_fromname = '';
/** 发送普通短信 */
const TPL_URL = 'https://sendcloud.sohu.com/webapi/mail.send_template.xml';
/**
* &get_instance
* 获取一个短信发送类的实例
*
* @return object
*/
public static function &instance()
{
static $instance = null;
if (! $instance) {
$instance = new MailCloud();
}
return $instance;
}
/**
* __construct
*
* @param mixed $group
* @return void
*/
public function __construct()
{
$this->_account = cfg('MAILCLOUD.ACCOUNT');
$this->_passwd = cfg('MAILCLOUD.PASSWORD');
$this->_from = cfg('MAILCLOUD.FROM');
$this->_fromname = cfg('MAILCLOUD.FROMNAME');
}
/**
* 发送模板邮件
*
* @param string $tpl_name
* 模板名称
* @param array $mails
* 接收人邮箱地址
* @param string $subject
* 邮箱主题
* @param array $vars
* 模板邮件的变量值, 保持和接收人邮箱地址一致的顺序
*/
public function send_tpl_mail($tpl_name, $mails, $subject, $vars = array(), $from = '', $fromname = '')
{
$tpl_vars = array(
'to' => $mails,
'sub' => $vars
);
// 判断发送人(邮箱)是否为空
if (empty($from)) {
$from = $this->_from;
}
// 判断发送人名称是否
if (empty($fromname)) {
$fromname = $this->_fromname;
}
// 发送参数
$param = array(
'api_user' => $this->_account,
'api_key' => $this->_passwd,
'from' => $from,
'fromname' => $fromname,
'template_invoke_name' => $tpl_name,
'subject' => $subject,
'substitution_vars' => json_encode($tpl_vars)
);
$options = array(
'http' => array(
'method' => 'POST',
'header' => "Connection: close\r\nContent-type: application/x-www-form-urlencoded",
'content' => http_build_query($param)
)
);
$context = stream_context_create($options);
$result = file_get_contents(self::TPL_URL, false, $context);
// 解析xml
$xml = (array) simplexml_load_string($result);
// 如果成功
if (isset($xml['message']) && 'success' == trim($xml['message'])) {
return true;
}
// 记录错误日志
\Think\Log::record(self::TPL_URL . '=>' . $result . '=>' . var_export($param, true));
return false;
}
}