Sending Email using CodeIgniter
0 2144
In current days, Email sending is a very necessary activity of web applications. We need to send emails to interact with users. It is very useful to verify a user in the sign-in/sign-up system.
In e-commerce projects, emails are used to update the users about the products and to send them all necessary information and documents such as purchased items description and invoices.
Related Topics:
Codeigniter Interview Questions
Login form in CodeIgniter
Create registration form in Codeigniter
Sending Email:
As compared to core PHP, email sending is easier in Codeigniter. Codeigniter provides an Email library for sending emails. This library has the following features:
- Multiple Protocols: Mail, Sendmail, and SMTP
- TLS and SSL Encryption for SMTP
- Multiple recipients
- CC and BCCs
- HTML or Plaintext email
- Attachments
- Word wrapping
- Priorities
- BCC Batch Mode, enabling large email lists to be broken into small BCC batches.
- Email Debugging tools
Email configuration: In Codeigniter, there is no configuration file that can handle all email settings. To create a configuration file, open directory application/config and create a file email.php.
email.php
<?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
);
?>
Parameter description:
- the protocol represents the SMTP protocol that is used to sending emails.
- smtp_host is used to set SMTP host.
- smtp_port represents the SMTP port configured for sending SMTP mails. 465 is an open port.
- smtp_user represents an email address that is used as the sender’s email address.
- smtp_pass represents the password of the email ID given to sending emails in smtp_user.
- smtp_crypto represents the encrypting method that is used to sending emails.
- mail type used to set the email type that is used in the email body. It may be text or HTML.
- smtp_timeout is used to set the time limitations in seconds during connecting to the host.
- charset represents the character set that is used to sending emails.
- wordwrap is a Boolean value that is used to enable and disable the word wrap. Word wrap is enabled on TRUE and disabled on FALSE.
Class Reference:
Following functions are used by the Email class to make the email sending process easy and efficient.
1 from(): This function is used to set the email address and the name of the person sending the email.
Syntax:
from($from[, $name = ''[, $return_path = NULL]]);
Parameter Description:
- $from (string) – "From" e-mail address
- $name (string) − "From" display name
- $return_path (string) − Optional email address to redirect undelivered e-mail to
This function returns CI_Email instance (method chaining) and the return type of this function is CI_Email.
2 reply_to(): This function is used to set the reply-to address and name.
Syntax:
reply_to($replyto[, $name = '']);
Parameter description:
This function returns CI_Email instance (method chaining) and the return type of this function is CI_Email.
3 to(): This function is used to set the email address of the receiver. It can be a single e-mail or an array.
syntax:
to($to);
Parameter description:
This function returns CI_Email instance (method chaining) and the return type of this function is CI_Email.
4 cc(): This function is used to set the cc e-mail address. It can also be a single e-mail address or an array of email addresses.
Syntax:
cc($cc);
Parameter description:
This function returns CI_Email instance (method chaining) and the return type of this function is CI_Email.
5 bcc(): This function is used to set the bcc e-mail address. It can also be a single e-mail address or an array of addresses.
Syntax:
bcc($bcc[, $limit = '']);
parameter description:
This function returns CI_Email instance (method chaining) and the return type of this function is CI_Email.
6 subject(): This function is used to set the subject of the e-mail.
Syntax:
subject($subject);
parameter description:
This function returns CI_Email instance (method chaining) and the return type of this function is CI_Email.
7 message(): This function is used to set the e-mail message body.
Syntax:
message($body);
parameter description:
This function returns CI_Email instance (method chaining) and the return type of this function is CI_Email.
8 set_alt_message(): This function is used if you send HTML formatted e-mail. It is optional.
Syntax:
set_alt_message($str);
parameter description:
This function returns CI_Email instance (method chaining) and the return type of this function is CI_Email.
9 set_header(): This function is used to append the additional header to the e-mail.
Syntax:
set_header($header, $value);
parameter description:
This function returns CI_Email instance (method chaining) and the return type of this function is CI_Email.
10 clear(): This function is used to initialize all the e-mail variables to an empty state. This function is used when we are sending e-mails in a loop.
Syntax:
clear([$clear_attachments = FALSE])
Parameter description:
This function returns CI_Email instance (method chaining) and the return type of this function is CI_Email.
11 send(): This e-mail sending method is used conditionally and it returns TRUE or FALSE, on the basis of success or failure.
Syntax:
send([$auto_clear = TRUE]);
parameter description:
12 attach(): This function is used to enable sending an attachment.
Syntax:
attach(name[, $disposition = ''[, $newname = NULL[, $mime = '']]]);
parameter description:
This function returns CI_Email instance (method chaining) and the return type of this function is CI_Email.
13 attachment_cid(): This function is used to set and return an attachment’s content ID, which enables us to embed an inline (picture) attachment into HTML.
Syntax:
attachment_cid(name);
Parameter description:
This function returns the attachment’s content ID or FALSE if not found and the return type of this function is a string.
Example:
Step 1 Open directory application/views and create a file email_view.php.
email_view.php
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Sending E-mail Form</h2>
<form method="post" enctype="multipart/formdata" action="<?=base_url('Email_controller/send')?>" style="width:40%" >
<div class="form-group">
<label for="email">Receiver's E-mail:</label>
<input type="text" class="form-control" name="to" placeholder="Enter E-mail">
</div>
<div class="form-group">
<label for="subject">Subject:</label>
<input type="text" class="form-control" name="subject" placeholder="Enter Subject">
</div>
<div class="form-group">
<label for="Message">Message:</label>
<input type="text" class="form-control" name="message" placeholder="Enter Message">
</div>
<input type="submit" name="send" class="btn btn-success" value="Send"/>
</form>
</div>
</body>
</html>
Step 2 Open directory application/controllers and create a controller file Email_controller.php.
<?php
class Email_controller extends CI_Controller
{
public function __construct()
{
parent:: __construct();
$this->load->helper('url');
}
public function index()
{
$this->load->view('email_view');
}
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());
}
}
}
?>
Step 3 Enter the given URL into the browser.
http://localhost/ci/index.php/Email_controller
Fill above form and click on the Send button.
Share:
Comments
Waiting for your comments