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"); 
}

Sending Email via SMTP Server in CodeIgniter

Sending Email via SMTP Server in CodeIgniter::

By Using SMTP mail server, we can send mail from Codeigniter. It is always a good idea to send email from the script. Sometimes PHP mail() function not works & fails to send email to the recipient or deliver email to the spam folder. We can  avoid this type issue by sending email as SMTP mail. CodeIgniter Email Class provides an easy way to send email from the PHP code or script. Also, we can send email via SMTP server using CodeIgniter Email library.

here, we will show how you can send HTML email via SMTP server in CodeIgniter application. The CodeIgniter email library will be used to send email using SMTP server

$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' => 'mail.example.com',
  'smtp_port' => 25,
  'smtp_user' => '[email protected]',
  'smtp_pass' => 'test_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");
}

Using PHP mail – setting correct MIME type

# Send Mail format # Using PHP mail – setting correct MIME type $to   = “[email protected]”; $subject = “Test subject”; $message = “Test message”; // To send HTML mail, the Content-type header must be set $headers[] = ‘MIME-Version: 1.0’; $headers[] = ‘Content-type: text/html; charset=iso-8859-1’; // Additional headers $headers[] = ‘To: Mary <[email protected]>, Kelly <[email protected]>’; $headers[] … Read moreUsing PHP mail – setting correct MIME type