CodeIgniter Send Email Using Gamil SMTP

In CodeIgniter send email is to easy with your Gmail account and through Gmail SMTP. Here in below SendMail function is the best example of send mail, just copy below code and pest in Controller file and set the function parameter. Here in below code, you need to add your Gmail email and account password.
<?php
public function SendMail($to,$cc,$bcc,$subject,$message){
$this->load->library(’email’);
$config[“protocol”] = “smtp”;
$config[‘mailpath’] = ‘/usr/sbin/sendmail’;
$config[“charset”] = “utf-8”;
$config[“wordwrap”] = TRUE;
$config[“smtp_host”]=”ssl://smtp.gmail.com”;
$config[“smtp_port”]=465;
$config[“smtp_crypto”]=”SSL”;
$config[“smtp_user”]=”YourGamilIdHere@gmail.com“;
$config[“smtp_pass”]=”YourGamilPassword“;
$config[“validate”]=TRUE;
$config[“newline”]=”\r\n”;
$config[“crlf”] = “\r\n”;
$config[“wordwrap”] = TRUE;
$config[“smtp_timeout”] = 300;
$config[“mailtype”] = “html”;
$this->load->library(’email’,$config);
$this->load->library(‘user_agent’);
$this->email->set_newline(“\r\n”);
$this->email->from(‘YourGamilIdHere@gmail.com‘, ‘Your Name Here‘);
$this->email->to($to);
$this->email->cc($cc);
$this->email->bcc($bcc);
$this->email->subject($subject);
$this->email->message($message);
if($this->email->send())
{
$a=true;
} else {
show_error($this->email->print_debugger());
}return $a;
}

 

// Send mail using above function

$to=”youremail@gmail.com”;

$cc==”CCemail@gmail.com”;

$bcc=”BCCemail@gmail.com”;

$subject=”Your subject is here “;

$message=”Your message body is here.”

$this->SendMail($to,$cc,$bcc,$subject,$message);

?>

Leave a Reply

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