Implementing Swift Mailer with Codeigniter

As we know that Composer is a tool for dependency management and we are going to use it to implement Swift Mailer. According to the Codeigniter Documentation, We can use Composer by settings $config['composer_autoload']; in your application/config/config.php. Now use terminal and locate your projects application directory by simple CD (Change Directory) command. Now make sure you have installed Composer, You can check the installation by typing the following command composer --v and if Composer installed into your machine it will tell you about the current version of the Composer.

 

Installation and implementation of Swift Mailer

After making sure type the flowing command composer require swiftmailer/swiftmailer and hit enter to install Swift Mailer. After finishing the installation, go to the application/libraries and create a file name Mailer.php and paste the code given below and change the email host, username, password etc. as per your outgoing mail server/provider.

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

class Mailer { 

    protected $to; 

    protected $subject; 

    protected $userName; 

    protected $password; 

    protected $emailHost; 

    protected $mailFrom; 

    protected $mailDirectory; 


    public function __construct() { 

        $this->userName = 'email@xyz.com';
        $this->password = 'your_password_here';
        $this->emailHost = 'SMTP_SETTINGS';
        $this->mailFrom = ['email@xyz.com' => 'WebOmnizz'];

        // Define email directory
        $this->mailDirectory = VIEWPATH . '/emails';
    }



    protected function init()
    {
        $transport = (new Swift_SmtpTransport( $this->emailHost, 465, 'ssl'))
                        ->setUsername( $this->userName )
                        ->setPassword( $this->password );
 
        // Create the Mailer using your created Transport
        $mailer = new Swift_Mailer($transport);
        return $mailer;
    }




    protected function initializeTemplate( $template, $__data ) {

        ob_start();
        extract( $__data );
        
        include $this->mailDirectory .'/'.$template;

        return ltrim( ob_get_clean() );
    }



    public function to( $email ) 
    {
        $this->to = $email;
        return $this;
    }



    public function subject( $subject )
    {
        $this->subject = $subject;
        return $this;
    }



    public function send( $view, array $data = [] )
    {
        // Initialize Swift Mailer
        $mailer = $this->init();
        $template = $this->initializeTemplate( $view, $data );

        // Create a message
        $message = (new Swift_Message( $this->subject ))
                    ->setFrom( $this->mailFrom )
                    ->setTo([$this->to])
                    ->setBody( $template, 'text/html' );

        // Send the message
        $result = $mailer->send($message);
    }
}

 

Using Swift Mailer through Mailer.php Library

Now we can use the default codeigniter mail or Swift Mailer by using this separate library. Now load the mailer library in your codes $this->load->library('mailer'); and use it like the following.

$email = 'youremail@example.com';

// Use user or any information to load in email template
$templateData = ['name' => 'John Doe']; 

$this->mailer->to($email)->subject("Enter your email subject")
            ->send("your_email_template.php", compact('templateData'));

Before using this code make sure to create the email template in application/views/emails. You can change the email template path by changing into the mailer library file by changing the $this->mailDirectory value $this->mailDirectory = VIEWPATH . '/emails';

I made this library to make the use of Swift Mailer and the email template system as simple as possible. Hope this library will make your coding life a little easier to live 😉

Posted by Jogesh Sharma

Jogesh Sharma is a web developer and blogger who loves all the things design and the technology, He love all the things having to do with PHP, WordPress, Joomla, Magento, Durpal, Codeigniter, jQuery, HTML5 etc. He is the author of this blog.