azmicolejr

Kirim Email dari Localhost dengan CI3 dan PHPMailer

Jun 30th, 2019
2,222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.51 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3.  
  4. // Import PHPMailer classes into the global namespace
  5. // These must be at the top of your script, not inside a function
  6. use PHPMailer\PHPMailer\PHPMailer;
  7. use PHPMailer\PHPMailer\SMTP;
  8. use PHPMailer\PHPMailer\Exception;
  9.  
  10. // Load Composer's autoloader
  11. require 'vendor/autoload.php';
  12.  
  13. class Welcome extends CI_Controller
  14. {
  15.  
  16.     /**
  17.      * Index Page for this controller.
  18.      *
  19.      * Maps to the following URL
  20.      *      http://example.com/index.php/welcome
  21.      *  - or -
  22.      *      http://example.com/index.php/welcome/index
  23.      *  - or -
  24.      * Since this controller is set as the default controller in
  25.      * config/routes.php, it's displayed at http://example.com/
  26.      *
  27.      * So any other public methods not prefixed with an underscore will
  28.      * map to /index.php/welcome/<method_name>
  29.      * @see https://codeigniter.com/user_guide/general/urls.html
  30.      */
  31.     public function index()
  32.     {
  33.         $this->load->view('welcome_message');
  34.     }
  35.  
  36.     public function kirim()
  37.     {
  38.         $this->load->view('kirim');
  39.     }
  40.  
  41.     public function kirim_proses()
  42.     {
  43.         if (isset($_POST['submit'])) {
  44.             // Instantiation and passing `true` enables exceptions
  45.             $mail = new PHPMailer(true);
  46.  
  47.             $no_invoice         = $this->input->post('no_invoice');
  48.             $nama_pengirim      = $this->input->post('nama_pengirim');
  49.             $email              = $this->input->post('email');
  50.  
  51.             //Server settings
  52.             // $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
  53.             $mail->isSMTP();                                            // Send using SMTP
  54.             $mail->Host       = 'smtp.gmail.com';                    // Set the SMTP server to send through
  55.             $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
  56.             $mail->Username   = 'namaEmailAnda';                     // SMTP username
  57.             $mail->Password   = 'passwordEmailAnda';                               // SMTP password
  58.             // $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
  59.             $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
  60.  
  61.             //Recipients
  62.             $mail->setFrom('asalemailPengirim');
  63.             $mail->addAddress($email, $nama_pengirim);     // Add a recipient
  64.  
  65.             $mail->addReplyTo('asalemailPengirim');
  66.             // $mail->addCC('[email protected]');
  67.             // $mail->addBCC('[email protected]');
  68.  
  69.             // Attachments
  70.             // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
  71.             // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
  72.  
  73.             // Content
  74.             $mail->isHTML(true);                                  // Set email format to HTML
  75.             $mail->Subject = 'Konfirmasi Pembayaran dari Localhost dengan Codeigniter';
  76.             $mail->Body    = '<h1>Halo, Admin.</h1> <p>Ada pesanan baru dengan nomor invoice ' . $no_invoice . '</p> ';
  77.  
  78.             if ($mail->send()) {
  79.                 echo 'Konfirmasi pembayaran telah berhasil';
  80.             } else {
  81.                 echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
  82.             }
  83.         } else {
  84.             echo "Tekan dulu tombolnya bos";
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment