Codeigniter

Send HTML Email With Image In CodeIgniter.

Sending an Email is a very common functionality in any web application. For example, we can send welcome Email when user register on our website, send a newsletter Email, sending an invoice, sending a birthday wishes. We can send either text mail or html Email. For better user experience we should prefer html Email. So In this post we will learn how we can send html Email with image in CodeIgniter.

Sending an Email in CodeIgniter is much easier because it is robust MVC framework so it has an inbuilt Email library to sending email in application. We can send text Email as well as HTML Email using it’s inbuilt Email library.

It’s robust Email class supports the following features:

1Multiple Protocols: Mail, Sendmail, and SMTP.
2TLS and SSL Encryption for SMTP.
3Multiple recipients.
4CC and BCCs.
5HTML or Plaintext email.
6Attachments.
7Word wrapping.
8Priorities
9BCC Batch Mode, enabling large email lists to be broken into small BCC batches.
10Email Debugging tools.

Below are the steps to send HTML Email with image in CodeIgniter.

Load The Email Library First.
$this->load->library('email');

Create Controller.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class HtmlEmail extends CI_Controller {

   public function index(){
	   
           $config['mailtype'] = 'html';    
           $this->load->library('email'); 
	   $data = array('name' => 'PHPFOREVER');
	   $from_email = "contact@phpforever.com"; 
           $to_email = 'test@testemail.com';            
           $this->email->from($from_email, 'PHPFOREVER'); 
           $this->email->to($to_email);
           $this->email->set_header('Content-Type', 'text/html');
           $this->email->subject('Email Test'); 
           $this->email->message($this->load->view('html_mail',$data,true));   
           if($this->email->send()){            
            echo "Mail Sent";
           }else{ 
            echo "Error";
          }
   }
}

Create View.
<html>
<head>
    <style type='text/css'>
        body {font-family: Verdana, Geneva, sans-serif}
        p {font-weight:bold}
    </style>
</head>
<body>
    <h3>Hi <?php echo $name;?>,</h3>
    <h3>Welcome to CI HTML Email Tutorial.</h3> 
    <img src='<?=base_url()?>/public/Desert.jpg'>
    <p>In this post we will learn, how we can send HTML email in codeigniter.</p>   
</body>
</html>

Send Email To Multiple Recipients.
$toarray = array('test1@testemail.com','test2@testemail.com','test3@testemail.com',
'test4@testemail.com');
$this->email->to($to_email);


Set The CC And BCC.
$this->email->cc('cc@testemail.com');
$this->email->bcc('bcc@testemail.com');

Rest API In Codeigniter – 3. Codeigniter – Autocomplete Search using Typeahead

Related Articles

Leave a Reply

Back to top button