PHPMailer adalah PHP library pihak ketiga yang menyediakan cara sederhana untuk mengirim email dalam PHP. Terdapat berbagai fitur fungsi mail() bawaan PHP, seperti dukungan untuk email HTML, lampiran, dan otentikasi SMTP.
Penerapan PHPMailer pada aplikasi web biasa digunakan untuk mengirim email aktivasi akun, notifikasi, newsletters, atau email transaksional.
Installation #
Masuk ke direktori aplikasi lalu jalankan perintah composer
composer require phpmailer/phpmailer
A Simple Example #
Buat file mailer.php
dan edit seperti berikut.
<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.example.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = '[email protected]'; //SMTP username
$mail->Password = 'secret'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
$mail->SMTPOptions = array (
'ssl' => array (
'verify_peer' => false // Tambahkan ini jika ingin tetap menggunakan SMTP Restrictions
)
);
//Recipients
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Joe User'); //Add a recipient
$mail->addAddress('[email protected]'); //Name is optional
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Sesuaikan konfigurasi SMTP dengan akun email yang Anda miliki lalu setting bagian Recipients
.
Test #
Test PHPMailer
php mailer.php
2023-06-13 14:11:49 SERVER -> CLIENT: 220-mailserver.host ESMTP Exim 4.96 #2 Tue, 13 Jun 2023 21:11:49 +0700
220-We do not authorize the use of this system to transport unsolicited,
220 and/or bulk e-mail.
2023-06-13 14:11:49 CLIENT -> SERVER: EHLO ng.topan.host
2023-06-13 14:11:49 SERVER -> CLIENT: 250-mailserver.host Hello subs [xx.xx.xx.xx]
250-SIZE 52428800
250-8BITMIME
250-PIPELINING
250-PIPECONNECT
250-AUTH PLAIN LOGIN
250 HELP
2023-06-13 14:11:49 CLIENT -> SERVER: AUTH LOGIN
2023-06-13 14:11:49 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2023-06-13 14:11:49 CLIENT -> SERVER: [credentials hidden]
2023-06-13 14:11:49 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2023-06-13 14:11:49 CLIENT -> SERVER: [credentials hidden]
2023-06-13 14:11:49 SERVER -> CLIENT: 235 Authentication succeeded
2023-06-13 14:11:49 CLIENT -> SERVER: MAIL FROM:<[email protected]>
2023-06-13 14:11:49 SERVER -> CLIENT: 250 OK
2023-06-13 14:11:49 CLIENT -> SERVER: RCPT TO:<[email protected]>
2023-06-13 14:11:49 SERVER -> CLIENT: 250 Accepted
...
2023-06-13 14:11:50 SERVER -> CLIENT: 250 OK id=1q94kR-006mvf-1d
2023-06-13 14:11:50 CLIENT -> SERVER: QUIT
2023-06-13 14:11:50 SERVER -> CLIENT: 221 mailserver.host closing connection
Jika Anda pengguna cPanel, pastikan SMTP Restrictions sudah disabled.
Additional options #
XMailer
$mail->XMailer = 'Company mail';
addCustomHeader
$mail->addCustomHeader('X-custom-header', 'custom-value');
Sender (Envelope Sender)
$mail->Sender = '[email protected]';
Language
$mail->setLanguage('id', './language/');
Gmail
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.gmail.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = '[email protected]'; //SMTP username
$mail->Password = 'password'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465;
Hotmail / Outlook
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.outlook.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = '[email protected]'; //SMTP username
$mail->Password = 'password'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable implicit TLS encryption
$mail->Port = 587;
Zoho
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.zoho.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = '[email protected]'; //SMTP username
$mail->Password = 'password'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465;