How to Send Email using CodeIgniter?
Whether you are working in a corporate or in a small firm, you will need Email to either record your conversations as a proof or to send the formal information online.
It has become very prominent in the web applications these days. In order to verify a new user on the website it is important that their Email address should be verified by sending them an email to verify their email address and then confirm their subscription.
There are various use of the Email on a website like, suppose a user forgets his password then a reset password link will be sent over to the user’s email address, in order to send the invoices and the receipts to various customers, and many more.
Now we can send email in codeigniter as it is extremely easy to send emails right from your applications and also by using a way lot of options. CodeIgniter email library is a built in library and programmers can work on that when sending the emails.
CodeIgniter Email Configuration
A central place is needed where the programmers can manage the email settings. There is no config file for emails that comes with the codeIgniter hence, the programmers will have to create one for themselves.
We have to create a file in the email.php directory application/config
To do so, the programmer needs to add the following depicted code to themail.php
<?php
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.example.com',
'smtp_port' => 465,
'smtp_user' => 'phptpoint@example.com
',
'smtp_pass' => '123456',
'smtp_crypto' => 'ssl',
'mailtype' => 'text',
'smtp_timeout' => '4',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
?>
Here is the breakdown of the code that what each function mean:
- 'protocol'--- while sending an email which protocol is to be used is specified by the ‘smtp’. It can be Gmail smtp settings or the smtp settings from your host
- 'smtp_host'--- 'smtp.example.com', is generally specifying the smtp host.
- 'smtp_port'--- 465, This is an open port that is on the specified smtp host that has been basically configured for the smtp mail
- 'smtp_user'--- 'phptpoint@example.com', this is the email address that will be basically used as the sender whenever any email is sent. This Email address would be very simple and is verified and preferably exist on the server.
- 'smtp_pass'--- '12345!', This is the password of the specified smtp user email
- 'smtp_crypto' --- 'ssl', This is generally used to specify the encryption method that is to be used i.e. ssl, tls etc.
- 'email type' ---- 'text', This is used to set the type of the email that is to be used. This text can generally be either a plain text or HTML that will totally depend on your needs.
- 'smtp_timeout' --- '4', This is used to specify the time in seconds that should be the maximum whenever there is a situation of a timeout.
- 'charset' --- 'iso-8859-1', This is used to define the character set that is to be used whenever sending the emails.
- 'wordwrap'-- The word-wrap is enabled just after the TRUE is set to TRUE . Word-wrap is not considered enabled if the set value is FALSE.
Please Note: In order to send the emails to work, there should be valid configuration parameters provided by the programmer. Because the dummy parameters that are generally set will not be able to send emails.
CodeIgniter Email View
Now that we have configured the Email, we will now create the view that will send the email to the recipient.
First step is to create a new directory of email in the application/views
After the first step, there is a need to create a new file contact.php application/views/email
Just add the following mentioned code to the application/views/email/contact.php
<!DOCTYPE html>
<html>
<head>
<title>CodeIgniter Email Send using SMTP</title>
</head>
<body>
<div>
<h3>Use the form below to send email</h3>
<form method="post" >
<input type="email" name="to" placeholder="Enter Receiver Email">
<br><br>
<input type="text" name="subject" placeholder="Enter Subject">
<br><br>
<textarea rows="6" name="message" placeholder="Enter your message here"></textarea>
<br><br>
<input type="submit" value="Send Email" />
</form>
</div>
</body>
</html>
In the above mentioned code there is a basic HTML form that will accept the email, subject and message then the parameters is passed to the email route.
CodeIgniter Email Controller
Now we will create the controller for handling the sent emails
The first step will create a new file EmailController.php in application/controllers/EmailController.php
Please add the following depicted code to the EmailController.php
<?php
class EmailController extends CI_Controller
{
public function __construct()
{
parent:: __construct();
$this->load->helper('url');
}
public function index()
{
$this->load->view('email');
}
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 'Email has been sent successfully';
}
else
{
show_error($this->email->print_debugger());
}
}
}
?>
Here is the breakdown of the code depicted below with a description of the codes:
- lass EmailController extends CI_Controller {…}: This is basically used to define the email controller that generally expands the parent CodeIgniter controller.
- public function __construct() {…} This is basically used to define the child constructor that generally calls the parent constructor method.
- public function index() {…} This is basically used to define the index method that is being displayed on the contact form
- function send() {…} This is basically used to define the method that will send the email
- $this->load->config('email'); This function is basically used to load the email configuration settings
- $this->load->library('email'); This function is basically used to load the email library
- $from = $this->config->item('smtp_user'); This function is basically used to get the sender id from the email configuration file that we defined.
- $to = $this->input->post('to'); This function is basically used to get the to value from the submitted form
- $subject = $this->input->post('subject'); This function is basically used to set the email subjected from the form
- $message = $this->input->post('message'); This function is basically used to set the email message from the form
- $this->email->set_newline("\r\n"); This function is basically used to define the new line characters for emails
- $this->email->from($from); This function is basically used to set the sender email address
- $this->email->to($to); This function is basically used to set the recipient email address
- $this->email->subject($subject); This function is basically used to set the email subject
- $this->email->message($message); This function is basically used to set the email message
The message ‘Your Email has successfully been sent’, if the email has been sent successfully else debug information will be printed depicting what might have gone wrong.
After doing all this there is a need to define the email routes
Email Routes
Please add the following depicted routes to application/config/routes.php
$route['send-email'] = 'email controller';
$route['email'] = 'email controller/send';
After getting this now the users can load the contacts form in the web browser
Congratulations you have now sent an Email in the CodeIgniter. To learn more about these things learn our codeIgniter tutorial. Just logon to phptopoint.com.
Download Free PHP Projects Here - PHP Projects Free Download With Source Code
Comments
Post a Comment