Codeigniter: Install Swift Mailer using Composer

Composer is the new kid on the block. Composer is a tool used to distribute and manage packages. I first heard of it while taking a look at Laravel. Composer’s power consists in the fact that the packages distributed by it are framework agnostic.

From the start I will assume that you have composer installed. Now, going to, look for swiftmailer ( As you can see the json command for getting the latest distribution of swiftmailer is: “required”: “swiftmailer/swiftmailer”: “5.1.*@dev”.

So, we will have to make a composer.json file inside the folder that contains the system and application folders, containing the following lines:

  "require": {
    "swiftmailer/swiftmailer": "5.1.*@dev"

After we do this, using the command prompt, we go to the folder that contains the composer.json and there we write the command composer install, which will start the installation of swiftmailer.

As you will see, after the installation, a new folder will appear, named vendor, and inside it we will find swiftmailer.

Once we have the swiftmailer installed we can use it inside the codeigniter application.

So, let’s make a controller and name it Swiftmailer.php

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

// we will use the composer autoloader to make sure we autoload the packages installed by composer
require_once APPPATH.'../vendor/autoload.php';

// if you want you can load only the swiftmailer, considering that you will only need it in this controller
// require_once APPPATH.'../vendor/swiftmailer/swiftmailer/lib/swift_required.php';

class Swiftmailer extends MY_Controller {
  public function index()
    //Create the Transport. I created it using the gmail configuration
    $transport = Swift_SmtpTransport::newInstance('', 465,'ssl')

    // You could alternatively use a different transport such as Sendmail or Mail:
    //$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
    //$transport = Swift_MailTransport::newInstance();

    //Create the message
    $message = Swift_Message::newInstance();

    //Give the message a subject
    $message->setSubject('Your subject')
      ->setBody('Here is the message sent with swiftmailer')
      ->addPart('<q>Here is the message sent with swiftmailer</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";
      echo "Email failed to send";

Now, if you will visit the controller and all went well, a mail will be sent and a confirmation of the sending will be displayed on the browser.

Worth reading:

Using Swift Mailer with CodeIgniter

Swift Mailer and Codeigniter

Swiftmailer documentation


  1. Thanks,
    But i am using thease two lines
    $transport = Swift_SmtpTransport::newInstance(‘’, 465,’ssl’)
    many times in application in mvc, where i am to place and how to call thease functions

  2. “So, we will have to make a composer.json file inside the folder that contains the system and application folders, containing the following lines:”

    Please be more specific with the respective folders. The composer.json file is supposed to be created in the web development project you’re working on:

    1. Well… I guess there is only one system and one application directory in CodeIgniter. And most of the time they are in the same directory. I really can’t be more specific, considering the server can be installed inside any partition and directory…

Leave a Reply

Your email address will not be published. Required fields are marked *

No spam? * Time limit is exhausted. Please reload CAPTCHA.