| 1 |
<?php |
<?php |
| 2 |
// $Id: smtp.module,v 1.17.2.6 2008/07/12 16:58:28 oadaeh Exp $ |
// $Id: smtp.module,v 1.17.2.7 2008/07/17 14:58:08 oadaeh Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 175 |
$params['subject'] = t('Drupal test e-mail'); |
$params['subject'] = t('Drupal test e-mail'); |
| 176 |
$params['body'] = t('If you receive this message it means your site is capable of sending e-mail.'); |
$params['body'] = t('If you receive this message it means your site is capable of sending e-mail.'); |
| 177 |
drupal_mail('smtp', 'smtp-test', $test_address, $language, $params); |
drupal_mail('smtp', 'smtp-test', $test_address, $language, $params); |
| 178 |
drupal_set_message(t('A test e-mail has been sent to @email. You may want to !check for any error messages.', array('@email' => $test_address, '!check' => l(t('check the logs'), 'admin/reports/watchdog')))); |
drupal_set_message(t('A test e-mail has been sent to @email. You may want to !check for any error messages.', array('@email' => $test_address, '!check' => l(t('check the logs'), 'admin/reports/dblog')))); |
| 179 |
} |
} |
| 180 |
$form['email_test'] = array( |
$form['email_test'] = array( |
| 181 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 231 |
* An array with at least the following elements: id, to, subject, body and |
* An array with at least the following elements: id, to, subject, body and |
| 232 |
* headers. |
* headers. |
| 233 |
* |
* |
| 234 |
* @see http://api.drupal.org/api/function/drupal_mail_send/6 |
* @see drupal_mail_send() |
| 235 |
*/ |
*/ |
| 236 |
function drupal_mail_wrapper($message) { |
function drupal_mail_wrapper($message) { |
| 237 |
$id = $message['id']; |
$id = $message['id']; |
| 238 |
$to = $message['to']; |
$to = $message['to']; |
| 239 |
$from = $message['from']; |
$from = $message['from']; |
| 240 |
$header = $message['headers']; |
$language = $message['language']; |
| 241 |
$subject = $message['subject']; |
$subject = $message['subject']; |
| 242 |
$body = $message['body']; |
$body = $message['body']; |
| 243 |
|
$headers = $message['headers']; |
| 244 |
|
|
| 245 |
// Include the PHPMailer class (which includes the SMTP class). |
// Include the PHPMailer class (which includes the SMTP class). |
| 246 |
require_once(drupal_get_path('module', 'smtp') .'/phpmailer/class.phpmailer.php'); |
require_once(drupal_get_path('module', 'smtp') .'/phpmailer/class.phpmailer.php'); |
| 248 |
// Create a new PHPMailer object. |
// Create a new PHPMailer object. |
| 249 |
$mail = new PHPMailer(); |
$mail = new PHPMailer(); |
| 250 |
|
|
| 251 |
global $language; |
|
| 252 |
if ($language) { |
// Set the language PHPMailer is to use. |
| 253 |
|
if (!$language) { |
| 254 |
|
global $language; |
| 255 |
|
if ($language) { |
| 256 |
|
$mail->SetLanguage($language->language, drupal_get_path('module', 'smtp') .'/phpmailer/language/'); |
| 257 |
|
} |
| 258 |
|
} |
| 259 |
|
else { |
| 260 |
$mail->SetLanguage($language->language, drupal_get_path('module', 'smtp') .'/phpmailer/language/'); |
$mail->SetLanguage($language->language, drupal_get_path('module', 'smtp') .'/phpmailer/language/'); |
| 261 |
} |
} |
| 262 |
|
|
| 263 |
|
|
| 264 |
|
// Turn in debugging, if requested. |
| 265 |
if (variable_get('smtp_debugging', 0) == 1) { |
if (variable_get('smtp_debugging', 0) == 1) { |
| 266 |
$mail->SMTPDebug = TRUE; |
$mail->SMTPDebug = TRUE; |
| 267 |
} |
} |
| 268 |
|
|
|
$username = variable_get('smtp_username', ''); |
|
|
$password = variable_get('smtp_password', ''); |
|
| 269 |
|
|
| 270 |
// Set the default name e-mails should be from. |
// Set the from name and e-mail address. |
|
// If value is not defined in settings, use site_name. |
|
| 271 |
if (variable_get('smtp_fromname', '') != '') { |
if (variable_get('smtp_fromname', '') != '') { |
| 272 |
$from_name = variable_get('smtp_fromname', ''); |
$from_name = variable_get('smtp_fromname', ''); |
| 273 |
} |
} |
| 274 |
else { |
else { |
| 275 |
// Blank value will let the e-mail address appear. |
// If value is not defined in settings, use site_name. |
| 276 |
$from_name = variable_get('site_name', ''); |
$from_name = variable_get('site_name', ''); |
| 277 |
} |
} |
| 278 |
|
|
| 279 |
// If from e-mail address is blank, use smtp_from config option. |
// Blank value will let the e-mail address appear. |
| 280 |
|
|
| 281 |
|
if ($from == NULL || $from == '') { |
| 282 |
|
// If from e-mail address is blank, use smtp_from config option. |
| 283 |
|
if ($from = variable_get('smtp_from', '') == '') { |
| 284 |
|
// If smtp_from config option is blank, use site_email. |
| 285 |
|
if ($from = variable_get('site_email', '') == '') { |
| 286 |
|
drupal_set_message(t('There is no submitted from address.'), 'error'); |
| 287 |
|
watchdog('smtp', 'There is no submitted from address.', array(), WATCHDOG_ERROR); |
| 288 |
|
return FALSE; |
| 289 |
|
} |
| 290 |
|
} |
| 291 |
|
} |
| 292 |
|
/* |
| 293 |
if ($from == NULL || $from == '') { |
if ($from == NULL || $from == '') { |
| 294 |
if (variable_get('smtp_from', '') != '') { |
if (variable_get('smtp_from', '') != '') { |
| 295 |
$from = variable_get('smtp_from', ''); |
$from = variable_get('smtp_from', ''); |
| 299 |
$from = variable_get('site_email', ''); |
$from = variable_get('site_email', ''); |
| 300 |
} |
} |
| 301 |
} |
} |
| 302 |
|
*/ |
| 303 |
if (preg_match('/^".*"\s*<.*>$/', $from)) { |
if (preg_match('/^".*"\s*<.*>$/', $from)) { |
| 304 |
|
// . == Matches any single character except line break characters \r and \n. |
| 305 |
|
// * == Repeats the previous item zero or more times. |
| 306 |
$from_name = preg_replace('/"(.*)"(.*)/i', '$1', $from); // It gives: Name |
$from_name = preg_replace('/"(.*)"(.*)/i', '$1', $from); // It gives: Name |
| 307 |
$from = preg_replace("/(.*)\<(.*)\>/i", '$2', $from); // It gives: name@domain.tld |
$from = preg_replace("/(.*)\<(.*)\>/i", '$2', $from); // It gives: name@domain.tld |
| 308 |
} |
} |
| 309 |
else if (!valid_email_address($from)) { |
elseif (!valid_email_address($from)) { |
| 310 |
$from_error_message = t('The submitted from address (@from) is not valid.', array('@from' => $from)); |
drupal_set_message(t('The submitted from address (@from) is not valid.', array('@from' => $from)), 'error'); |
| 311 |
drupal_set_message($from_error_message, 'error'); |
watchdog('smtp', 'The submitted from address (@from) is not valid.', array('@from' => $from), WATCHDOG_ERROR); |
| 312 |
watchdog('smtp', $from_error_message, WATCHDOG_ERROR); |
return FALSE; |
|
return false; |
|
| 313 |
} |
} |
| 314 |
|
|
| 315 |
// Defines the From value to what we expect. |
// Defines the From value to what we expect. |
|
// This should be done correctly by PHPMailer now. |
|
|
// $mail->From = '"'. $from_name .'" <'. $from .'>'; |
|
| 316 |
$mail->From = $from; |
$mail->From = $from; |
| 317 |
$mail->FromName = $from_name; |
$mail->FromName = $from_name; |
| 318 |
$mail->Sender = $from; |
$mail->Sender = $from; |
| 319 |
|
|
| 320 |
// Decide whether to use SMTP Authorization. |
|
| 321 |
if ($username != '' and $password != '') { |
// Create the list of 'To:' recipients. |
| 322 |
// Do so if username and password are given. |
$torecipients = split(',', $to); |
| 323 |
$auth = TRUE; |
foreach ($torecipients as $torecipient) { |
| 324 |
} |
if (strpos($torecipient, '<') !== FALSE) { |
| 325 |
else { |
$toparts = explode(' <', $torecipient); |
| 326 |
$auth = FALSE; |
$toname = $toparts[0]; |
| 327 |
|
$toaddr = rtrim($toparts[1], '>'); |
| 328 |
|
} |
| 329 |
|
else { |
| 330 |
|
$toname = ''; |
| 331 |
|
$toaddr = $torecipient; |
| 332 |
|
} |
| 333 |
|
$mail->AddAddress($toaddr, $toname); |
| 334 |
} |
} |
| 335 |
|
|
|
// Force the initial value to be set to text/plain. |
|
|
$mail->IsHTML(FALSE); |
|
| 336 |
|
|
| 337 |
// Take care of the e-mail headers. |
// Parse the headers of the message and set the PHPMailer object's settings |
| 338 |
foreach ($header as $key => $value) { |
// accordingly. |
| 339 |
|
foreach ($headers as $key => $value) { |
| 340 |
//watchdog('error', 'Key: ' . $key . ' Value: ' . $value); |
//watchdog('error', 'Key: ' . $key . ' Value: ' . $value); |
| 341 |
if (drupal_strtolower($key) == 'from') { |
switch (drupal_strtolower($key)) { |
| 342 |
if ($from == NULL or $from == '') { |
case 'from': |
| 343 |
// If a from value was already given, then set based on header. |
if ($from == NULL or $from == '') { |
| 344 |
// Should be the most common situation since drupal_mail moves the |
// If a from value was already given, then set based on header. |
| 345 |
// from to headers. |
// Should be the most common situation since drupal_mail moves the |
| 346 |
$from = $value; |
// from to headers. |
| 347 |
$mail->From = $value; |
$from = $value; |
| 348 |
// then from can be out of sync with from_name ! |
$mail->From = $value; |
| 349 |
$mail->FromName = ''; |
// then from can be out of sync with from_name ! |
| 350 |
$mail->Sender = $value; |
$mail->FromName = ''; |
| 351 |
} |
$mail->Sender = $value; |
| 352 |
|
} |
| 353 |
|
break; |
| 354 |
|
|
| 355 |
|
case 'content-type': |
| 356 |
|
if (strpos(drupal_strtolower($value), 'text/plain') !== FALSE) { |
| 357 |
|
// The message includes only a plain text part. |
| 358 |
|
$mail->IsHTML(FALSE); |
| 359 |
|
|
| 360 |
|
// Set the charset based on the provided value, if there is one. |
| 361 |
|
$charset = _smtp_get_substring($value, 'charset', '=', ';'); |
| 362 |
|
if ($charset) { |
| 363 |
|
$mail->CharSet = $charset; |
| 364 |
|
} |
| 365 |
|
|
| 366 |
|
$content_type = 'text/plain'; |
| 367 |
|
} |
| 368 |
|
elseif (strpos(drupal_strtolower($value), 'text/html') !== FALSE) { |
| 369 |
|
// The message includes only an HTML part. |
| 370 |
|
$mail->IsHTML(TRUE); |
| 371 |
|
$content_type = 'text/html'; |
| 372 |
|
} |
| 373 |
|
elseif (strpos(drupal_strtolower($value), 'multipart/related') !== FALSE) { |
| 374 |
|
// The message includes an HTML part w/inline attachments. |
| 375 |
|
$mail->ContentType = $content_type = 'multipart/related'; |
| 376 |
|
} |
| 377 |
|
elseif (strpos(drupal_strtolower($value), 'multipart/alternative') !== FALSE) { |
| 378 |
|
// The message includes both a plain text and an HTML part. |
| 379 |
|
$mail->ContentType = $content_type = 'multipart/alternative'; |
| 380 |
|
|
| 381 |
|
// Get the boundary ID from the Content-Type header. |
| 382 |
|
$boundary = _smtp_get_substring($value, 'boundary', '"', '"'); |
| 383 |
|
} |
| 384 |
|
elseif (strpos(drupal_strtolower($value), 'multipart/mixed') !== FALSE) { |
| 385 |
|
// The message includes one or more attachments. |
| 386 |
|
$mail->ContentType = $content_type = 'multipart/mixed'; |
| 387 |
|
|
| 388 |
|
// Get the boundary ID from the Content-Type header. |
| 389 |
|
$boundary = _smtp_get_substring($value, 'boundary', '"', '"'); |
| 390 |
|
} |
| 391 |
|
else { |
| 392 |
|
// Everything else is unsuppored by PHPMailer. |
| 393 |
|
drupal_set_message(t('The Content-Type of your message is not supported by PHPMailer and will be sent as text/plain instead.'), 'error'); |
| 394 |
|
watchdog('smtp', 'The Content-Type of your message is not supported by PHPMailer and will be sent as text/plain instead.', array(), WATCHDOG_ERROR); |
| 395 |
|
|
| 396 |
|
// Force the Content-Type to be text/plain. |
| 397 |
|
$mail->IsHTML(FALSE); |
| 398 |
|
$content_type = 'text/plain'; |
| 399 |
|
} |
| 400 |
|
break; |
| 401 |
|
|
| 402 |
|
case 'reply-to': |
| 403 |
|
// Only add a "reply-to" if it's not the same as "return-path". |
| 404 |
|
if ($value != $headers['Return-Path']) { |
| 405 |
|
$mail->AddReplyTo($value); |
| 406 |
|
} |
| 407 |
|
break; |
| 408 |
|
|
| 409 |
|
case 'content-transfer-encoding': |
| 410 |
|
$mail->Encoding = $value; |
| 411 |
|
break; |
| 412 |
|
|
| 413 |
|
case 'return-path': |
| 414 |
|
case 'mime-version': |
| 415 |
|
case 'x-mailer': |
| 416 |
|
// Let PHPMailer specify these. |
| 417 |
|
break; |
| 418 |
|
|
| 419 |
|
case 'errors-to': |
| 420 |
|
$mail->AddCustomHeader('Errors-To: '. $value); |
| 421 |
|
break; |
| 422 |
|
|
| 423 |
|
case 'cc': |
| 424 |
|
$ccrecipients = split(',', $value); |
| 425 |
|
foreach ($ccrecipients as $ccrecipient) { |
| 426 |
|
if (strpos($ccrecipient, '<') !== FALSE) { |
| 427 |
|
$ccparts = explode(' <', $ccrecipient); |
| 428 |
|
$ccname = $ccparts[0]; |
| 429 |
|
$ccaddr = rtrim($ccparts[1], '>'); |
| 430 |
|
} |
| 431 |
|
else { |
| 432 |
|
$ccname = ''; |
| 433 |
|
$ccaddr = $ccrecipient; |
| 434 |
|
} |
| 435 |
|
$mail->AddBCC($ccaddr, $ccname); |
| 436 |
|
} |
| 437 |
|
break; |
| 438 |
|
|
| 439 |
|
case 'bcc': |
| 440 |
|
$bccrecipients = split(',', $value); |
| 441 |
|
foreach ($bccrecipients as $bccrecipient) { |
| 442 |
|
if (strpos($bccrecipient, '<') !== FALSE) { |
| 443 |
|
$bccparts = explode(' <', $bccrecipient); |
| 444 |
|
$bccname = $bccparts[0]; |
| 445 |
|
$bccaddr = rtrim($bccparts[1], '>'); |
| 446 |
|
} |
| 447 |
|
else { |
| 448 |
|
$bccname = ''; |
| 449 |
|
$bccaddr = $bccrecipient; |
| 450 |
|
} |
| 451 |
|
$mail->AddBCC($bccaddr, $bccname); |
| 452 |
|
} |
| 453 |
|
break; |
| 454 |
|
|
| 455 |
|
default: |
| 456 |
|
// The header key is not special - add it as is. |
| 457 |
|
$mail->AddCustomHeader($key .': '. $value); |
| 458 |
} |
} |
| 459 |
else if (drupal_strtolower($key) == 'content-type' && strpos(drupal_strtolower($value), 'text/html') !== FALSE) { |
} |
| 460 |
$mail->IsHTML(TRUE); |
|
| 461 |
|
|
| 462 |
|
/** |
| 463 |
|
* TODO |
| 464 |
|
* Need to figure out the following. |
| 465 |
|
|
| 466 |
|
// Add one last header item, but not if it has already been added. |
| 467 |
|
$errors_to = FALSE; |
| 468 |
|
foreach ($mail->CustomHeader as $custom_header) { |
| 469 |
|
if ($custom_header[0] = '') { |
| 470 |
|
$errors_to = TRUE; |
| 471 |
} |
} |
| 472 |
else if (drupal_strtolower($key) == 'content-type' && strpos(drupal_strtolower($value), 'multipart/mixed') !== FALSE) { |
} |
| 473 |
// $body passed to smtp should already be formatted. Add multipart |
if ($errors_to) { |
| 474 |
// header and tell phpmailer to leave it alone |
$mail->AddCustomHeader('Errors-To: '. $from); |
| 475 |
$mail->AddCustomHeader($key .': '. $value); |
} |
| 476 |
$mail->message_type = "pre"; |
*/ |
| 477 |
$mail->ContentType = 'multipart/mixed'; |
|
| 478 |
} |
|
| 479 |
else if (drupal_strtolower($key) == 'reply-to') { |
// Add the message's subject. |
| 480 |
// Only add a "reply-to" if it's not the same as "return-path". |
$mail->Subject = $subject; |
| 481 |
if ($value != $header['Return-Path']) { |
|
| 482 |
$mail->AddReplyTo($value); |
|
| 483 |
|
// Processes the message's body. |
| 484 |
|
switch ($content_type) { |
| 485 |
|
case 'multipart/related': |
| 486 |
|
$mail->Body = $body; |
| 487 |
|
|
| 488 |
|
/** |
| 489 |
|
* TODO |
| 490 |
|
* Firgure out if there is anything more to handling this type. |
| 491 |
|
*/ |
| 492 |
|
|
| 493 |
|
break; |
| 494 |
|
|
| 495 |
|
case 'multipart/alternative': |
| 496 |
|
// Split the body based on the boundary ID. |
| 497 |
|
$body_parts = _smtp_boundary_split($body, $boundary); |
| 498 |
|
foreach ($body_parts as $body_part) { |
| 499 |
|
// If plain/text within the body part, add it to $mail->AltBody. |
| 500 |
|
if (strpos($body_part, 'text/plain')) { |
| 501 |
|
// Clean up the text. |
| 502 |
|
$body_part = trim(_smtp_remove_headers(trim($body_part))); |
| 503 |
|
// Include it as part of the mail object. |
| 504 |
|
$mail->AltBody = $body_part; |
| 505 |
|
} |
| 506 |
|
// If plain/html within the body part, add it to $mail->Body. |
| 507 |
|
elseif (strpos($body_part, 'text/html')) { |
| 508 |
|
// Clean up the text. |
| 509 |
|
$body_part = trim(_smtp_remove_headers(trim($body_part))); |
| 510 |
|
// Include it as part of the mail object. |
| 511 |
|
$mail->Body = $body_part; |
| 512 |
|
} |
| 513 |
} |
} |
| 514 |
} |
break; |
| 515 |
else if (drupal_strtolower($key) == 'return-path') { |
|
| 516 |
if (trim($value) != '') { |
case 'multipart/mixed': |
| 517 |
// This is be set by SmtpSend() |
// Split the body based on the boundary ID. |
| 518 |
// $mail->Sender = $value; |
$body_parts = _smtp_boundary_split($body, $boundary); |
| 519 |
|
|
| 520 |
|
// Determine if there is an HTML part for when adding the plain text part. |
| 521 |
|
$text_plain = FALSE; |
| 522 |
|
$text_html = FALSE; |
| 523 |
|
foreach ($body_parts as $body_part) { |
| 524 |
|
if (strpos($body_part, 'text/plain')) { |
| 525 |
|
$text_plain = TRUE; |
| 526 |
|
} |
| 527 |
|
if (strpos($body_part, 'text/html')) { |
| 528 |
|
$text_html = TRUE; |
| 529 |
|
} |
| 530 |
} |
} |
| 531 |
} |
|
| 532 |
else if (drupal_strtolower($key) == 'content-transfer-encoding') { |
foreach ($body_parts as $body_part) { |
| 533 |
$mail->Encoding = $value; |
// If test/plain within the body part, add it to either |
| 534 |
} |
// $mail->AltBody or $mail->Body, depending on whether there is |
| 535 |
else if (drupal_strtolower($key) == 'mime-version') { |
// also a text/html part ot not. |
| 536 |
// Ommit MIME-Version, since it will be set by PHPMailer. |
if (strpos($body_part, 'multipart/alternative')) { |
| 537 |
} |
// Clean up the text. |
| 538 |
else if (drupal_strtolower($key) == 'x-mailer') { |
$body_part = trim(_smtp_remove_headers(trim($body_part))); |
| 539 |
// Ommit X-Mailer, since it will be set by PHPMailer. |
// Get boundary ID from the Content-Type header. |
| 540 |
} |
$boundary2 = _smtp_get_substring($body_part, 'boundary', '"', '"'); |
| 541 |
else if (drupal_strtolower($key) == 'errors-to') { |
// Split the body based on the boundary ID. |
| 542 |
// Prefer to keep control on this one, it will be set by PHPMailer. |
$body_parts2 = _smtp_boundary_split($body_part, $boundary2); |
| 543 |
} |
|
| 544 |
else if (drupal_strtolower($key) == 'bcc') { |
foreach ($body_parts2 as $body_part2) { |
| 545 |
$bccrecipients = split(",", $value); |
// If plain/text within the body part, add it to $mail->AltBody. |
| 546 |
foreach ($bccrecipients as $bccrecipient) { |
if (strpos($body_part2, 'text/plain')) { |
| 547 |
if (strpos($bccrecipient, '<') !== false) { |
// Clean up the text. |
| 548 |
$bccparts = explode(" <", $bccrecipient); |
$body_part2 = trim(_smtp_remove_headers(trim($body_part2))); |
| 549 |
$bccname = $bccparts[0]; |
// Include it as part of the mail object. |
| 550 |
$bccaddr = rtrim($bccparts[1], ">"); |
$mail->AltBody = $body_part2; |
| 551 |
|
$mail->ContentType = 'multipart/mixed'; |
| 552 |
|
} |
| 553 |
|
// If plain/html within the body part, add it to $mail->Body. |
| 554 |
|
elseif (strpos($body_part2, 'text/html')) { |
| 555 |
|
// Clean up the text. |
| 556 |
|
$body_part2 = trim(_smtp_remove_headers(trim($body_part2))); |
| 557 |
|
// Include it as part of the mail object. |
| 558 |
|
$mail->Body = $body_part2; |
| 559 |
|
$mail->ContentType = 'multipart/mixed'; |
| 560 |
|
} |
| 561 |
|
} |
| 562 |
} |
} |
| 563 |
else { |
// If text/plain within the body part, add it to $mail->Body. |
| 564 |
$bccname = ""; |
elseif (strpos($body_part, 'text/plain')) { |
| 565 |
$bccaddr = $bccrecipient; |
// Clean up the text. |
| 566 |
|
$body_part = trim(_smtp_remove_headers(trim($body_part))); |
| 567 |
|
|
| 568 |
|
if ($text_html) { |
| 569 |
|
$mail->AltBody = $body_part; |
| 570 |
|
$mail->IsHTML(TRUE); |
| 571 |
|
$mail->ContentType = 'multipart/mixed'; |
| 572 |
|
} |
| 573 |
|
else { |
| 574 |
|
$mail->Body = $body_part; |
| 575 |
|
$mail->IsHTML(FALSE); |
| 576 |
|
$mail->ContentType = 'multipart/mixed'; |
| 577 |
|
} |
| 578 |
|
} |
| 579 |
|
// If text/html within the body part, add it to $mail->Body. |
| 580 |
|
elseif (strpos($body_part, 'text/html')) { |
| 581 |
|
// Clean up the text. |
| 582 |
|
$body_part = trim(_smtp_remove_headers(trim($body_part))); |
| 583 |
|
// Include it as part of the mail object. |
| 584 |
|
$mail->Body = $body_part; |
| 585 |
|
$mail->IsHTML(TRUE); |
| 586 |
|
$mail->ContentType = 'multipart/mixed'; |
| 587 |
|
} |
| 588 |
|
// Add the attachment. |
| 589 |
|
elseif (strpos($body_part, 'Content-Disposition: attachment;')) { |
| 590 |
|
$file_path = _smtp_get_substring($body_part, 'filename=', '"', '"'); |
| 591 |
|
$file_name = _smtp_get_substring($body_part, ' name=', '"', '"'); |
| 592 |
|
$file_encoding = _smtp_get_substring($body_part, 'Content-Transfer-Encoding', ' ', "\n"); |
| 593 |
|
$file_type = _smtp_get_substring($body_part, 'Content-Type', ' ', ';'); |
| 594 |
|
|
| 595 |
|
if (file_exists($file_path)) { |
| 596 |
|
if (!$mail->AddAttachment($file_path, $file_name, $file_encoding, $filetype)) { |
| 597 |
|
drupal_set_message('Attahment could not be found or accessed.'); |
| 598 |
|
} |
| 599 |
|
} |
| 600 |
|
else { |
| 601 |
|
// Clean up the text. |
| 602 |
|
$body_part = trim(_smtp_remove_headers(trim($body_part))); |
| 603 |
|
|
| 604 |
|
if (drupal_strtolower($file_encoding) == 'base64') { |
| 605 |
|
$attachment = base64_decode($body_part); |
| 606 |
|
} |
| 607 |
|
elseif (drupal_strtolower($file_encoding) == 'quoted-printable') { |
| 608 |
|
$attachment = quoted_printable_decode($body_part); |
| 609 |
|
} |
| 610 |
|
else { |
| 611 |
|
$attachment = $body_part; |
| 612 |
|
} |
| 613 |
|
|
| 614 |
|
$attachment_new_filename = tempnam(realpath(file_directory_temp()), 'smtp'); |
| 615 |
|
$file_path = file_save_data($attachment, $attachment_new_filename, FILE_EXISTS_RENAME); |
| 616 |
|
|
| 617 |
|
if (!$mail->AddAttachment($file_path, $file_name)) { // , $file_encoding, $filetype); |
| 618 |
|
drupal_set_message('Attachment could not be found or accessed.'); |
| 619 |
|
} |
| 620 |
|
} |
| 621 |
} |
} |
|
$mail->AddBCC($bccaddr, $bccname); |
|
| 622 |
} |
} |
| 623 |
} |
break; |
| 624 |
// Else the header key is not special. |
|
| 625 |
else { |
default: |
| 626 |
// Add header line. |
$mail->Body = $body; |
| 627 |
$mail->AddCustomHeader($key .': '. $value); |
break; |
| 628 |
} |
} |
| 629 |
|
|
| 630 |
|
|
| 631 |
|
// Set the authentication settings. |
| 632 |
|
$username = variable_get('smtp_username', ''); |
| 633 |
|
$password = variable_get('smtp_password', ''); |
| 634 |
|
|
| 635 |
|
// If username and password are given, use SMTP authentication. |
| 636 |
|
if ($username != '' && $password != '') { |
| 637 |
|
$mail->SMTPAuth = TRUE; |
| 638 |
|
$mail->Username = $username; |
| 639 |
|
$mail->Password = $password; |
| 640 |
} |
} |
| 641 |
|
|
| 642 |
|
|
| 643 |
// Set the correct protocol prefix to append to the smtp host. |
// Set the protocol prefix for the smtp host. |
| 644 |
switch (variable_get('smtp_protocol', 'standard')) { |
switch (variable_get('smtp_protocol', 'standard')) { |
| 645 |
case "ssl": |
case 'ssl': |
| 646 |
$mail->SMTPSecure = 'ssl'; |
$mail->SMTPSecure = 'ssl'; |
| 647 |
break; |
break; |
| 648 |
case "tls": |
|
| 649 |
|
case 'tls': |
| 650 |
$mail->SMTPSecure = 'tls'; |
$mail->SMTPSecure = 'tls'; |
| 651 |
break; |
break; |
| 652 |
case "standard": |
|
| 653 |
|
default: |
| 654 |
$mail->SMTPSecure = ''; |
$mail->SMTPSecure = ''; |
| 655 |
} |
} |
| 656 |
|
|
|
$mail->Host = variable_get('smtp_host', '') .';'. variable_get('smtp_hostbackup', ''); |
|
|
$mail->Port = variable_get('smtp_port', '25'); |
|
|
$mail->Mailer = 'smtp'; |
|
|
$mail->SMTPAuth = $auth; |
|
|
$mail->Username = $username; |
|
|
$mail->Password = $password; |
|
|
$mail->CharSet = 'utf-8'; |
|
|
$mail->AddCustomHeader('Errors-To: '. $from); |
|
| 657 |
|
|
| 658 |
$torecipients = split(',', $to); |
// Set other connection settings. |
| 659 |
foreach ($torecipients as $torecipient) { |
$mail->Host = variable_get('smtp_host', '') .';'. variable_get('smtp_hostbackup', ''); |
| 660 |
if (strpos($torecipient, '<') !== false) { |
$mail->Port = variable_get('smtp_port', '25'); |
| 661 |
$toparts = explode(' <', $torecipient); |
$mail->Mailer = 'smtp'; |
|
$toname = $toparts[0]; |
|
|
$toaddr = rtrim($toparts[1], '>'); |
|
|
} |
|
|
else { |
|
|
$toname = ''; |
|
|
$toaddr = $torecipient; |
|
|
} |
|
|
$mail->AddAddress($toaddr, $toname); |
|
|
} |
|
| 662 |
|
|
|
$mail->Subject = $subject; |
|
|
$mail->Body = $body; |
|
| 663 |
|
|
| 664 |
|
// Let the people know what is going on. |
| 665 |
watchdog('smtp', 'Sending mail to: @to', array('@to' => $to)); |
watchdog('smtp', 'Sending mail to: @to', array('@to' => $to)); |
| 666 |
|
|
| 667 |
// Try to send e-mail. If it fails, set watchdog entry. |
// Try to send e-mail. If it fails, set watchdog entry. |
| 668 |
if (!$mail->Send()) { |
if (!$mail->Send()) { |
| 669 |
watchdog('smtp', 'Error sending e-mail from @from to @to : !error_message', array('@from' => $from, '@to' => $to, '!error_message' => $mail->ErrorInfo), WATCHDOG_ERROR); |
watchdog('smtp', 'Error sending e-mail from @from to @to : !error_message', array('@from' => $from, '@to' => $to, '!error_message' => $mail->ErrorInfo), WATCHDOG_ERROR); |
| 670 |
return false; |
return FALSE; |
| 671 |
} |
} |
| 672 |
|
|
| 673 |
$mail->SmtpClose(); |
$mail->SmtpClose(); |
| 674 |
return true; |
return TRUE; |
| 675 |
} // End of drupal_mail_wrapper(). |
} // End of drupal_mail_wrapper(). |
| 676 |
|
|
| 677 |
|
|
| 685 |
$message['body'] = $params['body']; |
$message['body'] = $params['body']; |
| 686 |
} |
} |
| 687 |
} // End of smtp_mail(). |
} // End of smtp_mail(). |
| 688 |
|
|
| 689 |
|
|
| 690 |
|
|
| 691 |
|
/** |
| 692 |
|
* Splits the input into parts based on the given boundary. |
| 693 |
|
* |
| 694 |
|
* Swiped from Mail::MimeDecode, with modifications based on Drupal's coding |
| 695 |
|
* standards and this bug report: http://pear.php.net/bugs/bug.php?id=6495 |
| 696 |
|
* |
| 697 |
|
* @param input |
| 698 |
|
* A string containing the body text to parse. |
| 699 |
|
* @param boundary |
| 700 |
|
* A string with the boundary string to parse on. |
| 701 |
|
* @return |
| 702 |
|
* An array containing the resulting mime parts |
| 703 |
|
*/ |
| 704 |
|
function _smtp_boundary_split($input, $boundary) { |
| 705 |
|
$parts = array(); |
| 706 |
|
$bs_possible = drupal_substr($boundary, 2, -2); |
| 707 |
|
$bs_check = '\"'. $bs_possible .'\"'; |
| 708 |
|
|
| 709 |
|
if ($boundary == $bs_check) { |
| 710 |
|
$boundary = $bs_possible; |
| 711 |
|
} |
| 712 |
|
|
| 713 |
|
$tmp = explode('--'. $boundary, $input); |
| 714 |
|
|
| 715 |
|
for ($i = 1; $i < count($tmp); $i++) { |
| 716 |
|
if (trim($tmp[$i])) { |
| 717 |
|
$parts[] = $tmp[$i]; |
| 718 |
|
} |
| 719 |
|
} |
| 720 |
|
|
| 721 |
|
return $parts; |
| 722 |
|
} // End of _smtp_boundary_split(). |
| 723 |
|
|
| 724 |
|
|
| 725 |
|
|
| 726 |
|
/** |
| 727 |
|
* Strips the headers from the body part. |
| 728 |
|
* |
| 729 |
|
* @param input |
| 730 |
|
* A string containing the body part to strip. |
| 731 |
|
* @return |
| 732 |
|
* A string with the stripped body part. |
| 733 |
|
*/ |
| 734 |
|
function _smtp_remove_headers($input) { |
| 735 |
|
$part_array = explode("\n", $input); |
| 736 |
|
|
| 737 |
|
if (strpos($part_array[0], 'Content') !== FALSE) { |
| 738 |
|
if (strpos($part_array[1], 'Content') !== FALSE) { |
| 739 |
|
if (strpos($part_array[2], 'Content') !== FALSE) { |
| 740 |
|
array_shift($part_array); |
| 741 |
|
array_shift($part_array); |
| 742 |
|
array_shift($part_array); |
| 743 |
|
} |
| 744 |
|
else { |
| 745 |
|
array_shift($part_array); |
| 746 |
|
array_shift($part_array); |
| 747 |
|
} |
| 748 |
|
} |
| 749 |
|
else { |
| 750 |
|
array_shift($part_array); |
| 751 |
|
} |
| 752 |
|
} |
| 753 |
|
|
| 754 |
|
$output = implode("\n", $part_array); |
| 755 |
|
return $output; |
| 756 |
|
} // End of _smtp_remove_headers(). |
| 757 |
|
|
| 758 |
|
|
| 759 |
|
|
| 760 |
|
/** |
| 761 |
|
* Returns a string that is contained within another string. |
| 762 |
|
* |
| 763 |
|
* Returns the string from within $source that is some where after $target |
| 764 |
|
* and is between $beginning_character and $ending_character. |
| 765 |
|
* |
| 766 |
|
* @param $source |
| 767 |
|
* A string containing the text to look through. |
| 768 |
|
* @param $target |
| 769 |
|
* A string containing the text in $source to start looking from. |
| 770 |
|
* @param $beginning_character |
| 771 |
|
* A string containing the character just before the sought after text. |
| 772 |
|
* @param $ending_character |
| 773 |
|
* A string containing the character just after the sought after text. |
| 774 |
|
* @return |
| 775 |
|
* A string with the text found between the $beginning_character and the |
| 776 |
|
* $ending_character. |
| 777 |
|
*/ |
| 778 |
|
function _smtp_get_substring($source, $target, $beginning_character, $ending_character) { |
| 779 |
|
$search_start = strpos($source, $target) + 1; |
| 780 |
|
$first_character = strpos($source, $beginning_character, $search_start) + 1; |
| 781 |
|
$second_character = strpos($source, $ending_character, $first_character) + 1; |
| 782 |
|
$substring = drupal_substr($source, $first_character, $second_character - $first_character); |
| 783 |
|
$string_length = drupal_strlen($substring) - 1; |
| 784 |
|
|
| 785 |
|
if ($substring[$string_length] == $ending_character) { |
| 786 |
|
$substring = drupal_substr($substring, 0, $string_length); |
| 787 |
|
} |
| 788 |
|
|
| 789 |
|
return $substring; |
| 790 |
|
} // End of _smtp_get_substring(). |