CodeIgniter Email Class

Facebooktwitterredditpinterestlinkedinmailby feather

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email preferences as per needs.

An email class contains the following features:

  • It supports Multiple Protocols such as TLS, Mail, Send mail and SMTP (Simple Mail Transfer Protocol)
  • TLS and SSL Encryption
  • Multiple recipients
  • CC and BCCs
  • HTML or plaintext email
  • Attachments
  • Word wrapping
  • Priorities
  • BCC Batch Mode, it permits to broke large email lists into sub BCC batches.
  • Email Debugging tools

Load an Email Class

Before using the email class in Codeigniter, you must load in the controller file by following the

Syntax:

Various function of email class that helps to send an email

  1. from(): As we know, the from () function indicates the personal ID of the user who is sending an email to another person via email.

Syntax

It has three parameters:

$from (string): It denotes the user email address.

$name: It is an optional parameter. If you want to display your name in an email, you can pass the name in this field.

$return_path (string): In this parameter, you can set an alternate email address to redirect the mail.

  • reply_to(): As the name suggests, it helps to reply to a mail by defining an email address to the relpy_to() function.

Syntax

It has two parameters:

$reply_to: It defines an email address to reply the same user.

$name: It displays the username along with the email address.

  • to(): The to() function indicates the name of the recipient email address. And if you want to send a message to more than one user, then you need to define all the email in to() function.

Syntax

$to: It defines the email address of the recepint. You can also pass multiple email addresses by using a comma-delimited list or an array.

  • cc(): The cc() function is used to send carbon copies of the mail to other people, besides the main recipients.

Syntax

$cc: It contains the recipients’ email addresses, and if you want to share email more than one person, you can add the multiple email addresses separated by comma-delimiter or an array.

  • bcc(): A bcc standsfor Blind Carbon Copy. It is different from cc() and to() function, as in this function the main recipient couldn’t know about the bcc recipients.

Syntax

$bcc: It takes an email address for creating a blind carbon copy.

  • subject(): As the name defines, a subject() function is used to set the purpose of an email.

Syntax

$subject: It is used as a variable for defining the subject of an email.

  • message(): It is like the message body of an email in which the sender or receiver writes the messages.

Syntax

$body: It provides an area to write the content or message that you want to share with another person.

  • set_alt_message(): As the name suggests, a set_alt_message() function is an optional message string that is used to send an alternate email text when the receiver does not receive the particular message.

Syntax

$str: In this field, you can pass an alternative string.

  • set_header(): A set_header() function is used to set headers in emails. It also allows adding more than one header to an email.

Syntax

It contains two parameters:

$header:  It defines the header name of an email.

$value: It contains the header value of an email.

  1. clear(): As the function defines, it is used to clear all attached documents that are used to send an email in a loop.

Syntax

$clear_attachements: It uses a Boolean value that indicates whether you want to delete the attachment.

11. send(): As the name suggests, a send () function is an email sending method that returns a true or false value based on the success or failure of the email.

Syntax

$auto_clear: It uses a Boolean value to automatically clear sent messages.

12. attach(): The attach () function is used to attach JPEG, PDF, text, etc. to an email while sending any type of document to an individual.

Syntax

It has four parameters:

$filename: It defines the filename that you want to attached to an email.

$disposition: It defines the disposition of an attachment.

$newname: It allows to add a custom filename when sending an email.

$mime: It defines the type of file that you want to attach to the email.

13. print_debugger(): It is a function that is used to show an error message when an email is not sent to the recipients’ address.

Syntax

$include: It defines the part of the message that you want to display in the email.

Create an email system using Email class

Create a controller file Send_mail.php and save it in application/controller/Send_mail.php. After that, write the following program in the controller file.

Send_mail.php

Create a sent2.php view file and save it into an application/views/. After that, write the following program in the controller file.

Sent2.php

Create a sent3.php view file and save it into an application/views/. After that, write the following program in the controller file.

Sent3.php

Before executing the program, you have to follow these instructions, such as:

1. You have to log in with your Gmail id and click on the (six dotted) top right corner of the Gmail, it shows a pop-up box and then clicks on the account option, it shows a below screen in your system.

2. And, click on the Security option of the Gmail account and then scroll the page, it shows the below screen.

Email Class

3. After that, click on the Turn on access (not recommended) link; it redirects you to the next page, as shown in the below image.

Email Class

Now execute the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/Send_mail; it shows the output, as shown below.

Email Class

After filling the email details such as email id, subject, and message, and then click on the Send Message button, it shows the output as shown below:

Facebooktwitterredditpinterestlinkedinmailby feather