Na podstawie wątku na forum Kohany
Aby skorzystać z nieobecnej w Kohanie funkcji wysyłania maili (dostępnej w CI) należy skorzystać z udostępnionego razem z Kohaną narzędzie SwiftMailer. Na forum Kohany zostało zaprezentowane ułatwienie pozwalające w prosty sposób skorzystać z tego narzędzia. Wystarczy w katalogu application/helpers utworzyć plik mail.php z zawartością:
<?php defined('SYSPATH') or die('No direct script access.'); /* * Class: mail * A basic wrapper for Swiftmailer. */ class mail { /* * Method: swift * Creates a Swift object with connections. * * Returns: * A new Swift object. */ public static function swift() { static $run; // Only include Swift files once if ($run !== TRUE) { include Kohana::find_file('vendor', 'swift/Swift'); include Kohana::find_file('vendor', 'swift/Swift/Connection/SMTP'); include Kohana::find_file('vendor', 'swift/Swift/Connection/NativeMail'); include Kohana::find_file('vendor', 'swift/Swift/Connection/Multi'); $run = TRUE; } // SMTP connection $smtp = new Swift_Connection_SMTP(Config::item('mail.smtp_host')); $smtp->setUsername(Config::item('mail.smtp_user')); $smtp->setPassword(Config::item('mail.smtp_pass')); $connections[] = $smtp; // PHP's native mail function as fallback $connections[] = new Swift_Connection_NativeMail(); // Test connection try { $swift = new Swift(new Swift_Connection_Multi($connections)); } catch (Swift_ConnectionException $exception) { // TODO: lang file, more descriptive text Kohana::show_error('swift.no_connection', 'Check your mail connection settings.'); } // Return Swift instance return $swift; } /* * Method: send * Sends a mail. * * Parameters: * to - one or more recipients (RFC 2822 string) * subject - mail subject * message - mail body * from - sender (RFC 2822 string) * html - set to TRUE if you want to send HTML mails * * Returns: * An integer: the number of successful recipients. */ public static function send($to, $subject, $message, $from, $html = FALSE) { // Load Swiftmailer $swift = self::swift(); // Message $message = new Swift_Message ( (string) $subject, (string) $message, ($html) ? 'text/html' : 'text/plain', '8bit', 'utf-8' ); // Recipient(s) $to = self::parse_rfc2822($to); if (empty($to)) return FALSE; $recipient = new Swift_RecipientList; foreach ($to as $address) { $recipient->addTo($address[0], $address[1]); } // Sender $from = self::parse_rfc2822($from); if (empty($from)) return FALSE; $sender = new Swift_Address($from[0][0], $from[0][1]); // Send it! return $swift->send($message, $recipient, $sender); } /* * Method: parse_rfc2822 * Parses RFC 2822 compliant email strings. * * Parameters: * str - email string like 'User <user@example.com>, ...' * * Returns: * A nested array containing email addresses and names. */ public static function parse_rfc2822($str) { $return = array(); foreach (explode(',', (string) $str) as $email) { $email = trim($email); if (valid::email($email)) { $return[] = array($email, ''); } elseif (preg_match('/^(.*)<(\S+)>$/', $email, $matches) AND valid::email($matches[2])) { $return[] = array($matches[2], trim($matches[1])); } } return $return; } } // End mail ?>
Po utworzeniu helpera należy utworzyć w katalogu application/config pliku konfiguracyjnego mail.php z zawartością:
<?php defined('SYSPATH') or die('No direct script access.'); /* * File: config.php * This configuration file is unique to every application. * * Options: * smtp_host - smpt host address * smtp_user - smtp username * smtp_pass - smtp password */ $config = array ( 'smtp_host' => 'smtp.strona.com', 'smtp_user' => 'username', 'smtp_pass' => 'magic' ); ?>
W momencie, kiedy chcemy skorzystać z funkcji wysyłania maili wystarczy w kontrolerze dodać kod:
<?php defined('SYSPATH') or die('No direct script access.'); ... ... ... $this->load->helper('mail'); ... ... // Dla maili w czystym tekście mail::send('adres@odbiorcy.pl', 'Tytul maila', 'Tresc maila', 'adres@nadawcy.pl', FALSE); // Dla maili w HTMLu mail::send('adres@odbiorcy.pl', 'Tytul maila', '<body><b>Tresc maila</b></body>', 'adres@nadawcy.pl', TRUE); ... ... ?>
i voila...