Web Programming: How to Send Email With Attachment Using CodeIgniter Framework

in #education6 years ago

Hi Everyone!

Welcome to my first random tutorial about web programming. We will conduct a step by step process today on how to Send an Email With Attachment Using CodeIgniter Framework. I made this tutorial because I was once in the process of finding some approach on how I can send emails to our technicians regarding with their documents that we currently have in our system.

Overview
You must have a background in web programming and in using CodeIgniter framework. I choose CodeIgniter Framework since I have a better understanding of how it works and I have used this already for some years.

What is Web Programming?

Web development or Web Programming is a broad term for the work involved in developing a website for the Internet or an intranet.

What is CodeIgniter?

CodeIgniter is an open-source software rapid development web framework, for use in building dynamic websites with PHP.

As you have read the overview, so we need these skills in aiming our first goal which is to send an email with an attachment.

So, let's start.

First, we need to define or set our SMTP configurations. You can get the SMTP settings by your hosting so make sure you have it for you successfully be able to complete this tutorial.

CONFIGURATION SETTINGS
In your controller, create a function to initialize all your STMP configurations. Refer below for the representation. Your code will be something like of the function below. You just have to edit the necessary configs as per required. Please consider all backslash "//" as a comment so you would know the purpose of each line.

public function config_init(){
$config = array(
'protocol' => 'smtp', //The mail sending protocol.
'smtp_host' => 'smtp.office365.com', //SMTP Server Address.
'smtp_user' => '[email protected]', //SMTP Username.
'smtp_pass' => 'TruthsfinderPass', //Password
'smtp_crypto' => 'tls', //SMTP Encryption
'newline' => "\r\n", //Newline character. (Use “\r\n” to comply with RFC 822).
'smtp_port' => 587, //SMTP Port.
'mailtype' => 'html', //Type of mail.
'charset' => 'utf-8', //Character set (utf-8, iso-8859-1, etc.).
'wordwrap' => true //Enable word-wrap.
);
return $config;
}

Next, you will have to create your send email request template. This function will still be implemented in your controllers. This function will accept the configurations of your SMTP, the receiver of the email, the sender of the email, the name of the sender, the subject of the email, the message that you want to set, and the file that is being attached while sending the email.

public function send_mail_requests_with_attachments($config, $receiver, $sender_email, $sender_name, $subject, $message, $file){
$this->email->initialize($config); //This will initialize the STMP configurations that you set above.
$this->email->set_newline("\r\n"); //Newline character.
$this->email->set_crlf( "\r\n" ); //Newline character.
$formatted_message = "< p >" . nl2br($message) . "</ p >"; //Formats the message to receive < br > and " \n "
$this->email->to($receiver); //This will set the receiver of the email
$this->email->from($sender_email, $sender_name); //This set the sender of the email
$this->email->subject($subject); //This is the subject of the email
$this->email->message($formatted_message); //This will be your message, it is formatted above to display better
$this->email->attach($file); //This will hold the file of the email attachment
//Then we have to check if the email was sent to the receiver
if ( ! $this->email->send())
{
//This will display the error encountered upon sending the request.
echo $this->email->print_debugger();
}else{
//This will echo success if the email is successfully sent to the receiver.
echo "success ";
}
//Initializes all the email variables to an empty state.
$this->email->clear(true);
}

After you created these configurations, we can now use these functions for us to send a message to anyone.

But before that, I would like you to grab an idea of how my user interface will look like. Since I am only focusing on the send functionalities, I just only show the user interface I have but if you happen to read my blog and wanted to have my code for the design, just leave a comment and I will show you immediately as possible.

Since we have our configurations ready, we will now have the actual function call.

$file = realpath(APPPATH.'../asset/requests')."/".'attachment.docx';
//Basically the location of your attachment
$this->send_mail_requests_with_attachments($this->config_init(), "receiver_email_address", "sender_email_address", "receiver_name", "subject", "message", $file);

Basically, these lines of codes will save you hundred of your times and will successfully be able to send your email to your receiver. If you have questions, please let me know in the comment. I hope that this will be really helpful to everyone that urgently need something like this. Goodluck!!

Sort:  

Congratulations! This post has been upvoted by the communal account, @steemph.cebu by truthsfinder being run at Teenvestors Cebu (Road to Financial Freedom Channel). This service is exclusive to Steemians following the Steemph.cebu trail at Steemauto. Thank you for following Steemph.cebu curation trail!

Don't forget to join Steem PH Discord Server, our Discord Server for Philippines.

You have been upvoted by the @sndbox-alpha! Our curation team is currently formed by @jeffbernst, @bitrocker2020, @jrswab & @teachblogger . We are seeking posts of the highest quality and we deem your endeavour as one of them. If you want to get to know more, feel free to check our blog.

Thank you so much!! :)

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.034
BTC 64231.88
ETH 3128.59
USDT 1.00
SBD 3.95