Here I'm going to tell you about how to send contact emails in codeigniter using SMTP Settings. this is one of the simple way to integrate in Codeignter. We already discussed a bit about Codeigniter in previous tutorials. It has some library file to make this action. like that here also we have email library file. Lets see the code and steps below.

Please refer this article for settings
BASIC SETTING
application->config->autoload.php file
$autoload['libraries'] = array('database', 'email', 'session', 'upload');
DATABASE
Database Name : 2my4edge
TABLE NAME
1.code_email
CREATE TABLE `code_email` ( `id` int(11) NOT NULL, `name` varchar(250) NOT NULL, `email` varchar(250) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
VIEW FILES
1.emailform.php
<div align="center" class="container"> <?php if($this->session->flashdata('flash_job')){?> <div class="alert alert-success"> <?php echo $this->session->flashdata('flash_job')?> </div> <?php } ?> <form method="POST" action="<?php echo site_url('sendmail/sendmail');?>"> <table border="1"> <tr> <td>Name</td> <td><input type="text" name="name" required id="name" placeholder="Name"></td> </tr> <tr> <td>Email ID</td> <td><input type="email" name="emailid" required id="email" placeholder="Your Email id"></td> </tr> <tr> <td colspan="2" align="center"><input style="width: 50%;" type="submit" value="Submit"></td> </tr> </table> </form> </div>
CONTROLLER FILES
1.Sendmail.php
Change STML EMAIL and PASSWORD. Give your valid email id and password.
MODEL FILES
1.Sendmail_model.php
If you want to save the data in database use this model, else remove the model. and also from controller.
For more details visit this link : Codeignter
I hope this code is really helpful to all codeigniter developer. Thanks and comment your feedback below.
1.Sendmail.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Sendmail extends CI_Controller { public function __construct() { parent::__construct(); $this->load->helper('url'); /***** LOADING HELPER TO AVOID PHP ERROR ****/ $this->load->model('Sendmail_model','send'); /* LOADING MODEL * Welcome_model as welcome */ } public function index() { $this->load->view('emailform'); } public function sendmail() { $input_data=array('name'=>$this->input->post('name'), 'email'=>$this->input->post('emailid') ); $this->send->save_contact_data($input_data); $from_email="arun@2my4edge.com"; $replyemail = "arunkumarpsp1@gmail.com"; $name="Arunkumar 2my4edge"; $to_email=$input_data['email']; $subject="New Registration from 2my4edge.com"; $config = Array( 'protocol' => 'smtp', 'smtp_host' => 'ssl://smtp.gmail.com', 'smtp_port' => 465, 'smtp_user' => 'youremail@gmail.com', 'smtp_pass' => 'xxxyourpasswordxxx', 'mailtype' => 'text/html', 'newline' => '\r\n', 'charset' => 'utf-8' ); $this->load->library('email', $config); $this->email->set_newline("\r\n"); $this->email->set_mailtype("html"); $this->email->from($from_email,$name); $this->email->to($to_email); $this->email->reply_to($replyemail); $this->email->subject($subject); $message='<table style="background:white;width:100%;text-align:center;max-width:800px;min-width:450px;"> <tbody> <tr> <td style="padding:10px 0px;"> <a href="http://www.2my4edge.com"> <img src="http://2.bp.blogspot.com/-nr1K0W-Zqi0/U_4lUoyvvVI/AAAAAAAABJE/F_C7i48sI58/s1600/new2.png"> </a> </td> </tr> </tbody> </table> <table style="background:#F1F1F1;width:100%;padding: 12px; text-align: justify; line-height: 25px;font-family:roboto;max-width:800px;min-width:450px;"> <tbody> <tr> <td> <p style="color:#68461d;font-size:28px;"> <i></i><br> <small style="color:666666!important;font-size: medium; font-style: normal;"> Dear <b>'.$input_data['name'].',</b> <br> you have tested email from <a href="http://www.2my4edge.com">www.2my4edge.com</a>. <br> Your Email Id is <b>'.$input_data['email'].'</b>.</small> </p> </td> </tr> </tbody> </table>'; $this->email->message($message); $this->email->send(); $this->session->set_flashdata("flash_job",'Thank you for your submission. We will get back to you shorty.'); redirect('sendmail'); } }
MODEL FILES
1.Sendmail_model.php
<?php class Sendmail_model extends CI_Model { public function __construct() { parent::__construct(); } public function save_contact_data($inputdata) { $this->db->insert('code_email', $inputdata); } }
If you want to save the data in database use this model, else remove the model. and also from controller.
For more details visit this link : Codeignter
I hope this code is really helpful to all codeigniter developer. Thanks and comment your feedback below.
Bohot Achchha, Tutorial post kiya hai aapne bhai,
ReplyDeleteEse hi hum logo ko sikhate rehna..
Shukriya.
Bahu Saras, Tutorial 6 ...
ReplyDeleteOpen Internet Explorer and log in to your first Google account. Next, open Google Chrome and log in to your second Google account, and finally, open Firefox to log in to the third account. You can use all Google services for each account, just as if you were logged in to just one. how to create gmail account
ReplyDelete