CodeIgniter Tutorial - Kirim Email dengan menggunakan SMTP Gmail

 


Sebelum mengikuti tutorial ini, silahkan aktifkan Akses aplikasi yang kurang aman, di akun gmail anda masing-masing.

Aktifkan Akes aplikasi yang kurang aman

Jika sudah, let's start to code!

KirimEmail.php (CONTROLLER)
<?php
defined('BASEPATH'or exit('No direct script access allowed');

class KirimEmail extends CI_Controller
{
    public function index()
    {
        $data["title"= "Kirim Email dengan SMTP Gmail";
        $this->load->view('kirimEmail'$data);
    }

    public function kirim()
    {
        $kepada = $this->input->post("kepada");
        $subjek = $this->input->post("subjek");
        $pesan  = $this->input->post("pesan");

        //SMTP & mail configuration
        $config = array(
            'protocol'      => 'smtp',
            'smtp_host'     => 'ssl://smtp.googlemail.com',
            'smtp_port'     => 465,
            'smtp_timeout'  => 5,
            'smtp_user'     => 'akun_gmail_anda@gmail.com',
            'smtp_pass'     => 'password_akun_gmail_anda',
            'mailtype'      => 'html',
            'charset'       => 'iso-8859-1'
        );

        $this->load->library('email'$config);
        $this->email->set_newline("\r\n");
        $this->email->from('pt.ugs1988@gmail.com''PASIR GANTING TEKNOLOGI');
        $this->email->to($kepada);
        $this->email->subject($subjek);
        $this->email->message($pesan);

        if ($this->email->send()) {
            $this->session->set_flashdata('pesan''Email terkirim!');
        } else {
            $this->session->set_flashdata('pesan''Gagal!');
        }
        redirect('KirimEmail');
    }
}


kirimEmail.php (VIEW)
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- CDN Bootstrap -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <title><?= $title?></title>
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-md-8" style="margin: 0 auto; float:none">
                <h3 class="text-center"><?= $title ?></h3>
                <?php
                if ($this->session->flashdata('pesan')) {
                    echo "<div class='alert alert-success'>
            " . $this->session->flashdata('pesan') . "
            </div>";
                } else {
                    echo "<div class='alert alert-danger'>
            " . $this->session->flashdata('error') . "
            </div>";
                }
                ?>
                <form action="<?= base_url('kirimEmail/kirim') ?>" method="POST">
                    <div class="form-group">
                        <label for="">Kepada</label>
                        <input type="email" name="kepada" placeholder="Email Tujuan" class="form-control" required>
                        <label for="">Subjek</label>
                        <input type="text" name="subjek" placeholder="Subjek Email" class="form-control" required>
                        <label for="">Pesan</label>
                        <textarea name="pesan" rows="10" cols="30" placeholder="Tulis Pesan anda disini..." class="form-control"></textarea>
                        <div class="mt-2" style="float: right;">
                            <button type="submit" class="btn btn-sm btn-primary">Kirim</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>

</html>

DEMO PROGRAM

Jika kirim emailnya gagal, coba matikan antivirusnya.
Semoga bermanfaat!!!

Posting Komentar

0 Komentar