azmicolejr

Kirim Email dari Localhost dengan PHP dan PHPMailer

Jun 28th, 2019
2,594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.28 KB | None | 0 0
  1. <?php
  2. // Import PHPMailer classes into the global namespace
  3. // These must be at the top of your script, not inside a function
  4. use PHPMailer\PHPMailer\PHPMailer;
  5. use PHPMailer\PHPMailer\SMTP;
  6. use PHPMailer\PHPMailer\Exception;
  7.  
  8. // Load Composer's autoloader
  9. require 'vendor/autoload.php';
  10.  
  11. if(isset($_POST['submit']))
  12. {
  13.     // Instantiation and passing `true` enables exceptions
  14.     $mail = new PHPMailer(true);
  15.  
  16.     $no_invoice         = $_POST['no_invoice'];
  17.     $nama_pengirim      = $_POST['nama_pengirim'];
  18.     $email              = $_POST['email'];
  19.  
  20.     //Server settings
  21.     // $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
  22.     $mail->isSMTP();                                            // Send using SMTP
  23.     $mail->Host       = 'smtp.gmail.com';                    // Set the SMTP server to send through
  24.     $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
  25.     $mail->Username   = 'namaEmailAnda';                     // SMTP username
  26.     $mail->Password   = 'passwordEmailAnda';                               // SMTP password
  27.     // $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
  28.     $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
  29.  
  30.     //Recipients
  31.     $mail->setFrom('namaemail', 'Percobaan');
  32.     $mail->addAddress($email, $nama_pengirim);     // Add a recipient
  33.    
  34.     $mail->addReplyTo('namaemail', 'Percobaan');
  35.     // $mail->addCC('[email protected]');
  36.     // $mail->addBCC('[email protected]');
  37.  
  38.     // Attachments
  39.     // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
  40.     // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
  41.  
  42.     // Content
  43.     $mail->isHTML(true);                                  // Set email format to HTML
  44.     $mail->Subject = 'Konfirmasi Pembayaran dari Localhost';
  45.     $mail->Body    = '<h1>Halo, Admin.</h1> <p>Ada pesanan baru dengan nomor invoice '.$no_invoice.'</p> ';    
  46.  
  47.     if($mail->send())
  48.     {
  49.         echo 'Konfirmasi pembayaran telah berhasil';
  50.     }
  51.     else{
  52.         echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
  53.     }
  54. }
  55. else{
  56.     echo "Tekan dulu tombolnya bos";
  57. }
Add Comment
Please, Sign In to add comment