4webby web applications
06
Mar 2007

CI SWIFT MAILER

Well I wanted to call this library/plugin Freak_Swift_Mailer, but then& somebody killed my creativity&

Anyway, this is my solution for using SWIFT MAILER library with CI.

RESOURCES:

=> FORUM

=> official SWIFT MAILER WEBSITE

=> 4webby.com for tutorials

Author: Daniel Vecchiato (danfreak)
website: http://www.4webby.com
The original Swift Mailer package has been released by Chris Corbyn http://www.swiftmailer.org/

#
* @license GNU Lesser General Public License
#
*/

REQUIREMENTS:

- CodeIgniter 1.5.1/1.5.2 (http://www.codeigniter.com/download.php)

INSTALLATION:

  1. download SWIFT MAILER from SWIFT MAILER website (choose PHP4 or PHP5 version according to your needs)
  2. unzip Swift-X.0.X-phpX.zip in a local folder
  3. create a folder called my_classes in your system/application directory
  4. copy the files INSIDE the folder Swift-X.0.X-phpX/lib/ in your system/application/my_classes/ directory
  5. enable hooks in your application/config/config.php file
  6. /*
    |--------------------------------------------------------------------------
    | Enable/Disable System Hooks
    |--------------------------------------------------------------------------
    |
    | If you would like to use the "hooks" feature you must enable it by
    | setting this variable to TRUE (boolean). See the user guide for details.
    |
    */
    $config['enable_hooks'] = TRUE;

  7. add the following lines in application/config/hooks.php
  8. $hook['pre_controller'][] = array(
    'class' => 'MyClasses',
    'function' => 'index',
    'filename' => 'MyClasses.php',
    'filepath' => 'hooks'
    );

  9. in your system/application/hooks/ create a new file called MyClasses.php with the following inside
  10. <?php
    if (!defined('BASEPATH')) exit('No direct script access allowed');

    class Myclasses
    {
    /**
    * includes the directory application\my_classes\ in your includes directory
    *
    */
    function index()
    {
    //includes the directory application\my_classes\Swift\
    ini_set('include_path', ini_get('include_path').':'.BASEPATH.'application/my_classes/');
    }
    }
    ?>

    Be carefull to change the colon ':' to semicolon ';' if you use the above hook on Windows server/local server

    Windows example:
    //Windows
    ini_set('include_path', ini_get('include_path').';'.BASEPATH.'application/my_classes/')


    Linux example:
    //Linux
    ini_set('include_path', ini_get('include_path').':'.BASEPATH.'application/my_classes/')

  11. build a controller to test that everything works fine (example taken here)
  12. class Mail extends Controller
    {
    function Mail()
    {
    parent
    ::Controller();

    }

    function index()
    {
    //Load in the files we'll need
    require_once "Swift.php";
    require_once
    "Swift/Connection/SMTP.php";

    //Start Swift
    $swift =& new Swift(new Swift_Connection_SMTP("your.smtp.com"));

    //Create the message
    $message =& new Swift_Message("My subject", "My body");

    //Now check if Swift actually sends it
    if ($swift->send($message, "recipient@email.com", "sender@email.com")) echo "Sent";
    else echo
    "Failed";
    }
    }

    ?>

  13. now build other controllers/methods to suit your needs. Check out the SWIFT MAILER DOCUMENTATION
  14. use the examples in the documentation, but change
    //Load in the files we'll need
    require_once "lib/Swift.php";
    require_once "lib/Swift/Connection/SMTP.php";

    to
    //Load in the files we'll need
    require_once "Swift.php";
    require_once "Swift/Connection/SMTP.php";

  15. HAPPY CODING!