Skip to main content

How to Send Email with Attachment in Codeigniter

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>



Comments

Popular posts from this blog

Insert Fetch Update Delete Mysql data in Codeigniter

Database -- -- Table structure for table `tbl_user` -- CREATE TABLE IF NOT EXISTS `tbl_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `first_name` varchar(200) NOT NULL, `last_name` varchar(200) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=95 ; -- -- Dumping data for table `tbl_user` -- INSERT INTO `tbl_user` (`id`, `first_name`, `last_name`) VALUES (91, 'Harold', 'Jones'), (89, 'Christine', 'Smith'), (88, 'Marker', 'Angela'), (87, 'Romeo', 'Mary'), (86, 'Smith', 'John'); Controller - main.php <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Main extends CI_Controller { //functions public function index(){ $this->load->model("main_model"); $data["fetch_data"] = $this->main_model->...

How to Generate Excel File in Codeigniter using PHPExcel

Libraries/Excel.php <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); require_once('PHPExcel.php'); class Excel extends PHPExcel { public function __construct() { parent::__construct(); } } ?> Libraries/IOFactory.php <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); require_once('PHPExcel/IOFactory.php'); class IOFactory extends PHPExcel_IOFactory { public function __construct() { parent::__construct(); } } ?> Controllers/Excel_export.php <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Excel_export extends CI_Controller { function index() { $this->load->model("excel_export_model"); $data["employee_data"] = $this->excel_export_model->fetch_data(); $this->load->view("excel_export_view", $data); } function action() { $this->load->model(...

Core php login logout and register view delete

Core php login logout and register view delete Database.php CREATE TABLE IF NOT EXISTS register.`users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `email` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, `trn_date` datetime NOT NULL, PRIMARY KEY (`id`) ); CREATE TABLE IF NOT EXISTS register.`new_record` ( `id` int(11) NOT NULL AUTO_INCREMENT, `trn_date` datetime NOT NULL, `name` varchar(50) NOT NULL, `age`int(11) NOT NULL, `submittedby` varchar(50) NOT NULL, PRIMARY KEY (`id`) ); Auth.php <?php session_start(); if(!isset($_SESSION["username"])){ header("Location: login.php"); exit(); } ?> Dashbaord .php <!DOCTYPE html> <html lang="en"> <head> <title>Dashboard</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.boo...