wp code snippet

WordPress, PHP quick help codes

Sending emails through Mailgun using Codeigniter

This guide provides the best, quick and simple way to integrating Mailgun’s SMTP into Codeigniter using Email Class.

Get started by creating an email.php file in your application’s “config” folder.

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

/**
 * Mailgun Configuration
 */
$config['protocol']			= 'smtp';
$config['smtp_host']		= 'ssl://smtp.mailgun.org';
$config['smtp_port']		= 465;
$config['smtp_user']		= 'postmaster@sandbox.mailgun.org';
$config['smtp_pass']		= 'PASSWORD';
$config['smtp_crypto']		= '';
$config['charset']			= 'utf-8';
$config['mailtype']			= 'html';
$config['email']['newline']	= "rn";

Add the following code in your controller:

<?php
$this->load->library('email'); 
$this->email->from('sender@example.com', 'Sender Name');
$this->email->to('recipient@example.com','Recipient Name');
$this->email->subject('Your Subject');
$this->email->message('Your Message'); 
try {
    $this->email->send();
    echo 'Message has been sent.';
} catch(Exception $e) {
    echo $e->getMessage();
}