-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSMTPHub.php
More file actions
92 lines (86 loc) · 3 KB
/
SMTPHub.php
File metadata and controls
92 lines (86 loc) · 3 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
<?php
namespace lib\mail;
/**
* SMTPHub PHP SDK
*/
class SMTPHub
{
private $api;
private $appid;
private $appsecret;
/** 构造函数
* @param string $api 接口地址,如:https://smtphub.yourcompany.com/api.php
* @param string $appid 应用ID,如:1000
* @param string $appsecret 应用密钥,如:abcdefghijklmnopqrestuvwxyz
* @return void
*/
function __construct($api, $appid, $appsecret)
{
$this->api = $api;
$this->appid = $appid;
$this->appsecret = $appsecret;
}
/** 发送邮件
* @param string $to 收件人地址
* @param string $subject 邮件主题
* @param string $message 邮件内容
* @param string $to_name 收件人名称
* @param string $from_name 发送人名称
* @return string|bool 是否发送成功
*/
public function send($to, $subject, $message, $to_name, $from_name)
{
if (empty($this->api) || empty($this->appid) || empty($this->appsecret)) return false;
// 构造请求参数列表
$data = array(
'appid' => $this->appid,
'appsecret' => $this->appsecret,
'action' => 'send',
'to' => $to,
'to_name' => $to_name,
'subject' => $subject,
'message' => $message,
'from_name' => $from_name
);
$res = SMTPHub::curl_send($this->api, $data, 1);
return $res;
}
private function curl_send($url, $data = null, $post = 0, $referer = 0, $cookie = 0, $header = 0, $ua = 0, $nobaody = 0)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$httpheader[] = "Accept: */*";
$httpheader[] = "Accept-Encoding: gzip,deflate,sdch";
$httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
$httpheader[] = "Connection: close";
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if ($header) {
curl_setopt($ch, CURLOPT_HEADER, true);
}
if ($cookie) {
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
if ($referer) {
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
if ($ua) {
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
} else {
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.62");
}
if ($nobaody) {
curl_setopt($ch, CURLOPT_NOBODY, 1);
}
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
}