-
Notifications
You must be signed in to change notification settings - Fork 20
/
SubmailMailSend.php
202 lines (158 loc) · 4.81 KB
/
SubmailMailSend.php
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
<?php
namespace App\DAO;
use App\DAO\Lib\mail;
class SubmailMailSend
{
protected $configs;
protected $To = array();
protected $Addressbook = array();
protected $From = '';
protected $From_name = '';
protected $Reply = '';
protected $Cc = array();
protected $Bcc = array();
protected $Subject = '';
protected $Text = '';
protected $Html = '';
protected $Vars = array();
protected $Links = array();
protected $Attachments = array();
protected $Headers = array();
protected $asynchronous = "false";
function __construct($configs)
{
$this->configs = $configs;
}
public function AddTo($address, $name = '')
{
array_push($this->To, array('address' => $address, 'name' => $name));
}
public function AddAddressbook($addressbook)
{
array_push($this->Addressbook, $addressbook);
}
public function SetSender($sender, $name = '')
{
$this->From = $sender;
$this->From_name = $name;
}
public function SetReply($reply)
{
$this->Reply = $reply;
}
public function AddCc($address, $name = '')
{
array_push($this->Cc, array('address' => $address, 'name' => $name));
}
public function AddBcc($address, $name = '')
{
array_push($this->Bcc, array('address' => $address, 'name' => $name));
}
public function SetSubject($subject)
{
$this->Subject = $subject;
}
public function SetText($text)
{
$this->Text = $text;
}
public function SetHtml($html)
{
$this->Html = $html;
}
public function AddVar($key, $val)
{
$this->Vars[$key] = $val;
}
public function AddLink($key, $val)
{
$this->Links[$key] = $val;
}
public function AddAttachment($attachment)
{
array_push($this->Attachments, $attachment);
}
public function AddHeaders($key, $val)
{
$this->Headers[$key] = $val;
}
public function setAsynchronous($asynchronous)
{
if ($asynchronous == true) {
$this->asynchronous = true;
} else {
$this->asynchronous = false;
}
}
protected function buildRequest()
{
$request = array();
if (!empty($this->To)) {
$request['to'] = '';
foreach ($this->To as $tmp) {
$request['to'] .= $tmp['name'] . '<' . $tmp['address'] . '>,';
}
$request['to'] = substr($request['to'], 0, strlen($request['to']) - 1);
//dd($request['to']);
}
if (!empty($this->Addressbook)) {
$request['addressbook'] = '';
foreach ($this->Addressbook as $tmp) {
$request['addressbook'] .= $tmp . ',';
}
$request['addressbook'] = substr($request['addressbook'], 0, strlen($request['addressbook']) - 1);
}
$request['from'] = $this->From;
if ($this->From_name != '') {
$request['from_name'] = $this->From_name;
}
if ($this->Reply != '') {
$request['reply'] = $this->Reply;
}
if (!empty($this->Cc)) {
$request['cc'] = '';
foreach ($this->Cc as $tmp) {
$request['cc'] .= $tmp['name'] . '<' . $tmp['address'] . '>,';
}
$request['cc'] = substr($request['cc'], 0, strlen($request['cc']) - 1);
}
if (!empty($this->Bcc)) {
$request['bcc'] = '';
foreach ($this->Bcc as $tmp) {
$request['bcc'] .= $tmp['name'] . '<' . $tmp['address'] . '>,';
}
$request['bcc'] = substr($request['bcc'], 0, strlen($request['bcc']) - 1);
}
$request['subject'] = $this->Subject;
if ($this->Text != '') {
$request['text'] = $this->Text;
}
if ($this->Html != '') {
$request['html'] = $this->Html;
}
if (!empty($this->Vars)) {
$request['vars'] = json_encode($this->Vars);
}
if (!empty($this->Links)) {
$request['links'] = json_encode($this->Links);
}
if (!empty($this->Attachments)) {
for ($i = 0; $i < count($this->Attachments); $i++) {
//$request['attachments['.$i.']']="@".$this->Attachments[$i];
$request['attachments[' . $i . ']'] = curl_file_create($this->Attachments[$i]);
}
}
if (!empty($this->asynchronous)) {
$request['asynchronous'] = $this->asynchronous;
}
if (!empty($this->Headers)) {
$request['headers'] = json_encode($this->Headers);
}
return $request;
}
public function send()
{
$mail = new mail($this->configs);
return $mail->send($this->buildRequest());
}
}