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