sending email via gmail smtp with codeigniter
Sending email with gmail smtp with codeigniter email library
Using SMTP server is always a good idea to send email from the codeigniter. while sending email without smtp(mail()), it may chance to deliver email to the spam folder. To avoid this issue SMTP is an better way to send an email. CodeIgniter have a inbulit Email Class provides to sent email.
In this tutorial, we will show how you can send HTML email via Gmail SMTP server in CodeIgniter application. The CodeIgniter email library will be used to send email using Gmail SMTP server.
Send email via Gmail SMTP server in CodeIgniter
At first include the CodeIgniter email library. Now specify the SMTP host (smtp_host), port (smtp_port), email (smtp_user), and password (smtp_pass) in SMTP configuration ($config) as per your SMTP server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
//Load email library $this->load->library('email'); //SMTP & mail configuration $config = array( 'protocol' => 'smtp', 'smtp_host' => 'ssl://smtp.gmail.com', 'smtp_port' => 465, 'smtp_user' => 'xxx@gmail.com', 'smtp_pass' => 'xxx', 'mailtype' => 'html', 'charset' => 'iso-8859-1' ); $this->email->initialize($config); $this->email->set_mailtype("html"); $this->email->set_newline("\r\n"); //Email content $htmlContent = '<h1>Sending email via Gmail SMTP server</h1>'; $htmlContent .= '<p>This email has sent via Gmail SMTP server from CodeIgniter application.</p>'; $this->email->to('recipient@example.com'); $this->email->from('sender@example.com','MyWebsite'); $this->email->subject('How to send email via Gmail SMTP server in CodeIgniter'); $this->email->message($htmlContent); //Send email $this->email->send(); |
Send email via Gmail SMTP server in CodeIgniter
To use Gmail SMTP for sending email in CodeIgniter, you need to make some changes in Google account settings.
Follow the below steps to use Gmail SMTP in CodeIgniter email library.
Login to your Google account.
Go to the 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.
If you notice that the emails are sent to the spam folder, use Encrypt Class in CodeIgniter to solve this issue in Gmail. You need to load the CodeIgniter Encrypt library before sending the email. It will encrypt your email and help to avoid the spamming issue in Gmail.
1 |
$this->load->library('encrypt'); |
If you face any issue in above code.Please comment.Happy Coding…
yes my email is still goes to spam folder too.
is there another way to send e-mail so that e-mail does not enter into the spam folder?