Mailer.php
4.0 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
<?php
/***************************************************\
*
* Mailer (https://github.com/txthinking/Mailer)
*
* A lightweight PHP SMTP mail sender.
* Implement RFC0821, RFC0822, RFC1869, RFC2045, RFC2821
*
* Support html body, don't worry that the receiver's
* mail client can't support html, because Mailer will
* send both text/plain and text/html body, so if the
* mail client can't support html, it will display the
* text/plain body.
*
* Create Date 2012-07-25.
* Under the MIT license.
*
\***************************************************/
namespace Tx;
use Psr\Log\LoggerInterface;
use \Tx\Mailer\Message;
use \Tx\Mailer\SMTP;
/**
* Class Mailer
*
* This class provides the Mailer public methods for backwards compatibility, but it is recommended
* that you use the Tx\Mailer\SMTP and Tx\Mailer\Message classes going forward
*
* @package Tx
*/
class Mailer
{
/**
* SMTP Class
* @var SMTP
*/
protected $smtp;
/**
* Mail Message
* @var Message
*/
protected $message;
/**
* construct function
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger=null)
{
$this->smtp = new SMTP($logger);
$this->message = new Message();
}
/**
* set server and port
* @param string $host server
* @param int $port port
* @param string $secure ssl tls tlsv1.0 tlsv1.1 tlsv1.2
* @return $this
*/
public function setServer($host, $port, $secure=null)
{
$this->smtp->setServer($host, $port, $secure);
return $this;
}
/**
* auth with server
* @param string $username
* @param string $password
* @return $this
*/
public function setAuth($username, $password)
{
$this->smtp->setAuth($username, $password);
return $this;
}
/**
* auth oauthbearer with server
* @param string $accessToken
* @return $this
*/
public function setOAuth($accessToken)
{
$this->smtp->setOAuth($accessToken);
return $this;
}
/**
* set mail from
* @param string $name
* @param string $email
* @return $this
*/
public function setFrom($name, $email)
{
$this->message->setFrom($name, $email);
return $this;
}
/**
* set fake mail from
* @param string $name
* @param string $email
* @return $this
*/
public function setFakeFrom($name, $email)
{
$this->message->setFakeFrom($name, $email);
return $this;
}
/**
* add mail receiver
* @param string $name
* @param string $email
* @return $this
*/
public function addTo($name, $email)
{
$this->message->addTo($name, $email);
return $this;
}
/**
* add cc mail receiver
* @param string $name
* @param string $email
* @return $this
*/
public function addCc($name, $email)
{
$this->message->addCc($name, $email);
return $this;
}
/**
* add bcc mail receiver
* @param string $name
* @param string $email
* @return $this
*/
public function addBcc($name, $email)
{
$this->message->addBcc($name, $email);
return $this;
}
/**
* set mail subject
* @param string $subject
* @return $this
*/
public function setSubject($subject)
{
$this->message->setSubject($subject);
return $this;
}
/**
* set mail body
* @param string $body
* @return $this
*/
public function setBody($body)
{
$this->message->setBody($body);
return $this;
}
/**
* add mail attachment
* @param $name
* @param $path
* @return $this
*/
public function addAttachment($name, $path)
{
$this->message->addAttachment($name, $path);
return $this;
}
/**
* Send the message...
* @return boolean
*/
public function send()
{
return $this->smtp->send($this->message);
}
}