Today, We want to share with you codeigniter send email from localhost. Email is very important in online any web applications. When a member signs up, i might want to send them an email to verify their email address as well as allow the member to confirm subscription. I also use email to reset forgotten passwords, send invoice as well as receipts to customers, etc. PHP CodeIgniter makes it super easy for us to send emails from our web application using a variety of options.
CodeIgniter has a built-in email library that i can work with when sending emails.
In this tutorial, you will learn simple step by step
- Email Configuration
- Email View
- Email Controller
- Email Routes
How to Send Email using CodeIgniter?
Today We was working with PHP with codeigniter as well as there is email feature but that need to work on localhost but We stuck at that point. And then some time We got the best solution as well as today I will share that PHP code with you. First of all We tell you PHP codeigniter is great MVC framework as well as We like it very much.
First you required you make an account on mailtrap.io and after making an account you will get username and password for smtp settings and you required to add that config or settings in your CI web site.
Here is working CI code and you need to add this your PHP codeigniter’s config.php file:
config.php
$config['email'] = Array( 'protocol' => 'smtp', 'smtp_host' => 'smtp.mailtrap.io', 'smtp_port' => 2525, 'smtp_user' => 'your_user_name', 'smtp_pass' => 'your_user_pass', 'crlf' => "\r\n", 'newline' => "\r\n"
In this first step I will download the latest version of Codeigniter, first of all simple you can Go to this link Download Codeigniter.
CodeIgniter Email Configuration
application/config/email.php
here simple Create a file email.php in the directory application/config
'smtp', // 'mail', 'sendmail', or 'smtp' 'smtp_host' => 'smtp.example.com', 'smtp_port' => 465, 'smtp_user' => '[email protected]', 'smtp_pass' => '12345!', 'smtp_crypto' => 'ssl', //can be 'ssl' or 'tls' for example 'mailtype' => 'text', //plaintext 'text' mails or 'html' 'smtp_timeout' => '4', //in seconds 'charset' => 'iso-8859-1', 'wordwrap' => TRUE );
Basic Configurations
$config['base_url'] = 'http://localhost/demo';
CodeIgniter Email View
application/views/email/contact.php
CodeIgniter Send Email - www.pakainfo.com Use the form below to send email
CodeIgniter Email Controller
application/controllers/SendEmailCtrl.php
load->helper('url'); } public function index() { $this->load->view('email/contact'); } function send() { $this->load->config('email'); $this->load->library('email'); $from = $this->config->item('smtp_user'); $to = $this->input->post('to'); $subject = $this->input->post('subject'); $message = $this->input->post('message'); $this->email->set_newline("\r\n"); $this->email->from($from); $this->email->to($to); $this->email->subject($subject); $this->email->message($message); if ($this->email->send()) { echo 'Your Email has successfully been sent.'; } else { show_error($this->email->print_debugger()); } } }
Email Routes call to methods
Add the bellow routes to application/config/routes.php
$route['send-email'] = 'your email controller'; $route['email'] = 'your email controller/send';
how to send email from localhost with php CodeIgniter?
Here you can check the PHP source code for send email from localhost server simple step.
function sendMail() { $config = Array( 'protocol' => 'smtp', 'smtp_host' => 'ssl://smtp.googlemail.com', 'smtp_port' => 465, 'smtp_user' => 'yourname@mail_address.com', // Update it to yours 'smtp_pass' => 'yourname@s245sd', // Update it to yours 'mailtype' => 'html', 'charset' => 'iso-8859-1', 'wordwrap' => TRUE ); $message = ''; $this->load->library('email', $config); $this->email->set_newline("\r\n"); $this->email->from('yourname@mail_address.com'); // Update it to yours $this->email->to('yourname@mail_address.com');// Update it to yours $this->email->subject('Send email by using codeigniter library via localhost For Testing'); $this->email->message($message); if($this->email->send()) { echo 'Good Luck Your Email sent.'; } else { show_error($this->email->print_debugger()); } }
I hope you get an idea about send link in email codeigniter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.