This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
/
YiiMail.php
242 lines (223 loc) · 6.8 KB
/
YiiMail.php
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
* YiiMail class file.
*
* @author Jonah Turnquist <poppitypop@gmail.com>
* @link https://code.google.com/p/yii-mail/
* @package Yii-Mail
*/
/**
* YiiMail is an application component used for sending email.
*
* You may configure it as below. Check the public attributes and setter
* methods of this class for more options.
* <pre>
* return array(
* ...
* 'import => array(
* ...
* 'ext.mail.YiiMailMessage',
* ),
* 'components' => array(
* 'mail' => array(
* 'class' => 'ext.yii-mail.YiiMail',
* 'transportType' => 'php',
* 'viewPath' => 'application.views.mail',
* 'logging' => true,
* 'dryRun' => false
* ),
* ...
* )
* );
* </pre>
*
* Example usage:
* <pre>
* $message = new YiiMailMessage;
* $message->setBody('Message content here with HTML', 'text/html');
* $message->subject = 'My Subject';
* $message->addTo('johnDoe@domain.com');
* $message->from = Yii::app()->params['adminEmail'];
* Yii::app()->mail->send($message);
* </pre>
*/
class YiiMail extends CApplicationComponent
{
/**
* @var bool whether to log messages using Yii::log().
* Defaults to true.
*/
public $logging = true;
/**
* @var bool whether to disable actually sending mail.
* Defaults to false.
*/
public $dryRun = false;
/**
* @var string the delivery type. Can be either 'php' or 'smtp'. When
* using 'php', PHP's {@link mail()} function will be used.
* Defaults to 'php'.
*/
public $transportType = 'php';
/**
* @var string the path to the location where mail views are stored.
* Defaults to 'application.views.mail'.
*/
public $viewPath = 'application.views.mail';
/**
* @var string options specific to the transport type being used.
* To set options for STMP, set this attribute to an array where the keys
* are the option names and the values are their values.
* Possible options for SMTP are:
* <ul>
* <li>host</li>
* <li>username</li>
* <li>password</li>
* <li>port</li>
* <li>encryption</li>
* <li>timeout</li>
* <li>extensionHandlers</li>
* </ul>
* See the SwiftMailer documentaion for the option meanings.
*/
public $transportOptions;
/**
* @var mixed Holds the SwiftMailer transport
*/
protected $transport;
/**
* @var mixed Holds the SwiftMailer mailer
*/
protected $mailer;
private static $registeredScripts = false;
/**
* Calls the {@link registerScripts()} method.
*/
public function init() {
$this->registerScripts();
parent::init();
}
/**
* Send a {@link YiiMailMessage} as it would be sent in a mail client.
*
* All recipients (with the exception of Bcc) will be able to see the other
* recipients this message was sent to.
*
* If you need to send to each recipient without disclosing details about the
* other recipients see {@link batchSend()}.
*
* Recipient/sender data will be retreived from the {@link YiiMailMessage}
* object.
*
* The return value is the number of recipients who were accepted for
* delivery.
*
* @param YiiMailMessage $message
* @param array &$failedRecipients, optional
* @return int
* @see batchSend()
*/
public function send(YiiMailMessage $message, &$failedRecipients = null) {
if ($this->logging===true) self::log($message);
if ($this->dryRun===true) return count($message->to);
else return $this->getMailer()->send($message->message, $failedRecipients);
}
/**
* Send the given {@link YiiMailMessage} to all recipients individually.
*
* This differs from {@link send()} in the way headers are presented to the
* recipient. The only recipient in the "To:" field will be the individual
* recipient it was sent to.
*
* If an iterator is provided, recipients will be read from the iterator
* one-by-one, otherwise recipient data will be retreived from the
* {@link YiiMailMessage} object.
*
* Sender information is always read from the {@link YiiMailMessage} object.
*
* The return value is the number of recipients who were accepted for
* delivery.
*
* @param YiiMailMessage $message
* @param array &$failedRecipients, optional
* @param Swift_Mailer_RecipientIterator $it, optional
* @return int
* @see send()
*/
public function batchSend(YiiMailMessage $message, &$failedRecipients = null, Swift_Mailer_RecipientIterator $it = null) {
if ($this->logging===true) self::log($message);
if ($this->dryRun===true) return count($message->to);
else return $this->getMailer()->batchSend($message->message, $failedRecipients, $it);
}
/**
* Sends a message in an extremly simple but less extensive way.
*
* @param mixed from address, string or array of the form $address => $name
* @param mixed to address, string or array of the form $address => $name
* @param string subject
* @param string body
*/
public function sendSimple($from, $to, $subject, $body) {
$message = new YiiMailMessage;
$message->setSubject($subject)
->setFrom($from)
->setTo($to)
->setBody($body, 'text/html');
if ($this->logging===true) self::log($message);
if ($this->dryRun===true) return count($message->to);
else return $this->getMailer()->send($message);
}
/**
* Logs a YiiMailMessage in a (hopefully) readable way using Yii::log.
* @return string log message
*/
public static function log(YiiMailMessage $message) {
$msg = 'Sending email to '.implode(', ', array_keys($message->to))."\n".
implode('', $message->headers->getAll())."\n".
$message->body
;
Yii::log($msg, CLogger::LEVEL_INFO, 'ext.yii-mail.YiiMail'); // TODO: attempt to determine alias/category at runtime
return $msg;
}
/**
* Gets the SwiftMailer transport class instance, initializing it if it has
* not been created yet
* @return mixed {@link Swift_MailTransport} or {@link Swift_SmtpTransport}
*/
public function getTransport() {
if ($this->transport===null) {
switch ($this->transportType) {
case 'php':
$this->transport = Swift_MailTransport::newInstance();
if ($this->transportOptions !== null)
$this->transport->setExtraParams($this->transportOptions);
break;
case 'smtp':
$this->transport = Swift_SmtpTransport::newInstance();
foreach ($this->transportOptions as $option => $value)
$this->transport->{'set'.ucfirst($option)}($value); // sets option with the setter method
break;
}
}
return $this->transport;
}
/**
* Gets the SwiftMailer {@link Swift_Mailer} class instance
* @return Swift_Mailer
*/
public function getMailer() {
if ($this->mailer===null)
$this->mailer = Swift_Mailer::newInstance($this->getTransport());
return $this->mailer;
}
/**
* Registers swiftMailer autoloader and includes the required files
*/
public function registerScripts() {
if (self::$registeredScripts) return;
self::$registeredScripts = true;
require dirname(__FILE__).'/vendors/swiftMailer/classes/Swift.php';
Yii::registerAutoloader(array('Swift','autoload'));
require dirname(__FILE__).'/vendors/swiftMailer/swift_init.php';
}
}