Send email via Gmail SMTP server in CodeIgniter

Send email via Gmail SMTP server in CodeIgniter:

To use Gmail SMTP for sending email in CodeIgniter, We need to make some changes in Google account settings Option.

Follow the below steps to use Gmail SMTP Setup in CodeIgniter email library.

  • Login to your Google account.
  • Go to the Google My Account page. Click the Signing in to Google link from Sign-in & security section.

  • Scroll down the Password & sign-in method section and turn Off the 2-Step Verification.

  • Scroll down the Connected apps & sites section and turn On Allow less secure apps.

 

Now your Gmail account is ready to use in CodeIgniter email library as an SMTP server.

The following example code help to send HTML email in CodeIgniter using your Gmail account. You only need to specify your Gmail email address (smtp_user) and password (smtp_pass).

$from_name = "Test Email"; 
$from_email = '[email protected]'; 
$to_email = '[email protected]'; 

//Email content 
$subject = 'checking Mail2'; 
$message = 'Message : checking Mail2'; 

//Load email library 
$this->load->library('email'); 

//SMTP & mail configuration 

$config = array( 
    'protocol' => 'smtp', 
    'smtp_host' => 'ssl://smtp.googlemail.com', 
    'smtp_port' => 465, 
    'smtp_user' => '[email protected]', 
    'smtp_pass' => 'gmail_password', 
    'mailtype' => 'html', 
    'charset' => 'utf-8' 
); 

$this->email->initialize($config); 
$this->email->set_mailtype("html"); 
$this->email->set_newline("\r\n"); 
$this->email->from($from_email, $from_name); 
$this->email->to($to_email); 
$this->email->subject($subject); 
$this->email->message($message); 

//Send email 
if($this->email->send()){ 
   print_r('Send Successfully'); 
}else{ 
   echo "Error Happened"; 
   echo $this->email->print_debugger(); 
   die("error1"); 
}

Leave a Reply

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