Sulhi.id

Catatan Kecil Seputar Blogging dan Koding

Menggunakan SMTP Gmail di CodeIgniter sebagai SMTP Relay

Saturday, March 4th, 2017     CodeIgniter

SMTP Relay yang penulis gunakan sebelumnya adalah TurboSMTP dengan akun email apps menggunakan Zoho Mail sebagai pengganti Google Apps yang sudah tidak gratis lagi. Entah penyebabnya apa, ketika menggunakan TurboSMTP  email yang masuk selalau ke SPAM. Singkat kata penulis akhirnya memutuskan menggunakan SMTP Gmail dengan akun gmail.

MEMBUAT KONFIGURASI LIBRARY EMAIL CODEIGNITER

Silakan buat file email.php yang diletakan pada folder application/config/email.php dan isi kode seperti di bawah ini:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------
| EMAIL CONFING
| -------------------------------------------------------------------
| Configuration of outgoing mail server.
| */
 
$config['wordwrap'] = FALSE;
$config['mailtype'] = 'html';
$config['crlf'] = '\r\n';
$config['charset']='utf-8';
$config['newline']="\r\n";
$config['priority']=1;
$config['protocol']='smtp';
$config['smtp_host']='ssl://smtp.gmail.com';
$config['smtp_port']='465';
$config['smtp_timeout']='30';
$config['smtp_user']='xxxxx@gmail.com';
$config['smtp_pass']='*********';
$config['newline']="\r\n";
 
/* End of file email.php */
/* Location: ./system/application/config/email.php */

MEMBUAT CONTROLLER UNTUK FORM CONTACT DAN FUNGSI SENDMAIL

Silakan buat file contoller dengan nama Contact.php, dan isi dengan kode seperti di bawah ini:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Contact extends CI_Controller {
 
 public function __construct() {
 parent::__construct();
 $this->load->model('posts_model');
 $this->load->library('form_validation');
 }
  
 public function index() {
 $this->load->view('contact_view'); 
 }
 
 public function sendmail()
 {
 $this->form_validation->set_rules('email', 'email', 'required');
 $this->form_validation->set_rules('nama', 'nama', 'required');
 $this->form_validation->set_rules('subjek', 'subjek', 'required');
 $this->form_validation->set_rules('pesan', 'pesan', 'required');
 if ($this->form_validation->run() === TRUE) 
 {
 $email = $this->input->post('email');
 $nama = $this->input->post('nama');
 $subjek = $this->input->post('subjek');
 $pesan = $this->input->post('pesan');
  
 $this->load->library('email'); // load email library
 $this->email->from($email, $nama);
 $this->email->to('admin@infocybernet.com');
 $this->email->subject($subjek);
 $this->email->message($pesan);
 $this->email->send();
 $this->session->set_flashdata('pesan', 'Pesan berhasil dikirim, Pesan Anda akan segera kami respon secepatnya! Terima kasih atas partisipasinya');
 redirect('contact'); 
 }
 else
 { 
 //show_error($this->email->print_debugger()); 
 $this->session->set_flashdata('pesan', 'Pesan Gagal dikirim! Semua form harus diisi');
 redirect('contact'); 
 }
 }
}

BUAT VIEW FORM CONTACT

Buat file view dengan nama file contact_view.php yang berfungsi sebagai form contact. Isikan dengan kode berikut:

<form class="form-horizontal" method="POST" action="<?php echo site_url(); ?>/contact/sendmail">
 <div class="form-group">
 <label for="nama" class="col-sm-2 control-label">Nama</label>
 <div class="col-sm-10">
 <input type="text" class="form-control" value="" name="nama" placeholder="Nama Anda" required>
 </div>
 </div>
  
 <div class="form-group">
 <label for="email" class="col-sm-2 control-label">Email</label>
 <div class="col-sm-10">
 <input type="email" class="form-control" value="" name="email" placeholder="Email Anda" required>
 </div>
 </div>
  
 <div class="form-group">
 <label for="subjek" class="col-sm-2 control-label">Subjek</label>
 <div class="col-sm-10">
 <input type="text" class="form-control" value="" name="subjek" placeholder="Subjek" required>
 </div>
 </div> 
 
 <div class="form-group">
 <label for="pesan" class="col-sm-2 control-label">Pesan</label>
 <div class="col-sm-10">
 <textarea type="text" class="form-control" value="" name="pesan" placeholder="Pesan/saran" required></textarea>
 </div>
 </div> 
  
 <div class="form-group">
 <div class="col-sm-offset-2 col-sm-10">
 <button type="submit" class="btn btn-primary">Kirim</button>
 </div>
 </div>
 </form>

Let’s tray send a mail. Happy blogging!


http://103.147.222.22/ https://sisbpn.petrolab.co.id/ https://survey.petrolab.co.id/pulsa/ http://jdih-aceh-dev.kemenkumham.go.id/ http://dev-realisasi.stipjakarta.ac.id/ https://efinger.bkpp.gorontalokota.go.id/ https://lppm.nurulfikri.ac.id/ https://sierik.bkpp.gorontalokota.go.id/ http://kebunraya.balikpapan.go.id/ https://dev-sido.sebi.ac.id/ https://wginc.com/ https://jdih.majalengkakab.go.id/ slotpulsa