• Using Swift Mailer with CodeIgniter
  • Using Swift Mailer with CodeIgniterI really do enjoy using CodeIgniter for a lot of my web development projects but sometimes I do get frustrated at the lack of simple things such as chaining especially with the email library.  Not to mention, I have had a few problems with it on occasion. I recently decided to take a look at what PHP apps or libraries could easily integrate with PHP without so much as having to always write a "CI" library for integration to be successful..  

    For my first port of call, I immediately thought of Swift Mailer.  I did search the web and found a library with tried to integrate Swift Mailer with CodeIgniter but guess what.  It was for an old version of CodeIgniter and I felt the version of Swift Mailer used then was pretty old.

    This tutorial will show you a very easy way of integrating Swift Mailer into CodeIgniter.  The best part is that you do not have to write any library in order to use it.  So here goes.

    Setup your code application as you normally do.  Next download the latest version of Swift Mailer 4.0.6.  Create a new folder called swift_mailer in your CI's application/libraries folder and extract the contents of the swift mailer archive into this folder.

    Next, using your favourite editor (so loving PHPStorm) create a controller called swiftmail.php in your Controllers folder. You should have a file structure as shown below.  Pay no attention to the other files. The main files you need have are outlined as well.

    Next add the code below to the swiftmail.php controller:

    	<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    require_once APPPATH.'libraries/swift_mailer/swift_required.php';
    
    class Swiftmail extends Controller {
    
        function __construct() {
            parent::__construct();
        }
    
        function index() {
            //Create the Transport
            $transport = Swift_MailTransport::newInstance();
    
            /*
            You could alternatively use a different transport such as Sendmail or Mail:
    
            //Sendmail
            $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
    
            //Mail
            $transport = Swift_MailTransport::newInstance();
            */
    
            //Create the message
            $message = Swift_Message::newInstance();
    
            //Give the message a subject
            $message->setSubject('Your subject')
                    ->setFrom('webmaster@gkaos.eo')
                    ->setTo('eokorie@gkaos.eo')
                    ->setBody('Here is the message itself')
                    ->addPart('<q>Here is the message itself</q>', 'text/html')
            ;
    
            //Create the Mailer using your created Transport
            $mailer = Swift_Mailer::newInstance($transport);
    
            //Send the message
            $result = $mailer->send($message);
    
            if ($result) {
                echo "Email sent successfully";
            } else {
                echo "Email failed to send";
            }
        }
    }
     
    

    The code should be self-explanatory as I just used the same one that was used in the Swift Mailer documentation.  Make sure you chnage the setFrom or setTo values for your testting purposes.  You can allow pass an array as a value for both of them as well.  Simply access your controller from your browser, and hopefully, you should have an email sent to you.

    If you dont always want to have to call require the Swift Mailer library manually all the time, simply bung the code below into a MY_Controller.php file: 

    	function require_thirdparty_package($package = '') {
            switch($package)
            {
    
                case 'swift_mailer':
                    require_once APPPATH.'third_party/swift_mailer/swift_required.php';
                    break;
    
            }
    }

    Simply get swiftmail.php to extend MY_Controller and initiate the library by typing in the code below wherever you need it in your controller.

    	$this->require_thirdparty_package("swift_mailer");

    I hope this has been useful to someone.


« Previous Article Next Article »



Related Articles