From 3291c7d850d9d031a367f24523255d2ec5e96e0b Mon Sep 17 00:00:00 2001 From: Deon George Date: Mon, 29 Sep 2014 14:24:38 +1000 Subject: [PATCH] Moved Email into Kohana_Email to enable extension --- modules/khemail/classes/Email.php | 144 +--------------------- modules/khemail/classes/Kohana/Email.php | 145 +++++++++++++++++++++++ 2 files changed, 146 insertions(+), 143 deletions(-) create mode 100644 modules/khemail/classes/Kohana/Email.php diff --git a/modules/khemail/classes/Email.php b/modules/khemail/classes/Email.php index 32ddec5..5a2586e 100644 --- a/modules/khemail/classes/Email.php +++ b/modules/khemail/classes/Email.php @@ -1,145 +1,3 @@ load('email'); - - switch ($config['driver']) - { - case 'smtp': - // Set port - $port = empty($config['options']['port']) ? 25 : (int) $config['options']['port']; - - // Create SMTP Transport - $transport = Swift_SmtpTransport::newInstance($config['options']['hostname'], $port); - - if ( ! empty($config['options']['encryption'])) - { - // Set encryption - $transport->setEncryption($config['options']['encryption']); - } - - // Do authentication, if part of the DSN - empty($config['options']['username']) or $transport->setUsername($config['options']['username']); - empty($config['options']['password']) or $transport->setPassword($config['options']['password']); - - // Set the timeout to 5 seconds - $transport->setTimeout(empty($config['options']['timeout']) ? 5 : (int) $config['options']['timeout']); - break; - case 'sendmail': - // Create a sendmail connection - $transport = Swift_SendmailTransport::newInstance(empty($config['options']) ? "/usr/sbin/sendmail -bs" : $config['options']); - - break; - default: - // Use the native connection - $transport = Swift_MailTransport::newInstance($config['options']); - break; - } - - // Create the SwiftMailer instance - return Email::$mail = Swift_Mailer::newInstance($transport); - } - - /** - * Send an email message. - * - * @param string|array recipient email (and name), or an array of To, Cc, Bcc names - * @param string|array sender email (and name) - * @param string message subject - * @param string message body - * @param boolean send email as HTML - * @return integer number of emails sent - */ - public static function send($to, $from, $subject, $message, $html = FALSE) - { - // Connect to SwiftMailer - (Email::$mail === NULL) and email::connect(); - - // Determine the message type - $html = ($html === TRUE) ? 'text/html' : 'text/plain'; - - // Create the message - $message = Swift_Message::newInstance($subject, $message, $html, 'utf-8'); - - if (is_string($to)) - { - // Single recipient - $message->setTo($to); - } - elseif (is_array($to)) - { - if (isset($to[0]) AND isset($to[1])) - { - // Create To: address set - $to = array('to' => $to); - } - - foreach ($to as $method => $set) - { - if ( ! in_array($method, array('to', 'cc', 'bcc'))) - { - // Use To: by default - $method = 'to'; - } - - // Create method name - $method = 'add'.ucfirst($method); - - if (is_array($set)) - { - // Add a recipient with name - $message->$method($set[0], $set[1]); - } - else - { - // Add a recipient without name - $message->$method($set); - } - } - } - - if (is_string($from)) - { - // From without a name - $message->setFrom($from); - } - elseif (is_array($from)) - { - // From with a name - $message->setFrom($from[0], $from[1]); - } - - return Email::$mail->send($message); - } - -} // End email +abstract class Email extends Kohana_Email { } diff --git a/modules/khemail/classes/Kohana/Email.php b/modules/khemail/classes/Kohana/Email.php new file mode 100644 index 0000000..5abc387 --- /dev/null +++ b/modules/khemail/classes/Kohana/Email.php @@ -0,0 +1,145 @@ +load('email'); + + switch ($config['driver']) + { + case 'smtp': + // Set port + $port = empty($config['options']['port']) ? 25 : (int) $config['options']['port']; + + // Create SMTP Transport + $transport = Swift_SmtpTransport::newInstance($config['options']['hostname'], $port); + + if ( ! empty($config['options']['encryption'])) + { + // Set encryption + $transport->setEncryption($config['options']['encryption']); + } + + // Do authentication, if part of the DSN + empty($config['options']['username']) or $transport->setUsername($config['options']['username']); + empty($config['options']['password']) or $transport->setPassword($config['options']['password']); + + // Set the timeout to 5 seconds + $transport->setTimeout(empty($config['options']['timeout']) ? 5 : (int) $config['options']['timeout']); + break; + case 'sendmail': + // Create a sendmail connection + $transport = Swift_SendmailTransport::newInstance(empty($config['options']) ? "/usr/sbin/sendmail -bs" : $config['options']); + + break; + default: + // Use the native connection + $transport = Swift_MailTransport::newInstance($config['options']); + break; + } + + // Create the SwiftMailer instance + return Email::$mail = Swift_Mailer::newInstance($transport); + } + + /** + * Send an email message. + * + * @param string|array recipient email (and name), or an array of To, Cc, Bcc names + * @param string|array sender email (and name) + * @param string message subject + * @param string message body + * @param boolean send email as HTML + * @return integer number of emails sent + */ + public static function send($to, $from, $subject, $message, $html = FALSE) + { + // Connect to SwiftMailer + (Email::$mail === NULL) and email::connect(); + + // Determine the message type + $html = ($html === TRUE) ? 'text/html' : 'text/plain'; + + // Create the message + $message = Swift_Message::newInstance($subject, $message, $html, 'utf-8'); + + if (is_string($to)) + { + // Single recipient + $message->setTo($to); + } + elseif (is_array($to)) + { + if (isset($to[0]) AND isset($to[1])) + { + // Create To: address set + $to = array('to' => $to); + } + + foreach ($to as $method => $set) + { + if ( ! in_array($method, array('to', 'cc', 'bcc'))) + { + // Use To: by default + $method = 'to'; + } + + // Create method name + $method = 'add'.ucfirst($method); + + if (is_array($set)) + { + // Add a recipient with name + $message->$method($set[0], $set[1]); + } + else + { + // Add a recipient without name + $message->$method($set); + } + } + } + + if (is_string($from)) + { + // From without a name + $message->setFrom($from); + } + elseif (is_array($from)) + { + // From with a name + $message->setFrom($from[0], $from[1]); + } + + return Email::$mail->send($message); + } + +} // End email