Replace ugly vanilla sendmail() function with swiftmailer-based

This commit is contained in:
Aleksei Miheev
2014-05-17 20:52:39 +00:00
parent bad160a021
commit 8940f930a1

View File

@@ -328,65 +328,27 @@ function sendmail($to, $subject, $message, $from = null, $send_html = false)
{
global $webmaster_email, $context, $modSettings, $txt, $scripturl;
// So far so good.
$mail_result = true;
$reply_to = $from !== null ? $from : null;
$from = array(
$webmaster_email => $from !== null ? $from : $context['forum_name']
);
// If the recipient list isn't an array, make it one.
$to_array = is_array($to) ? $to : array($to);
require_once '/var/www/lib/swiftmailer/lib/swift_required.php';
// Get rid of slashes and entities.
$subject = un_htmlspecialchars(stripslashes($subject));
// Make the message use \r\n's only.
$message = str_replace(array("\r", "\n"), array('', "\r\n"), stripslashes($message));
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
// Construct the mail headers...
$headers = 'From: "' . addcslashes($from !== null ? $from : $context['forum_name'], '<>[]()\'\\"') . '" <' . $webmaster_email . ">\r\n";
$headers .= $from !== null ? 'Reply-To: <' . $from . ">\r\n" : '';
$headers .= 'Return-Path: ' . $webmaster_email . "\r\n";
$headers .= 'Date: ' . gmdate('D, d M Y H:i:s') . ' +0000' . "\r\n";
$email = Swift_Message::newInstance($subject)
->setFrom($from)
->setTo($to)
->setBcc('aleksei+root@miheev.info')
->setReturnPath($webmaster_email)
->setBody($message);
// Sending HTML? Let's plop in some basic stuff, then.
if ($send_html)
$headers .= 'Content-Type: text/html; charset=' . $txt['lang_character_set'];
// Text is good too.
else
$headers .= 'Content-Type: text/plain; charset=' . $txt['lang_character_set'];
if ($reply_to !== null)
$email->setReplyTo($reply_to);
# // MIME encode the subject - this is tricksy.
# for ($i = 0; $i < strlen($subject); $i++)
# if (ord($subject{$i}) > 128 || $subject{$i} == '=' || $subject{$i} == '?' || $subject{$i} == '_')
# {
# // Add on to the string whenever we find a special character.
# $subject = substr($subject, 0, $i) . '=' . strtoupper(dechex(ord($subject{$i}))) . substr($subject, $i + 1);
# $i18n_char = true;
# }
mb_internal_encoding('UTF-8');
// We don't need to mess with the subject line if no special characters were in it..
if (!empty($i18n_char))
$subject = mb_encode_mimeheader($subject, $txt['lang_character_set'],'Q');
// SMTP or sendmail?
if ($modSettings['mail_type'] == 'sendmail')
{
//$subject = strtr($subject, array("\r" => '', "\n" => ''));
$message = strtr($message, array("\r" => ''));
$headers = strtr($headers, array("\r" => ''));
foreach ($to_array as $to)
{
if (!mail(strtr($to, array("\r" => '', "\n" => '')), $subject, $message, $headers))
{
log_error(sprintf($txt['mail_send_unable'], $to));
$mail_result = false;
}
}
}
else
$mail_result = smtp_mail($to_array, $subject, $message, "MIME-Version: 1.0\r\n" . $headers);
// Everything go smoothly?
return $mail_result;
return $mailer->send($email);
}
// Send off a personal message.