Warning: Use of undefined constant wp_cumulus_widget - assumed 'wp_cumulus_widget' (this will throw an Error in a future version of PHP) in /home/blogoptimise/public_html/wp-content/plugins/wp-cumulus/wp-cumulus.php on line 375
Optimise Blog » Codeigniter – Sending Emails using Gmail’s SMTP Server
Warning: Use of undefined constant style - assumed 'style' (this will throw an Error in a future version of PHP) in /home/blogoptimise/public_html/wp-content/themes/optimblog/functions.php on line 522

Codeigniter – Sending Emails using Gmail’s SMTP Server

Written by Gordon

Topics: Codeigniter

Sending emails and attaching files along with them are super easy! Just create a file called email.php in the config folder of your application. This file will contain the details of your Gmail SMTP server.

[php]
$config[‘protocol’] = ‘smtp’;
$config[‘smtp_host’] = ‘ssl://smtp.gmail.com’;
$config[‘smtp_port’] = ‘465’;
$config[‘smtp_user’] = ‘youremail@gmail.com’;
$config[‘smtp_pass’] = ‘password’;
$config[‘charset’] = ‘utf-8’;
$config[‘newline’] = "\r\n";
[/php]

In your Email controller, create a function with the following code…

[php]
class Email extends CI_Controller
{

function send()
{
// Loads the email library
$this->load->library(’email’);
// FCPATH refers to the CodeIgniter install directory
// Specifying a file to be attached with the email
$file = FCPATH . ‘license.txt’;
// Defines the email details
$this->email->from(‘me@example.com’, ‘My Name’);
$this->email->to(‘your@example.com’);
$this->email->cc(‘another@example.com’);
$this->email->bcc(‘one-another@example.com’);
$this->email->subject(‘Email Test);
$this->email->message(‘Testing the email class.’);
$this->email->attach($file);
// The email->send() statement will return a true or false
// If true, the email will be sent
if ($this->email->send()) {
echo "All OK";
} else {
echo $this->email->print_debugger();
}
}

}
[/php]

The above example is very basic, but explains how to send emails using Google Mail’s SMTP server.

Leave a Comment Here's Your Chance to Be Heard!