1<?php namespace Tx;
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 \***************************************************/
19
20use Psr\Log\LoggerInterface;
21use \Tx\Mailer\Message;
22use \Tx\Mailer\SMTP;
23
24/**
25 * Class Mailer
26 *
27 * This class provides the Mailer public methods for backwards compatibility, but it is recommended
28 * that you use the Tx\Mailer\SMTP and Tx\Mailer\Message classes going forward
29 *
30 * @package Tx
31 */
32class Mailer{
33    /**
34     * SMTP Class
35     * @var SMTP
36     */
37    protected $smtp;
38
39    /**
40     * Mail Message
41     * @var Message
42     */
43    protected $message;
44
45    /**
46     * construct function
47     */
48    public function __construct(LoggerInterface $logger=null){
49        $this->smtp = new SMTP($logger);
50        $this->message = new Message();
51    }
52
53    /**
54     * set server and port
55     * @param string $host server
56     * @param int $port port
57     * @param string $secure ssl tls
58     * @return $this
59     */
60    public function setServer($host, $port, $secure=null){
61        $this->smtp->setServer($host, $port, $secure);
62        return $this;
63    }
64
65    /**
66     * auth with server
67     * @param string $username
68     * @param string $password
69     * @return $this
70     */
71    public function setAuth($username, $password){
72        $this->smtp->setAuth($username, $password);
73        return $this;
74    }
75
76    /**
77     * set mail from
78     * @param string $name
79     * @param string $email
80     * @return $this
81     */
82    public function setFrom($name, $email){
83        $this->message->setFrom($name, $email);
84        return $this;
85    }
86
87    /**
88     * set fake mail from
89     * @param string $name
90     * @param string $email
91     * @return $this
92     */
93    public function setFakeFrom($name, $email){
94        $this->message->setFakeFrom($name, $email);
95        return $this;
96    }
97
98    /**
99     * set mail receiver
100     * @param string $name
101     * @param string $email
102     * @return $this
103     */
104    public function setTo($name, $email){
105        $this->message->addTo($name, $email);
106        return $this;
107    }
108
109    /**
110     * add mail receiver
111     * @param string $name
112     * @param string $email
113     * @return $this
114     */
115    public function addTo($name, $email){
116        $this->message->addTo($name, $email);
117        return $this;
118    }
119
120    /**
121     * set mail subject
122     * @param string $subject
123     * @return $this
124     */
125    public function setSubject($subject){
126        $this->message->setSubject($subject);
127        return $this;
128    }
129
130    /**
131     * set mail body
132     * @param string $body
133     * @return $this
134     */
135    public function setBody($body){
136        $this->message->setBody($body);
137        return $this;
138    }
139
140    /**
141     * set mail attachment
142     * @param $name
143     * @param $path
144     * @return $this
145     * @internal param string $attachment
146     */
147    public function setAttachment($name, $path){
148        $this->message->addAttachment($name, $path);
149        return $this;
150    }
151
152    /**
153     * add mail attachment
154     * @param $name
155     * @param $path
156     * @return $this
157     * @internal param string $attachment
158     */
159    public function addAttachment($name, $path){
160        $this->message->addAttachment($name, $path);
161        return $this;
162    }
163
164    /**
165     *  Send the message...
166     * @return boolean
167     */
168    public function send(){
169        return $this->smtp->send($this->message);
170    }
171
172}
173
174