Cara Mengirim Attachment ke Email Menggunakan Codeigniter

Attcahment email adalah lampiran berupa file yang masih merupakan bagian dari isi email. Kali ini saya akan membuat Cara Mengirim Attachment ke Email Menggunakan Codeigniter. Sebelum kita membuat struktur htmlnya, teman-teman isi terlebih dahulu libraries dan helper di dalam file autoload.php seperti di bawah ini.

$autoload['libraries'] = array('session');

$autoload['helper'] = array('url','file');

Jika sudah selesai maka langkah selanjutnya untuk Cara Mengirim Attachment ke Email Menggunakan Codeigniter yaitu buat struktur html di dalam folder views dengan nama sendemail.php seperti script di bawah ini.

<?php

if($this->session->flashdata("message")){

  echo "<div class='alert alert-success'>

        ".$this->session->flashdata("message")."

        </div>";

}

?>

<!DOCTYPE html>

<html>

  <head>

    <title>Cara Mengirim Attachment ke Email Menggunakan Codeigniter</title>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

  </head>

  <body>

    <div class="container">

      <div class="row">

        <div class="col-md-8" style="margin:0 auto; float:none;">

          <h3 align="center">Cara Mengirim Attachment ke Email Menggunakan Codeigniter</h3>

          <div class="registrasi border p-2">

            <h4 align="center">Form Registrasi Lamaran</h4>     

            <form method="post" action="http://localhost/shelli/codeigniter/ARTICLE/send_attach/index.php/sendemail/send" enctype="multipart/form-data">

              <div class="row">

                <div class="col-md-6">

                  <div class="form-group">

                    <label>Nama</label>

                    <input type="text" name="name" placeholder="Masukan Nama" class="form-control" required />

                  </div>

                  <div class="form-group">

                    <label>Alamat</label>

                    <textarea name="address" placeholder="Masukan Alamat" class="form-control" required></textarea>

                  </div>

                  <div class="form-group">

                    <label>Alamat Email</label>

                    <input type="email" name="email" class="form-control" placeholder="Masukan Alamat Email" required />

                  </div>

                  <div class="form-group">

                    <label>Bahasa Pemprograman</label>

                    <select name="programming_languages[]" class="form-control" multiple required style="height:150px;">

                      <option value="Android">Android</option>

                      <option value="C">C</option>

                      <option value="C++">C++</option>

                      <option value="CSS">CSS</option>

                      <option value="Flash">Flash</option>

                      <option value="HTML">HTML</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="MySQL">MySQL</option>

                      <option value="Oracle">Oracle</option>

                      <option value="PHP">PHP</option>

                      <option value="SEO">SEO</option>

                    </select>

                  </div>

                </div>

                <div class="col-md-6">

                  <div class="form-group">

                    <label>Pengalaman Kerja</label>

                    <select name="experience" class="form-control" required>

                      <option value="">-Pilih-</option>

                      <option value="0-1 tahun">0-1 tahun</option>

                      <option value="2-3 tahun">2-3 tahun</option>

                      <option value="4-5 tahun">4-5 tahun</option>

                      <option value="6-7 tahun">6-7 tahun</option>

                      <option value="8-9 tahun">8-9 tahun</option>

                      <option value=">10 tahun">>10 tahun</option>

                    </select>

                  </div>

                  <div class="form-group">

                    <label>No. Handphone</label>

                    <input type="text" name="mobile" placeholder="Masukan No. Handphone" class="form-control" pattern="\d*" required />

                  </div>

                  <div class="form-group">

                    <label>Lampirkan CV</label>

                    <input type="file" name="resume" accept=".doc,.docx, .pdf" required />

                  </div>

                  <div class="form-group">

                    <label>Keterangan</label>

                    <textarea name="additional_information" placeholder="Keterangan Diri" 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>

    </div>

  </body>

</html>

Jika sudah selesai maka teman-teman buat fungsi didalam folder controller dengan nama file Sendemail.php seperti di bawah ini.

<?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"); //subjek di pesan email

    $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.googlemail.com', //host gmail

           'smtp_port' => 465, //port gmail

           'smtp_user' => 'xxxxxx', //masukan alamat gmail kamu

           'smtp_pass' => 'xxxxxx, //masukkan password gmail kamu

           'mailtype'  => 'html', //tipe email

           'charset'  => 'iso-8859-1',

           'wordwrap'  => TRUE

      );

      $this->load->library('email', $config);

      $this->email->set_newline("\r\n");

      $this->email->from($this->input->post("email")); //masukkan alamat email pengirim

      $this->email->to('shelliripati.dumet@gmail.com'); //masukkan alamat gmail kamu / email yang dituju

      $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', 'Lamaran Terkirim');

           redirect('sendemail');

          }

         }else{

          if(delete_files($file_data['file_path'])){

           $this->session->set_flashdata('message', 'Pengiriman Lamaran Error');

           redirect('sendemail');

          }

         }

     }else{

      $this->session->set_flashdata('message', 'Attach File Error ');

         redirect('sendemail');

     }

  }

  function upload_file(){

    $config['upload_path'] = 'uploads/';

    $config['allowed_types'] = 'doc|docx|pdf'; //tipe file attach

    $this->load->library('upload', $config);

    if($this->upload->do_upload('resume')){

     return $this->upload->data();  

    }else{

     return $this->upload->display_errors();

    }

  }

}

Nah kita buka dihalaman browser maka akan tampil seperti gambar di bawah.

Cara Mengirim Attachment ke Email Menggunakan Codeigniter

Dan berikut ini adalah tampilan email yang masuk.

Cara Mengirim Attachment ke Email Menggunakan Codeigniter

Demikian artikel tentang Cara Mengirim Attachment ke Email Menggunakan Codeigniter. Semoga bermanfaat dan selamat mencoba.

18 April 2019

Webinar Gratis 2024


Selanjutnya Pada Bulan Maret 2024

Sabtu, 09 Maret 2024


10 Bahasa Rekomendasi Untuk Dipelajari di 2024

Python Developer, Data Science, Web Application

Kursus Python Django Web Application 2024 di DUMET School Kursus Postgre SQL Database Developer Terbaik 2024 di Jakarta
chat