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