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.
Written by Gordon
Topics: Codeigniter