xref: /plugin/smtp/vendor/txthinking/mailer/src/Mailer.php (revision 28d0809a4424b7a71eb621f24d1b3a19c5927170)
1<?php
2/***************************************************\
3 *
4 *  Mailer (https://github.com/txthinking/Mailer)
5 *
6 *  A lightweight PHP SMTP mail sender.
7 *  Implement RFC0821, RFC0822, RFC1869, RFC2045, RFC2821
8 *
9 *  Support html body, don't worry that the receiver's
10 *  mail client can't support html, because Mailer will
11 *  send both text/plain and text/html body, so if the
12 *  mail client can't support html, it will display the
13 *  text/plain body.
14 *
15 *  Create Date 2012-07-25.
16 *  Under the MIT license.
17 *
18 \***************************************************/
19namespace Tx;
20
21use Psr\Log\LoggerInterface;
22use \Tx\Mailer\Message;
23use \Tx\Mailer\SMTP;
24
25/**
26 * Class Mailer
27 *
28 * This class provides the Mailer public methods for backwards compatibility, but it is recommended
29 * that you use the Tx\Mailer\SMTP and Tx\Mailer\Message classes going forward
30 *
31 * @package Tx
32 */
33class Mailer
34{
35    /**
36     * SMTP Class
37     * @var SMTP
38     */
39    protected $smtp;
40
41    /**
42     * Mail Message
43     * @var Message
44     */
45    protected $message;
46
47    /**
48     * construct function
49     * @param LoggerInterface $logger
50     */
51    public function __construct(LoggerInterface $logger=null)
52    {
53        $this->smtp = new SMTP($logger);
54        $this->message = new Message();
55    }
56
57    /**
58     * set server and port
59     * @param string $host server
60     * @param int $port port
61     * @param string $secure ssl tls tlsv1.0 tlsv1.1 tlsv1.2
62     * @return $this
63     */
64    public function setServer($host, $port, $secure=null)
65    {
66        $this->smtp->setServer($host, $port, $secure);
67        return $this;
68    }
69
70    /**
71     * auth with server
72     * @param string $username
73     * @param string $password
74     * @return $this
75     */
76    public function setAuth($username, $password)
77    {
78        $this->smtp->setAuth($username, $password);
79        return $this;
80    }
81
82    /**
83     * auth oauthbearer with server
84     * @param string $accessToken
85     * @return $this
86     */
87    public function setOAuth($accessToken)
88    {
89        $this->smtp->setOAuth($accessToken);
90        return $this;
91    }
92
93    /**
94     * set mail from
95     * @param string $name
96     * @param string $email
97     * @return $this
98     */
99    public function setFrom($name, $email)
100    {
101        $this->message->setFrom($name, $email);
102        return $this;
103    }
104
105    /**
106     * set fake mail from
107     * @param string $name
108     * @param string $email
109     * @return $this
110     */
111    public function setFakeFrom($name, $email)
112    {
113        $this->message->setFakeFrom($name, $email);
114        return $this;
115    }
116
117    /**
118     * add mail receiver
119     * @param string $name
120     * @param string $email
121     * @return $this
122     */
123    public function addTo($name, $email)
124    {
125        $this->message->addTo($name, $email);
126        return $this;
127    }
128
129    /**
130     * add cc mail receiver
131     * @param string $name
132     * @param string $email
133     * @return $this
134     */
135    public function addCc($name, $email)
136    {
137        $this->message->addCc($name, $email);
138        return $this;
139    }
140
141    /**
142     * add bcc mail receiver
143     * @param string $name
144     * @param string $email
145     * @return $this
146     */
147    public function addBcc($name, $email)
148    {
149        $this->message->addBcc($name, $email);
150        return $this;
151    }
152
153    /**
154     * set mail subject
155     * @param string $subject
156     * @return $this
157     */
158    public function setSubject($subject)
159    {
160        $this->message->setSubject($subject);
161        return $this;
162    }
163
164    /**
165     * set mail body
166     * @param string $body
167     * @return $this
168     */
169    public function setBody($body)
170    {
171        $this->message->setBody($body);
172        return $this;
173    }
174
175    /**
176     * add mail attachment
177     * @param $name
178     * @param $path
179     * @return $this
180     */
181    public function addAttachment($name, $path)
182    {
183        $this->message->addAttachment($name, $path);
184        return $this;
185    }
186
187    /**
188     * set mail reply-to
189     * @param string $name
190     * @param string $email
191     * @return $this
192     */
193    public function setReplyTo($name, $email)
194    {
195        $this->message->setReplyTo($name, $email);
196        return $this;
197    }
198
199    /**
200     *  Send the message...
201     * @return boolean
202     */
203    public function send()
204    {
205        return $this->smtp->send($this->message);
206    }
207
208}
209
210