root@9
|
1 <?php
|
root@9
|
2 /**
|
root@9
|
3 * @file
|
root@9
|
4 * The code processing mail in the smtp module.
|
root@9
|
5 *
|
root@9
|
6 */
|
root@9
|
7
|
root@9
|
8 /**
|
root@9
|
9 * Modify the drupal mail system to use smtp when sending emails.
|
root@9
|
10 * Include the option to choose between plain text or HTML
|
root@9
|
11 */
|
root@9
|
12 class SmtpMailSystem implements MailSystemInterface {
|
root@9
|
13
|
root@9
|
14 protected $AllowHtml;
|
root@9
|
15 /**
|
root@9
|
16 * Concatenate and wrap the e-mail body for either
|
root@9
|
17 * plain-text or HTML emails.
|
root@9
|
18 *
|
root@9
|
19 * @param $message
|
root@9
|
20 * A message array, as described in hook_mail_alter().
|
root@9
|
21 *
|
root@9
|
22 * @return
|
root@9
|
23 * The formatted $message.
|
root@9
|
24 */
|
root@9
|
25 public function format(array $message) {
|
root@9
|
26 $this->AllowHtml = variable_get('smtp_allowhtml', 0);
|
root@9
|
27 // Join the body array into one string.
|
root@9
|
28 $message['body'] = implode("\n\n", $message['body']);
|
root@9
|
29 if ($this->AllowHtml == 0) {
|
root@9
|
30 // Convert any HTML to plain-text.
|
root@9
|
31 $message['body'] = drupal_html_to_text($message['body']);
|
root@9
|
32 // Wrap the mail body for sending.
|
root@9
|
33 $message['body'] = drupal_wrap_mail($message['body']);
|
root@9
|
34 }
|
root@9
|
35 return $message;
|
root@9
|
36 }
|
root@9
|
37
|
root@9
|
38 /**
|
root@9
|
39 * Send the e-mail message.
|
root@9
|
40 *
|
root@9
|
41 * @see drupal_mail()
|
root@9
|
42 *
|
root@9
|
43 * @param $message
|
root@9
|
44 * A message array, as described in hook_mail_alter().
|
root@9
|
45 * @return
|
root@9
|
46 * TRUE if the mail was successfully accepted, otherwise FALSE.
|
root@9
|
47 */
|
root@9
|
48 public function mail(array $message) {
|
root@9
|
49 $id = $message['id'];
|
root@9
|
50 $to = $message['to'];
|
root@9
|
51 $from = $message['from'];
|
root@9
|
52 $body = $message['body'];
|
root@9
|
53 $headers = $message['headers'];
|
root@9
|
54 $subject = $message['subject'];
|
root@9
|
55
|
root@9
|
56 // Create a new PHPMailer object - autoloaded from registry.
|
root@9
|
57 $mailer = new PHPMailer();
|
root@9
|
58
|
root@9
|
59 // Turn on debugging, if requested.
|
root@9
|
60 if (variable_get('smtp_debugging', 0) == 1) {
|
root@9
|
61 $mailer->SMTPDebug = TRUE;
|
root@9
|
62 }
|
root@9
|
63
|
root@9
|
64 // Set the from name.
|
root@9
|
65 if (variable_get('smtp_fromname', '') != '') {
|
root@9
|
66 $from_name = variable_get('smtp_fromname', '');
|
root@9
|
67 }
|
root@9
|
68 else {
|
root@9
|
69 // If value is not defined in settings, use site_name.
|
root@9
|
70 $from_name = variable_get('site_name', '');
|
root@9
|
71 }
|
root@9
|
72
|
root@9
|
73 //Hack to fix reply-to issue.
|
root@9
|
74 $properfrom = variable_get('site_mail', '');
|
root@9
|
75 if (!empty($properfrom)) {
|
root@9
|
76 $headers['From'] = $properfrom;
|
root@9
|
77 }
|
root@9
|
78 if (!isset($headers['Reply-To']) || empty($headers['Reply-To'])) {
|
root@9
|
79 if (strpos($from, '<')) {
|
root@9
|
80 $reply = preg_replace('/>.*/', '', preg_replace('/.*</', '', $from));
|
root@9
|
81 }
|
root@9
|
82 else {
|
root@9
|
83 $reply = $from;
|
root@9
|
84 }
|
root@9
|
85 $headers['Reply-To'] = $reply;
|
root@9
|
86 }
|
root@9
|
87
|
root@9
|
88 // Blank value will let the e-mail address appear.
|
root@9
|
89
|
root@9
|
90 if ($from == NULL || $from == '') {
|
root@9
|
91 // If from e-mail address is blank, use smtp_from config option.
|
root@9
|
92 if (($from = variable_get('smtp_from', '')) == '') {
|
root@9
|
93 // If smtp_from config option is blank, use site_email.
|
root@9
|
94 if (($from = variable_get('site_mail', '')) == '') {
|
root@9
|
95 drupal_set_message(t('There is no submitted from address.'), 'error');
|
root@9
|
96 watchdog('smtp', 'There is no submitted from address.', array(), WATCHDOG_ERROR);
|
root@9
|
97 return FALSE;
|
root@9
|
98 }
|
root@9
|
99 }
|
root@9
|
100 }
|
root@9
|
101 if (preg_match('/^"?.*"?\s*<.*>$/', $from)) {
|
root@9
|
102 // . == Matches any single character except line break characters \r and \n.
|
root@9
|
103 // * == Repeats the previous item zero or more times.
|
root@9
|
104 $from_name = preg_replace('/"?([^("\t\n)]*)"?.*$/', '$1', $from); // It gives: Name
|
root@9
|
105 $from = preg_replace("/(.*)\<(.*)\>/i", '$2', $from); // It gives: name@domain.tld
|
root@9
|
106 }
|
root@9
|
107 elseif (!valid_email_address($from)) {
|
root@9
|
108 drupal_set_message(t('The submitted from address (@from) is not valid.', array('@from' => $from)), 'error');
|
root@9
|
109 watchdog('smtp', 'The submitted from address (@from) is not valid.', array('@from' => $from), WATCHDOG_ERROR);
|
root@9
|
110 return FALSE;
|
root@9
|
111 }
|
root@9
|
112
|
root@9
|
113 // Defines the From value to what we expect.
|
root@9
|
114 $mailer->From = $from;
|
root@9
|
115 $mailer->FromName = $from_name;
|
root@9
|
116 $mailer->Sender = $from;
|
root@9
|
117
|
root@9
|
118
|
root@9
|
119 // Create the list of 'To:' recipients.
|
root@9
|
120 $torecipients = explode(',', $to);
|
root@9
|
121 foreach ($torecipients as $torecipient) {
|
root@9
|
122 if (strpos($torecipient, '<') !== FALSE) {
|
root@9
|
123 $toparts = explode(' <', $torecipient);
|
root@9
|
124 $toname = $toparts[0];
|
root@9
|
125 $toaddr = rtrim($toparts[1], '>');
|
root@9
|
126 }
|
root@9
|
127 else {
|
root@9
|
128 $toname = '';
|
root@9
|
129 $toaddr = $torecipient;
|
root@9
|
130 }
|
root@9
|
131 $mailer->AddAddress($toaddr, $toname);
|
root@9
|
132 }
|
root@9
|
133
|
root@9
|
134
|
root@9
|
135 // Parse the headers of the message and set the PHPMailer object's settings
|
root@9
|
136 // accordingly.
|
root@9
|
137 foreach ($headers as $key => $value) {
|
root@9
|
138 //watchdog('error', 'Key: ' . $key . ' Value: ' . $value);
|
root@9
|
139 switch (drupal_strtolower($key)) {
|
root@9
|
140 case 'from':
|
root@9
|
141 if ($from == NULL or $from == '') {
|
root@9
|
142 // If a from value was already given, then set based on header.
|
root@9
|
143 // Should be the most common situation since drupal_mail moves the
|
root@9
|
144 // from to headers.
|
root@9
|
145 $from = $value;
|
root@9
|
146 $mailer->From = $value;
|
root@9
|
147 // then from can be out of sync with from_name !
|
root@9
|
148 $mailer->FromName = '';
|
root@9
|
149 $mailer->Sender = $value;
|
root@9
|
150 }
|
root@9
|
151 break;
|
root@9
|
152 case 'content-type':
|
root@9
|
153 // Parse several values on the Content-type header, storing them in an array like
|
root@9
|
154 // key=value -> $vars['key']='value'
|
root@9
|
155 $vars = explode(';', $value);
|
root@9
|
156 foreach ($vars as $i => $var) {
|
root@9
|
157 if ($cut = strpos($var, '=')) {
|
root@9
|
158 $new_var = trim(drupal_strtolower(drupal_substr($var, $cut + 1)));
|
root@9
|
159 $new_key = trim(drupal_substr($var, 0, $cut));
|
root@9
|
160 unset($vars[$i]);
|
root@9
|
161 $vars[$new_key] = $new_var;
|
root@9
|
162 }
|
root@9
|
163 }
|
root@9
|
164 // Set the charset based on the provided value, otherwise set it to UTF-8 (which is Drupals internal default).
|
root@9
|
165 $mailer->CharSet = isset($vars['charset']) ? $vars['charset'] : 'UTF-8';
|
root@9
|
166 // If $vars is empty then set an empty value at index 0 to avoid a PHP warning in the next statement
|
root@9
|
167 $vars[0] = isset($vars[0])?$vars[0]:'';
|
root@9
|
168
|
root@9
|
169 switch ($vars[0]) {
|
root@9
|
170 case 'text/plain':
|
root@9
|
171 // The message includes only a plain text part.
|
root@9
|
172 $mailer->IsHTML(FALSE);
|
root@9
|
173 $content_type = 'text/plain';
|
root@9
|
174 break;
|
root@9
|
175 case 'text/html':
|
root@9
|
176 // The message includes only an HTML part.
|
root@9
|
177 $mailer->IsHTML(TRUE);
|
root@9
|
178 $content_type = 'text/html';
|
root@9
|
179 break;
|
root@9
|
180 case 'multipart/related':
|
root@9
|
181 // Get the boundary ID from the Content-Type header.
|
root@9
|
182 $boundary = $this->_get_substring($value, 'boundary', '"', '"');
|
root@9
|
183
|
root@9
|
184 // The message includes an HTML part w/inline attachments.
|
root@9
|
185 $mailer->ContentType = $content_type = 'multipart/related; boundary="' . $boundary . '"';
|
root@9
|
186 break;
|
root@9
|
187 case 'multipart/alternative':
|
root@9
|
188 // The message includes both a plain text and an HTML part.
|
root@9
|
189 $mailer->ContentType = $content_type = 'multipart/alternative';
|
root@9
|
190
|
root@9
|
191 // Get the boundary ID from the Content-Type header.
|
root@9
|
192 $boundary = $this->_get_substring($value, 'boundary', '"', '"');
|
root@9
|
193 break;
|
root@9
|
194 case 'multipart/mixed':
|
root@9
|
195 // The message includes one or more attachments.
|
root@9
|
196 $mailer->ContentType = $content_type = 'multipart/mixed';
|
root@9
|
197
|
root@9
|
198 // Get the boundary ID from the Content-Type header.
|
root@9
|
199 $boundary = $this->_get_substring($value, 'boundary', '"', '"');
|
root@9
|
200 break;
|
root@9
|
201 default:
|
root@9
|
202 // Everything else is unsuppored by PHPMailer.
|
root@9
|
203 drupal_set_message(t('The %header of your message is not supported by PHPMailer and will be sent as text/plain instead.', array('%header' => "Content-Type: $value")), 'error');
|
root@9
|
204 watchdog('smtp', 'The %header of your message is not supported by PHPMailer and will be sent as text/plain instead.', array('%header' => "Content-Type: $value"), WATCHDOG_ERROR);
|
root@9
|
205
|
root@9
|
206 // Force the Content-Type to be text/plain.
|
root@9
|
207 $mailer->IsHTML(FALSE);
|
root@9
|
208 $content_type = 'text/plain';
|
root@9
|
209 }
|
root@9
|
210 break;
|
root@9
|
211
|
root@9
|
212 case 'reply-to':
|
root@9
|
213 // Only add a "reply-to" if it's not the same as "return-path".
|
root@9
|
214 if ($value != $headers['Return-Path']) {
|
root@9
|
215 if (strpos($value, '<') !== FALSE) {
|
root@9
|
216 $replyToParts = explode('<', $value);
|
root@9
|
217 $replyToName = trim($replyToParts[0]);
|
root@9
|
218 $replyToName = trim($replyToName, '"');
|
root@9
|
219 $replyToAddr = rtrim($replyToParts[1], '>');
|
root@9
|
220 $mailer->AddReplyTo($replyToAddr, $replyToName);
|
root@9
|
221 }
|
root@9
|
222 else {
|
root@9
|
223 $mailer->AddReplyTo($value);
|
root@9
|
224 }
|
root@9
|
225 }
|
root@9
|
226 break;
|
root@9
|
227
|
root@9
|
228 case 'content-transfer-encoding':
|
root@9
|
229 $mailer->Encoding = $value;
|
root@9
|
230 break;
|
root@9
|
231
|
root@9
|
232 case 'return-path':
|
root@9
|
233 case 'mime-version':
|
root@9
|
234 case 'x-mailer':
|
root@9
|
235 // Let PHPMailer specify these.
|
root@9
|
236 break;
|
root@9
|
237
|
root@9
|
238 case 'errors-to':
|
root@9
|
239 $mailer->AddCustomHeader('Errors-To: ' . $value);
|
root@9
|
240 break;
|
root@9
|
241
|
root@9
|
242 case 'cc':
|
root@9
|
243 $ccrecipients = explode(',', $value);
|
root@9
|
244 foreach ($ccrecipients as $ccrecipient) {
|
root@9
|
245 if (strpos($ccrecipient, '<') !== FALSE) {
|
root@9
|
246 $ccparts = explode(' <', $ccrecipient);
|
root@9
|
247 $ccname = $ccparts[0];
|
root@9
|
248 $ccaddr = rtrim($ccparts[1], '>');
|
root@9
|
249 }
|
root@9
|
250 else {
|
root@9
|
251 $ccname = '';
|
root@9
|
252 $ccaddr = $ccrecipient;
|
root@9
|
253 }
|
root@9
|
254 $mailer->AddCC($ccaddr, $ccname);
|
root@9
|
255 }
|
root@9
|
256 break;
|
root@9
|
257
|
root@9
|
258 case 'bcc':
|
root@9
|
259 $bccrecipients = explode(',', $value);
|
root@9
|
260 foreach ($bccrecipients as $bccrecipient) {
|
root@9
|
261 if (strpos($bccrecipient, '<') !== FALSE) {
|
root@9
|
262 $bccparts = explode(' <', $bccrecipient);
|
root@9
|
263 $bccname = $bccparts[0];
|
root@9
|
264 $bccaddr = rtrim($bccparts[1], '>');
|
root@9
|
265 }
|
root@9
|
266 else {
|
root@9
|
267 $bccname = '';
|
root@9
|
268 $bccaddr = $bccrecipient;
|
root@9
|
269 }
|
root@9
|
270 $mailer->AddBCC($bccaddr, $bccname);
|
root@9
|
271 }
|
root@9
|
272 break;
|
root@9
|
273
|
root@9
|
274 default:
|
root@9
|
275 // The header key is not special - add it as is.
|
root@9
|
276 $mailer->AddCustomHeader($key . ': ' . $value);
|
root@9
|
277 }
|
root@9
|
278 }
|
root@9
|
279
|
root@9
|
280 /**
|
root@9
|
281 * TODO
|
root@9
|
282 * Need to figure out the following.
|
root@9
|
283 *
|
root@9
|
284 * Add one last header item, but not if it has already been added.
|
root@9
|
285 * $errors_to = FALSE;
|
root@9
|
286 * foreach ($mailer->CustomHeader as $custom_header) {
|
root@9
|
287 * if ($custom_header[0] = '') {
|
root@9
|
288 * $errors_to = TRUE;
|
root@9
|
289 * }
|
root@9
|
290 * }
|
root@9
|
291 * if ($errors_to) {
|
root@9
|
292 * $mailer->AddCustomHeader('Errors-To: '. $from);
|
root@9
|
293 * }
|
root@9
|
294 */
|
root@9
|
295 // Add the message's subject.
|
root@9
|
296 $mailer->Subject = $subject;
|
root@9
|
297
|
root@9
|
298 // Processes the message's body.
|
root@9
|
299 switch ($content_type) {
|
root@9
|
300 case 'multipart/related':
|
root@9
|
301 $mailer->Body = $body;
|
root@9
|
302
|
root@9
|
303 /**
|
root@9
|
304 * TODO
|
root@9
|
305 * Firgure out if there is anything more to handling this type.
|
root@9
|
306 */
|
root@9
|
307
|
root@9
|
308 break;
|
root@9
|
309
|
root@9
|
310 case 'multipart/alternative':
|
root@9
|
311 // Split the body based on the boundary ID.
|
root@9
|
312 $body_parts = $this->_boundary_split($body, $boundary);
|
root@9
|
313 foreach ($body_parts as $body_part) {
|
root@9
|
314 // If plain/text within the body part, add it to $mailer->AltBody.
|
root@9
|
315 if (strpos($body_part, 'text/plain')) {
|
root@9
|
316 // Clean up the text.
|
root@9
|
317 $body_part = trim($this->_remove_headers(trim($body_part)));
|
root@9
|
318 // Include it as part of the mail object.
|
root@9
|
319 $mailer->AltBody = $body_part;
|
root@9
|
320 }
|
root@9
|
321 // If plain/html within the body part, add it to $mailer->Body.
|
root@9
|
322 elseif (strpos($body_part, 'text/html')) {
|
root@9
|
323 // Clean up the text.
|
root@9
|
324 $body_part = trim($this->_remove_headers(trim($body_part)));
|
root@9
|
325 // Include it as part of the mail object.
|
root@9
|
326 $mailer->Body = $body_part;
|
root@9
|
327 }
|
root@9
|
328 }
|
root@9
|
329 break;
|
root@9
|
330
|
root@9
|
331 case 'multipart/mixed':
|
root@9
|
332 // Split the body based on the boundary ID.
|
root@9
|
333 $body_parts = $this->_boundary_split($body, $boundary);
|
root@9
|
334
|
root@9
|
335 // Determine if there is an HTML part for when adding the plain text part.
|
root@9
|
336 $text_plain = FALSE;
|
root@9
|
337 $text_html = FALSE;
|
root@9
|
338 foreach ($body_parts as $body_part) {
|
root@9
|
339 if (strpos($body_part, 'text/plain')) {
|
root@9
|
340 $text_plain = TRUE;
|
root@9
|
341 }
|
root@9
|
342 if (strpos($body_part, 'text/html')) {
|
root@9
|
343 $text_html = TRUE;
|
root@9
|
344 }
|
root@9
|
345 }
|
root@9
|
346
|
root@9
|
347 foreach ($body_parts as $body_part) {
|
root@9
|
348 // If test/plain within the body part, add it to either
|
root@9
|
349 // $mailer->AltBody or $mailer->Body, depending on whether there is
|
root@9
|
350 // also a text/html part ot not.
|
root@9
|
351 if (strpos($body_part, 'multipart/alternative')) {
|
root@9
|
352 // Get boundary ID from the Content-Type header.
|
root@9
|
353 $boundary2 = $this->_get_substring($body_part, 'boundary', '"', '"');
|
root@9
|
354 // Clean up the text.
|
root@9
|
355 $body_part = trim($this->_remove_headers(trim($body_part)));
|
root@9
|
356 // Split the body based on the boundary ID.
|
root@9
|
357 $body_parts2 = $this->_boundary_split($body_part, $boundary2);
|
root@9
|
358
|
root@9
|
359 foreach ($body_parts2 as $body_part2) {
|
root@9
|
360 // If plain/text within the body part, add it to $mailer->AltBody.
|
root@9
|
361 if (strpos($body_part2, 'text/plain')) {
|
root@9
|
362 // Clean up the text.
|
root@9
|
363 $body_part2 = trim($this->_remove_headers(trim($body_part2)));
|
root@9
|
364 // Include it as part of the mail object.
|
root@9
|
365 $mailer->AltBody = $body_part2;
|
root@9
|
366 $mailer->ContentType = 'multipart/mixed';
|
root@9
|
367 }
|
root@9
|
368 // If plain/html within the body part, add it to $mailer->Body.
|
root@9
|
369 elseif (strpos($body_part2, 'text/html')) {
|
root@9
|
370 // Clean up the text.
|
root@9
|
371 $body_part2 = trim($this->_remove_headers(trim($body_part2)));
|
root@9
|
372 // Include it as part of the mail object.
|
root@9
|
373 $mailer->Body = $body_part2;
|
root@9
|
374 $mailer->ContentType = 'multipart/mixed';
|
root@9
|
375 }
|
root@9
|
376 }
|
root@9
|
377 }
|
root@9
|
378 // If text/plain within the body part, add it to $mailer->Body.
|
root@9
|
379 elseif (strpos($body_part, 'text/plain')) {
|
root@9
|
380 // Clean up the text.
|
root@9
|
381 $body_part = trim($this->_remove_headers(trim($body_part)));
|
root@9
|
382
|
root@9
|
383 if ($text_html) {
|
root@9
|
384 $mailer->AltBody = $body_part;
|
root@9
|
385 $mailer->IsHTML(TRUE);
|
root@9
|
386 $mailer->ContentType = 'multipart/mixed';
|
root@9
|
387 }
|
root@9
|
388 else {
|
root@9
|
389 $mailer->Body = $body_part;
|
root@9
|
390 $mailer->IsHTML(FALSE);
|
root@9
|
391 $mailer->ContentType = 'multipart/mixed';
|
root@9
|
392 }
|
root@9
|
393 }
|
root@9
|
394 // If text/html within the body part, add it to $mailer->Body.
|
root@9
|
395 elseif (strpos($body_part, 'text/html')) {
|
root@9
|
396 // Clean up the text.
|
root@9
|
397 $body_part = trim($this->_remove_headers(trim($body_part)));
|
root@9
|
398 // Include it as part of the mail object.
|
root@9
|
399 $mailer->Body = $body_part;
|
root@9
|
400 $mailer->IsHTML(TRUE);
|
root@9
|
401 $mailer->ContentType = 'multipart/mixed';
|
root@9
|
402 }
|
root@9
|
403 // Add the attachment.
|
root@9
|
404 elseif (strpos($body_part, 'Content-Disposition: attachment;')) {
|
root@9
|
405 $file_path = $this->_get_substring($body_part, 'filename=', '"', '"');
|
root@9
|
406 $file_name = $this->_get_substring($body_part, ' name=', '"', '"');
|
root@9
|
407 $file_encoding = $this->_get_substring($body_part, 'Content-Transfer-Encoding', ' ', "\n");
|
root@9
|
408 $file_type = $this->_get_substring($body_part, 'Content-Type', ' ', ';');
|
root@9
|
409
|
root@9
|
410 if (file_exists($file_path)) {
|
root@9
|
411 if (!$mailer->AddAttachment($file_path, $file_name, $file_encoding, $file_type)) {
|
root@9
|
412 drupal_set_message(t('Attahment could not be found or accessed.'));
|
root@9
|
413 }
|
root@9
|
414 }
|
root@9
|
415 else {
|
root@9
|
416 // Clean up the text.
|
root@9
|
417 $body_part = trim($this->_remove_headers(trim($body_part)));
|
root@9
|
418
|
root@9
|
419 if (drupal_strtolower($file_encoding) == 'base64') {
|
root@9
|
420 $attachment = base64_decode($body_part);
|
root@9
|
421 }
|
root@9
|
422 elseif (drupal_strtolower($file_encoding) == 'quoted-printable') {
|
root@9
|
423 $attachment = quoted_printable_decode($body_part);
|
root@9
|
424 }
|
root@9
|
425 else {
|
root@9
|
426 $attachment = $body_part;
|
root@9
|
427 }
|
root@9
|
428
|
root@9
|
429 $attachment_new_filename = drupal_tempnam('temporary://', 'smtp');
|
root@9
|
430 $file_path = file_save_data($attachment, $attachment_new_filename, FILE_EXISTS_REPLACE);
|
root@9
|
431 $real_path = drupal_realpath($file_path->uri);
|
root@9
|
432
|
root@9
|
433 if (!$mailer->AddAttachment($real_path, $file_name)) {
|
root@9
|
434 drupal_set_message(t('Attachment could not be found or accessed.'));
|
root@9
|
435 }
|
root@9
|
436 }
|
root@9
|
437 }
|
root@9
|
438 }
|
root@9
|
439 break;
|
root@9
|
440
|
root@9
|
441 default:
|
root@9
|
442 $mailer->Body = $body;
|
root@9
|
443 break;
|
root@9
|
444 }
|
root@9
|
445
|
root@9
|
446 // Process mimemail attachments
|
root@9
|
447 if (isset($message['params']['attachments'])) {
|
root@9
|
448 foreach ($message['params']['attachments'] as $attachment) {
|
root@9
|
449 if (isset($attachment['filecontent'])) {
|
root@9
|
450 $mailer->AddStringAttachment($attachment['filecontent'], $attachment['filename'], 'base64', $attachment['filemime']);
|
root@9
|
451 }
|
root@9
|
452 if (isset($attachment['filepath'])) {
|
root@9
|
453 $mailer->AddAttachment($attachment['filepath'], $attachment['filename'], 'base64', $attachment['filemime']);
|
root@9
|
454 }
|
root@9
|
455 }
|
root@9
|
456 }
|
root@9
|
457
|
root@9
|
458 // Set the authentication settings.
|
root@9
|
459 $username = variable_get('smtp_username', '');
|
root@9
|
460 $password = variable_get('smtp_password', '');
|
root@9
|
461
|
root@9
|
462 // If username and password are given, use SMTP authentication.
|
root@9
|
463 if ($username != '' && $password != '') {
|
root@9
|
464 $mailer->SMTPAuth = TRUE;
|
root@9
|
465 $mailer->Username = $username;
|
root@9
|
466 $mailer->Password = $password;
|
root@9
|
467 }
|
root@9
|
468
|
root@9
|
469
|
root@9
|
470 // Set the protocol prefix for the smtp host.
|
root@9
|
471 switch (variable_get('smtp_protocol', 'standard')) {
|
root@9
|
472 case 'ssl':
|
root@9
|
473 $mailer->SMTPSecure = 'ssl';
|
root@9
|
474 break;
|
root@9
|
475
|
root@9
|
476 case 'tls':
|
root@9
|
477 $mailer->SMTPSecure = 'tls';
|
root@9
|
478 break;
|
root@9
|
479
|
root@9
|
480 default:
|
root@9
|
481 $mailer->SMTPSecure = '';
|
root@9
|
482 }
|
root@9
|
483
|
root@9
|
484
|
root@9
|
485 // Set other connection settings.
|
root@9
|
486 $mailer->Host = variable_get('smtp_host', '') . ';' . variable_get('smtp_hostbackup', '');
|
root@9
|
487 $mailer->Port = variable_get('smtp_port', '25');
|
root@9
|
488 $mailer->Mailer = 'smtp';
|
root@9
|
489
|
root@9
|
490 // Let the people know what is going on.
|
root@9
|
491 watchdog('smtp', 'Sending mail to: @to', array('@to' => $to));
|
root@9
|
492
|
root@9
|
493 // Try to send e-mail. If it fails, set watchdog entry.
|
root@9
|
494 if (!$mailer->Send()) {
|
root@9
|
495 watchdog('smtp', 'Error sending e-mail from @from to @to : !error_message', array('@from' => $from, '@to' => $to, '!error_message' => $mailer->ErrorInfo), WATCHDOG_ERROR);
|
root@9
|
496 return FALSE;
|
root@9
|
497 }
|
root@9
|
498
|
root@9
|
499 $mailer->SmtpClose();
|
root@9
|
500 return TRUE;
|
root@9
|
501 }
|
root@9
|
502
|
root@9
|
503 /**
|
root@9
|
504 * Splits the input into parts based on the given boundary.
|
root@9
|
505 *
|
root@9
|
506 * Swiped from Mail::MimeDecode, with modifications based on Drupal's coding
|
root@9
|
507 * standards and this bug report: http://pear.php.net/bugs/bug.php?id=6495
|
root@9
|
508 *
|
root@9
|
509 * @param input
|
root@9
|
510 * A string containing the body text to parse.
|
root@9
|
511 * @param boundary
|
root@9
|
512 * A string with the boundary string to parse on.
|
root@9
|
513 * @return
|
root@9
|
514 * An array containing the resulting mime parts
|
root@9
|
515 */
|
root@9
|
516 protected function _boundary_split($input, $boundary) {
|
root@9
|
517 $parts = array();
|
root@9
|
518 $bs_possible = drupal_substr($boundary, 2, -2);
|
root@9
|
519 $bs_check = '\"' . $bs_possible . '\"';
|
root@9
|
520
|
root@9
|
521 if ($boundary == $bs_check) {
|
root@9
|
522 $boundary = $bs_possible;
|
root@9
|
523 }
|
root@9
|
524
|
root@9
|
525 $tmp = explode('--' . $boundary, $input);
|
root@9
|
526
|
root@9
|
527 for ($i = 1; $i < count($tmp); $i++) {
|
root@9
|
528 if (trim($tmp[$i])) {
|
root@9
|
529 $parts[] = $tmp[$i];
|
root@9
|
530 }
|
root@9
|
531 }
|
root@9
|
532
|
root@9
|
533 return $parts;
|
root@9
|
534 } // End of _smtp_boundary_split().
|
root@9
|
535
|
root@9
|
536 /**
|
root@9
|
537 * Strips the headers from the body part.
|
root@9
|
538 *
|
root@9
|
539 * @param input
|
root@9
|
540 * A string containing the body part to strip.
|
root@9
|
541 * @return
|
root@9
|
542 * A string with the stripped body part.
|
root@9
|
543 */
|
root@9
|
544 protected function _remove_headers($input) {
|
root@9
|
545 $part_array = explode("\n", $input);
|
root@9
|
546
|
root@9
|
547 // will strip these headers according to RFC2045
|
root@9
|
548 $headers_to_strip = array( 'Content-Type', 'Content-Transfer-Encoding', 'Content-ID', 'Content-Disposition');
|
root@9
|
549 $pattern = '/^(' . implode('|', $headers_to_strip) . '):/';
|
root@9
|
550
|
root@9
|
551 while (count($part_array) > 0) {
|
root@9
|
552
|
root@9
|
553 // ignore trailing spaces/newlines
|
root@9
|
554 $line = rtrim($part_array[0]);
|
root@9
|
555
|
root@9
|
556 // if the line starts with a known header string
|
root@9
|
557 if (preg_match($pattern, $line)) {
|
root@9
|
558 $line = rtrim(array_shift($part_array));
|
root@9
|
559 // remove line containing matched header.
|
root@9
|
560
|
root@9
|
561 // if line ends in a ';' and the next line starts with four spaces, it's a continuation
|
root@9
|
562 // of the header split onto the next line. Continue removing lines while we have this condition.
|
root@9
|
563 while (substr($line, -1) == ';' && count($part_array) > 0 && substr($part_array[0], 0, 4) == ' ') {
|
root@9
|
564 $line = rtrim(array_shift($part_array));
|
root@9
|
565 }
|
root@9
|
566 }
|
root@9
|
567 else {
|
root@9
|
568 // no match header, must be past headers; stop searching.
|
root@9
|
569 break;
|
root@9
|
570 }
|
root@9
|
571 }
|
root@9
|
572
|
root@9
|
573 $output = implode("\n", $part_array);
|
root@9
|
574 return $output;
|
root@9
|
575 } // End of _smtp_remove_headers().
|
root@9
|
576
|
root@9
|
577 /**
|
root@9
|
578 * Returns a string that is contained within another string.
|
root@9
|
579 *
|
root@9
|
580 * Returns the string from within $source that is some where after $target
|
root@9
|
581 * and is between $beginning_character and $ending_character.
|
root@9
|
582 *
|
root@9
|
583 * @param $source
|
root@9
|
584 * A string containing the text to look through.
|
root@9
|
585 * @param $target
|
root@9
|
586 * A string containing the text in $source to start looking from.
|
root@9
|
587 * @param $beginning_character
|
root@9
|
588 * A string containing the character just before the sought after text.
|
root@9
|
589 * @param $ending_character
|
root@9
|
590 * A string containing the character just after the sought after text.
|
root@9
|
591 * @return
|
root@9
|
592 * A string with the text found between the $beginning_character and the
|
root@9
|
593 * $ending_character.
|
root@9
|
594 */
|
root@9
|
595 protected function _get_substring($source, $target, $beginning_character, $ending_character) {
|
root@9
|
596 $search_start = strpos($source, $target) + 1;
|
root@9
|
597 $first_character = strpos($source, $beginning_character, $search_start) + 1;
|
root@9
|
598 $second_character = strpos($source, $ending_character, $first_character) + 1;
|
root@9
|
599 $substring = drupal_substr($source, $first_character, $second_character - $first_character);
|
root@9
|
600 $string_length = drupal_strlen($substring) - 1;
|
root@9
|
601
|
root@9
|
602 if ($substring[$string_length] == $ending_character) {
|
root@9
|
603 $substring = drupal_substr($substring, 0, $string_length);
|
root@9
|
604 }
|
root@9
|
605
|
root@9
|
606 return $substring;
|
root@9
|
607 } // End of _smtp_get_substring().
|
root@9
|
608 }
|