PHP Mailer menggunakan SMTP GMAIL

<?php
include('phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
//$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "localhost"; // SMTP server
//IsSMTP(); // send via SMTP
$mail->Host     = "ssl://smtp.gmail.com"; // SMTP server Gmail
$mail->Mailer   = "smtp";
$mail->SMTPAuth = true; // turn on SMTP authentication

$mail->Username = "username@gmail.com"; // 
$mail->Password = "password"; // SMTP password
$webmaster_email = "username@domain.com"; //Reply to this email ID
$email = "emailtujuan@domain.com"; // Recipients email ID
$name = "namapenerima"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "namapengirim";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"namawebmaster");
$mail->WordWrap = 50; // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Ini adalah Email HTML";
$mail->Body = "Ini adalah email contoh"; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body 
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
  • Edit kode di atas sesuai data sobat
  • Simpan di document root dengan nama mail.php
  • Jadi sekarang di dalam document root kita sudah ada file mail.php dan folder phpmailer

  • Eits,, jangan langsung dieksekusi ya! (hehehe), masih ada beberapa konfigurasi lagi yang harus dirubah.
  • Buka file class.smtp.php yang berada di dalam direktori phpmailer
  • Cari baris berikut //connect to the smtp server
  • Lalu tambahkan baris-baris kode berikut tepat di bawahnya
     $host = "ssl://smtp.gmail.com"; 
     $port = 465; 
  • Simpan hasil konfigurasi.

Langkah terakhir saatnya pengujian. Buka browser sobat lalu ketikkan alamat URL server lokasi file mail.php yang sudah kita buat. misalnya (localhost/platinum/mail.php). Jika berhasil maka akan tampil pesan "Message has ben sent" pada layar.

Share on Google Plus

About Elmirakom

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment