- Download PHPMailer dari github.com/PHPMailer
- Upload ke hosting anda dan letakan di dalam public_html
- buat email account untuk dijadikan smtp mailer contoh : noreply@dopmainanda.tld
- masuk kedalam folder PHPMailer, buat 1 file bernama sendemail.php
- Isi kode sendmail.php dengan kode berikut:
<?php
include 'PHPMailerAutoLoad.php';
$mail = new PHPMailer();
$mail->IsSMTP();
// setting phpmailer untuk menggunakan protokol smtp
$mail->Host = "domainanda.tld";
// setting host smtp, bisa ip server, domain, hostname server
$mail->SMTPAuth = true;
// aktifkan autentikasi smtp
$mail->Username = "noreply@domainanda.tld";
// SMTP username, didapat dari email yang dibuat di cpanel melalui email account
$mail->Password = "passsword of noreply@dopmainanda.tld";
$mail->From = "notifikasi@domainanda.tld";
// Email pengirim
$mail->FromName = "Nama Anda";
// Nama Pengirim
$mail->AddAddress("josh@example.net", "Josh Adams");
// Tujuan Kirim email dengan nama
$mail->AddAddress("ellen@example.com");
// Tujuan kirim email tanpa nama
$mail->AddReplyTo("info@domainanda.tld", "Information");
// Email tujuan balasan apabila penerima email ingin membalas
$mail->AddCC('{CC Email Address}');
// CC email
$mail->AddBCC('{BCC Email Address}');
// BCC Email
$mail->AddAttachment('alamat lampiran file/path file', 'Nama Lampiran');
// Email mengandung lampiran file, nama lampiran opsional bisa dihapus
$mail->IsHTML(true);
// Bila isi email beformat HTML, gunakan opsi ini
$mail->Subject = 'Subjek Email';
// Subjek Email yang dikirim
$mail->Body = 'Isi email dalam bentuk HTML';
// Isi email
$mail->AltBody = 'Isi email tambahan dalam bentuk teks biasa';
// Tambahan isi emailif(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}echo "Message has been sent";
?> - Body message dapat diganti dengan isian dari form kontak email.
- AddReplyTo dapat diganti dengan email si pengirim dari form kontak email di web
- Subject dapat diganti dengan subjek pesan dari from kontak email.
- Untuk integrasi dengan form kontak sederhana maka kodenya akan seperti ini (dalam hal ini kami mengaktifkan debug untuk mendapatkan detail pesan sukses)
<form method="POST" action="sendmail.php">
<input type="text" name="namapengirim" placeholder="Name"/></br>
<input type="email" name="emailpengirim" placeholder="Email"/></br>
<input type="text" name="subjekemail" placeholder="subject"/></br>
<textarea name="pesan" col="10" row="5"></textarea></br>
<input type="submit" value="Kirim email"/>
</form>
<?phpinclude 'PHPMailerAutoload.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$mail = new PHPMailer();$mail->IsSMTP();
$mail->Host = "zeus.rcs-server.net";
$mail->SMTPAuth = true;
$mail->Username = "username@zeus.rcs-server.net";
$mail->Password = "password";
$mail->From = $_POST['emailpengirim'];
$mail->FromName = "Kontak dari ".$_POST['namapengirim'];
$mail->AddAddress("yourgmail@gmail.com");
$mail->AddReplyTo($_POST['emailpengirim'],$_POST['namapengirim']);
$mail->Subject = $_POST['subjekemail'];
$mail->Body = $_POST['pesan'];
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}echo "Message has been sent";
}
?> - Apabila sukses output debug adalah
2017-05-25 13:57:22 SERVER -> CLIENT: 220-zeus.hideserver.net ESMTP Exim 4.89 #1 Thu, 25 May 2017 20:57:22 +0700 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
2017-05-25 13:57:22 CLIENT -> SERVER: EHLO zeus.rcs-server.net
2017-05-25 13:57:22 SERVER -> CLIENT: 250-zeus.hideserver.net Hello zeus.rcs-server.net [103.28.22.89]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250-STARTTLS250 HELP
2017-05-25 13:57:22 CLIENT -> SERVER: STARTTLS
2017-05-25 13:57:22 SERVER -> CLIENT: 220 TLS go ahead
2017-05-25 13:57:22 CLIENT -> SERVER: EHLO zeus.rcs-server.net
2017-05-25 13:57:22 SERVER -> CLIENT: 250-zeus.hideserver.net Hello zeus.rcs-server.net [103.28.22.89]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250 HELP
2017-05-25 13:57:22 CLIENT -> SERVER: AUTH LOGIN
2017-05-25 13:57:22 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2017-05-25 13:57:22 CLIENT -> SERVER: c210cEB6ZXVzLnJjcy1zZXJ2ZXIubmV0
2017-05-25 13:57:22 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2017-05-25 13:57:22 CLIENT -> SERVER: ejN1QGt1IQ==
2017-05-25 13:57:22 SERVER -> CLIENT: 235 Authentication succeeded
2017-05-25 13:57:22 CLIENT -> SERVER: MAIL FROM:<fromkontak@gmail.com>
2017-05-25 13:57:22 SERVER -> CLIENT: 250 OK
2017-05-25 13:57:22 CLIENT -> SERVER: RCPT TO:<ownerweb@gmail.com>
2017-05-25 13:57:22 SERVER -> CLIENT: 250 Accepted
2017-05-25 13:57:22 CLIENT -> SERVER: DATA
2017-05-25 13:57:22 SERVER -> CLIENT: 354 Enter message, ending with "." on a line by itself
2017-05-25 13:57:22 CLIENT -> SERVER: Date: Thu, 25 May 2017 20:57:22 +0700
2017-05-25 13:57:22 CLIENT -> SERVER: To: ownerweb@gmail.com
2017-05-25 13:57:22 CLIENT -> SERVER: From: Kontak dari Hafizh <fromkontak@gmail.com>
2017-05-25 13:57:22 CLIENT -> SERVER: Reply-To: Hafizh <fromkontak@gmail.com>
2017-05-25 13:57:22 CLIENT -> SERVER: Subject: PHP Mailer Zeus
2017-05-25 13:57:22 CLIENT -> SERVER: Message-ID: <89a7829d2ef1684ac5d659817ae87ee5@zeus.rcs-server.net>
2017-05-25 13:57:22 CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.23 (https://github.com/PHPMailer/PHPMailer)
2017-05-25 13:57:22 CLIENT -> SERVER: MIME-Version: 1.0
2017-05-25 13:57:22 CLIENT -> SERVER: Content-Type: text/plain; charset=iso-8859-1
2017-05-25 13:57:22 CLIENT -> SERVER:
2017-05-25 13:57:22 CLIENT -> SERVER: hallo ini kode php mailer
2017-05-25 13:57:22 CLIENT -> SERVER:
2017-05-25 13:57:22 CLIENT -> SERVER: .
2017-05-25 13:57:23 SERVER -> CLIENT: 250 OK id=1dDtGU-001OdQ-V5
2017-05-25 13:57:23 CLIENT -> SERVER: QUIT
2017-05-25 13:57:23 SERVER -> CLIENT: 221 zeus.hideserver.net closing connection
Message has been sent - Untuk menghindari email masuk spam karena from dan host smtp berbeda, kami sarankan pada bagian ini
$mail->From = $_POST['emailpengirim'];
$mail->FromName = "Kontak dari ".$_POST['namapengirim'];
diset sama dengan smtp username dan nama anda sendiri
Berikut adalah cara berkirim email dengan memakai librari PHPMailer.
- PHPMailer, PHP, PHP, SMTP
- 958 Kasutajad peavad seda kasulikuks
Seotud artiklid
Powered by WHMCompleteSolution