Saturday, 24 February 2018

How to Send Email with Attachment in Codeigniter



After learning Send email with Attachment using PHP Mailer, now in this post we have discuss how to send email with attachment in Codeigniter. Because we all know Codeigniter is robust framework for developing any web application and it has it's own Email library for send an email from application. So, here we have seen Codeigniter send email using SMTP with file attachment.

This is MVC framework, so here we have to create two file like Controllers and Views. Controller files will received request from browser and it will send request to view file and view file will display output on browser. That means first times when controller will load in browser then it will called index method and in that method it will load view file. After this user will fill view file form and click on submit button then view file will send request to controller method and in that there has been two process has been done. Because Codeigniter Send Email with Attachment, for this first we have to know how to upload file in Codeigniter. For Attach file with email send in Codeigniter we have to first upload file to folder then after we can attachment file with mail.

For this topic like send html email in Codeigniter with attachment we have use two library like Email and Upload of Codeigniter. File will be uploaded by using upload library and email send with attachment has been use email library. For attach uploaded file with email here we have use attach() method of email library. We all know send email from web application is required feature of any web application. And Sometime with email file attachment is also required. So here we have make this tutorial on Codeigniter Send Email with Attachment. Below you can find complete source code.






Source Code


Sendemail.php - Controllers



<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Sendemail extends CI_Controller {

 public function index()
 {
  $this->load->view('sendemail');
 }

 public function send()
 {
  $subject = 'Application for Programmer Registration By - ' . $this->input->post("name");
  $programming_languages = implode(", ", $this->input->post("programming_languages"));
  $file_data = $this->upload_file();
  if(is_array($file_data))
  {
   $message = '
   <h3 align="center">Programmer Details</h3>
    <table border="1" width="100%" cellpadding="5">
     <tr>
      <td width="30%">Name</td>
      <td width="70%">'.$this->input->post("name").'</td>
     </tr>
     <tr>
      <td width="30%">Address</td>
      <td width="70%">'.$this->input->post("address").'</td>
     </tr>
     <tr>
      <td width="30%">Email Address</td>
      <td width="70%">'.$this->input->post("email").'</td>
     </tr>
     <tr>
      <td width="30%">Progamming Language Knowledge</td>
      <td width="70%">'.$programming_languages.'</td>
     </tr>
     <tr>
      <td width="30%">Experience Year</td>
      <td width="70%">'.$this->input->post("experience").'</td>
     </tr>
     <tr>
      <td width="30%">Phone Number</td>
      <td width="70%">'.$this->input->post("mobile").'</td>
     </tr>
     <tr>
      <td width="30%">Additional Information</td>
      <td width="70%">'.$this->input->post("additional_information").'</td>
     </tr>
    </table>
   ';

   $config = Array(
         'protocol'  => 'smtp',
         'smtp_host' => 'smtpout.secureserver.net',
         'smtp_port' => 80,
         'smtp_user' => 'xxxxxxxxxx', 
         'smtp_pass' => 'xxxxxxxxxx', 
         'mailtype'  => 'html',
         'charset'  => 'iso-8859-1',
         'wordwrap'  => TRUE
      );
   //$file_path = 'uploads/' . $file_name;
      $this->load->library('email', $config);
      $this->email->set_newline("\r\n");
      $this->email->from($this->input->post("email"));
      $this->email->to('web-tutorial@programmer.net');
      $this->email->subject($subject);
         $this->email->message($message);
         $this->email->attach($file_data['full_path']);
         if($this->email->send())
         {
          if(delete_files($file_data['file_path']))
          {
           $this->session->set_flashdata('message', 'Application Sended');
           redirect('sendemail');
          }
         }
         else
         {
          if(delete_files($file_data['file_path']))
          {
           $this->session->set_flashdata('message', 'There is an error in email send');
           redirect('sendemail');
          }
         }
     }
     else
     {
      $this->session->set_flashdata('message', 'There is an error in attach file');
         redirect('sendemail');
     }
 }



 function upload_file()
 {
  $config['upload_path'] = 'uploads/';
  $config['allowed_types'] = 'doc|docx|pdf';
  $this->load->library('upload', $config);
  if($this->upload->do_upload('resume'))
  {
   return $this->upload->data();   
  }
  else
  {
   return $this->upload->display_errors();
  }
 }
}







sendemail.php - Views



<!DOCTYPE html>
<html>
  <head>
    <title>How to Send an Email with Attachment in Codeigniter</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  </head>
  <body>
    <br />
    <div class="container">
      <div class="row">
        <div class="col-md-8" style="margin:0 auto; float:none;">
          <h3 align="center">How to Send an Email with Attachment in Codeigniter</h3>
          <br />
          <?php

          if($this->session->flashdata("message"))
          {
            echo "
            <div class='alert alert-success'>
              ".$this->session->flashdata("message")."
            </div>
            ";
          }
          ?>
          <h4 align="center">Programmer Register Here</h4>
          <br />          
          <form method="post" action="<?php echo base_url(); ?>sendemail/send" enctype="multipart/form-data">
            <div class="row">
              <div class="col-md-6">
                <div class="form-group">
                  <label>Enter Name</label>
                  <input type="text" name="name" placeholder="Enter Name" class="form-control" required />
                </div>
                <div class="form-group">
                  <label>Enter Address</label>
                  <textarea name="address" placeholder="Enter Address" class="form-control" required></textarea>
                </div>
                <div class="form-group">
                  <label>Enter Email Address</label>
                  <input type="email" name="email" class="form-control" placeholder="Enter Email Address" required />
                </div>
    <div class="form-group">
                  <label>Select Programming Language</label>
                  <select name="programming_languages[]" class="form-control" multiple required style="height:150px;">
                    <option value=".NET">.NET</option>
                    <option value="Android">Android</option>
                    <option value="ASP">ASP</option>
                    <option value="Blackberry">Blackberry</option>
                    <option value="C">C</option>
                    <option value="C++">C++</option>
                    <option value="COCOA">COCOA</option>
                    <option value="CSS">CSS</option>
                    <option value="DHTML">DHTML</option>
                    <option value="Drupal">Drupal</option>
                    <option value="Flash">Flash</option>
                    <option value="HTML">HTML</option>
                    <option value="HTML 5">HTML 5</option>
                    <option value="IPAD">IPAD</option>
                    <option value="IPHONE">IPHONE</option>
                    <option value="Java">Java</option>
                    <option value="Java Script">Java Script</option>
                    <option value="Joomla">Joomla</option>
                    <option value="LAMP">LAMP</option>
                    <option value="Linux">Linux</option>
                    <option value="MAC OS">MAC OS</option>
                    <option value="Magento">Magento</option>
                    <option value="MySQL">MySQL</option>
                    <option value="Oracle">Oracle</option>
                    <option value="PayPal">PayPal</option>
                    <option value="Perl">Perl</option>
                    <option value="PHP">PHP</option>
                    <option value="Ruby on Rails">Ruby on Rails</option>
                    <option value="Salesforce.com">Salesforce.com</option>
                    <option value="SEO">SEO</option>
                  </select>
                </div>
              </div>
              <div class="col-md-6">
                <div class="form-group">
                  <label>Select Year of Experience</label>
                  <select name="experience" class="form-control" required>
                    <option value="">Select Experience</option>
                    <option value="0-1 years">0-1 years</option>
                    <option value="2-3 years">2-3 years</option>
                    <option value="4-5 years">4-5 years</option>
                    <option value="6-7 years">6-7 years</option>
                    <option value="8-9 years">8-9 years</option>
                    <option value="10 or more years">10 or more years</option>
                  </select>
                </div>
                <div class="form-group">
                  <label>Enter Mobile Number</label>
                  <input type="text" name="mobile" placeholder="Enter Mobile Number" class="form-control" pattern="\d*" required />
                </div>
                <div class="form-group">
                  <label>Select Your Resume</label>
                  <input type="file" name="resume" accept=".doc,.docx, .pdf" required />
                </div>
                <div class="form-group">
                  <label>Enter Additional Information</label>
                  <textarea name="additional_information" placeholder="Enter Additional Information" class="form-control" required rows="8"></textarea>
                </div>
              </div>
            </div>
            <div class="form-group" align="center">
              <input type="submit" name="submit" value="Submit" class="btn btn-info" />
            </div>
          </form>
        </div>
      </div>
    </div>
  </body>
</html>

1 comment: