Sending Email in Codeigniter

Inside Controller :

<?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' => 'ssl://smtp.gmail.com',
      'smtp_port' => '465',
      'smtp_user' => 'balamuralim111@gmail.com',
      'smtp_pass' => 'la9014809163ba',
      'mailtype' => 'html',
      'charset' => 'utf-8',//iso-8859-1
      'newline'   => '\r\n',
      'validation'=> TRUE,
      '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('balamuralim111@gmail.com');
    $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']))     //using file_helper
        {
        $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();
}
}
}


Inside View :

<!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

Set Base URL in config file in Codeigniter

Form Validation Using Javascript

Image Display Inside Form