This is documentation for Kohana v2.3.x.

Table of Contents
StatusDraft
Todofuller description of driver (when to use one over another). Test attachment example fully

Email Helper

An Email helper to work with the Swift email library. You need to have the Swiftmailer library in the vendor directory if you want the helper to work. The directory structure must be:

vendor
 +- swift
 |    +- Swift
 |        |  ...
 |        |  ...
 |    -- Swift.php
 |    -- EasySwift.php

Configuration

The swiftmailer configuration is done in the application/config/email.php file, if it's not there take the one from system/config and copy it to the application folder (see cascading filesystem):

// Valid drivers are: native, sendmail, smtp
$config['driver'] = 'native';
 
// Driver options:
$config['options'] = NULL;

Drivers

config['driver'] sets the SwiftMailer driver. Valid drivers are:

Drivers options

$config['options'] contains driver specific parameters.

// Standard smtp connection
$config['options'] = array('hostname'=>'yourhostname', 'port'=>'25', 'username'=>'yourusername', 'password'=>'yourpassword');
 
// Secure SMTP connections
$config['options'] =  array('hostname'=>'smtp.gmail.com', 'port'=>'465', 'username'=>'yourusername', 'password'=>'yourpassword', 'encryption' => 'tls');
$config['options'] = '/path/to/sendmail';

Usage examples

Sending a basic email

To send a basic HTML email, use the code below:

$to      = '[email protected]';  // Address can also be array('[email protected]', 'Name')
$from    = '[email protected]';
$subject = 'This is an example subject';
$message = 'This is an <strong>example</strong> message';
 
email::send($to, $from, $subject, $message, TRUE);

Sending advanced emails

To send advanced emails you need to use Swift mailer methods. The code below show how to send an email with an attachment and multiple recipients. For advanced emails, Swiftmailer methods and classes will be directly used. email::connect can still be used to load Swiftmailer library and set the appropriate settings made in the config file config/email.php.

This example will not work correctly anymore due to a Swift Mailer bug.

// Use connect() method to load Swiftmailer and connect using the parameters set in the email config file
$swift = email::connect();
 
// From, subject and HTML message
$from = '[email protected]';
$subject = 'Backup: ' . date("d/m/Y");
$message = 'This is the <b>backup</b> for ' . date("d/m/Y");
 
// Build recipient lists
$recipients = new Swift_RecipientList;
$recipients->addTo('[email protected]');
$recipients->addTo('[email protected]');
 
// Build the HTML message
$message = new Swift_Message($subject, $message, "text/html");
 
// Attachment
$swiftfile = new Swift_File('/backups/dump-' . date("d-m-Y") . '.tar.gz');
$attachment = new Swift_Message_Attachment($swiftfile);
$message->attach($attachment);
 
if ($swift->send($message, $recipients, $from))
{
  // Success
}
else
{
  // Failure
}
 
// Disconnect
$swift->disconnect();

Methods

connect()

'connect' creates a SwiftMailer instance according to the driver and parameters set in the config file.

send()

'send' sends an e-mail using the specified information. The parameters are: