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
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.
Create Contact Form And Send Email using PHP
application/config/email.php
here simple Create a file email.php in the directory application/config
<?php defined('BASEPATH') OR exit('No direct script access allowed');
$config = array(
'protocol' => 'smtp', // 'mail', 'sendmail', or 'smtp'
'smtp_host' => 'smtp.example.com',
'smtp_port' => 465,
'smtp_user' => 'no-reply@example.com',
'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';
application/views/email/contact.php
<!DOCTYPE html>
<html>
<head>
<title>CodeIgniter Send Email - www.pakainfo.com</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>
<h3>Use the form below to send email</h3>
<form method="post" action="<?=base_url('email')?>" enctype="multipart/form-data">
<input type="email" id="to" name="to" placeholder="Receiver Email">
<br><br>
<input type="text" id="subject" name="subject" placeholder="Enter Your Subject">
<br><br>
<textarea rows="6" id="message" name="message" placeholder="Type your message here"></textarea>
<br><br>
<input type="submit" value="Send Email" />
</form>
</div>
</body>
</html>
application/controllers/SendEmailCtrl.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class SendEmailCtrl extends CI_Controller {
public function __construct() {
parent:: __construct();
$this->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());
}
}
}
Add the bellow routes to application/config/routes.php
$route['send-email'] = 'your email controller';
$route['email'] = 'your email controller/send';
PHP Mail Attachment – Php Send Email File-Images Attachments
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.