Cara menggunakan send email localhost php

Ketika kamu menguji aplikasi Web kamu di server localhost/online atau jika kamu menggunakan Sistem Operasi Windows sulit untuk mensetup mail server untuk mengirim email. Dengan library PHP yaitu PHPMailer kita dapat dengan mudah mengirim email dari localhost/server online menggunakan PHP.

Menggunakan PHPMailer untuk mengirim email dari localhost/server online

Library PHPMailer adalah library favorit pribadi saya untuk mengirim email dari localhost dan pada kenyataannya digunakan oleh diperkirakan 10 juta pengguna di seluruh dunia. Dengan menggunakan PHPMailer kita dapat mengirim email html, email teks, via smtp, gmail. Kamu juga dapat mengirim cc dan bcc ke beberapa penerima. Bahkan kita dapat men-download library PHPMailer dari Github dan Sourceforge. Silahkan lihat pada beberapa contoh di sini.

Mengkonfigurasi PHPMailer untuk mengirim email via SMTP

Saya mengasumsikan bahwa kamu sudah punya akun email Google (gmail), karena ini adalah cara termudah untuk mengirim email via smtp. Jika kamu sudah punya akun di outloook, atau layanan email lainnya itu bisa dilakukan juga. Ikuti langkah-langkah berikut untuk mengkonfigurasi library PHPMailer untuk mengirim email.

Langkah pertama adalah men-download file zip library PHPMailer dari situs PHPMailer/Github/Sourceforge. Setelah kamu men-downloadnya, extract ke folder project kamu direktori localhost / server online kamu.

Sertakan file

<?php
// membuat obyek dari class PHPMailer
$mail = new PHPMailer(true);
?>
2 dalam file php kamu. Ini akan menjadi halaman yang akan digunakan untuk mengirim email.

<?php
// menyertakan file PHPMailerAutoload
require 'phpmailer/PHPMailerAutoload.php';
?>

Menginstansiasi class phpmailer, yaitu membuat obyek dari class PHPMailer.

<?php
// membuat obyek dari class PHPMailer
$mail = new PHPMailer(true);
?>

Mengatur class untuk menggunakan metode SMTP.

<?php
$mail->IsSMTP();
?>

Jika kamu ingin melakukan debug kode atau mengecek output setelah mengirim email, gunakan opsi ini:

<?php
// mengaktifkan informasi debug SMPT (untuk pengujian)
// atur 0 untuk mode debugging
// atur 1 untuk menampilkan hasil debug
$mail->SMTPDebug = 0;
?>

Mengaktifkan otentikasi SMPT. Disarankan untuk menggunakannya.

<?php
$mail->SMTPAuth = true;
?>

Mengatur mode pengiriman email, server smtp dan port. Sekali lagi Saya mengasumsikan bahwa kamu sudah menggunakan Gmail.

<?php
// untuk Gmail
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;

// untuk Outlook
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp-mail.outlook.com';
$mail->Port = 587; //gunakan port 25 sebagai alternatif

?>

Berikan alamat email dan password. Juga mengatur alamat email pengirim dan namanya.

<?php

$mail->Username = '[email protected]';
// harus dalam tanda kutip tunggal
$mail->Password = 'passwordmu';

$mail->SetFrom('[email protected]', 'Nama Kamu');
?>

Jika kamu ingin menambahkan satu atau beberapa penerima, tambahkan baris kode ini di bawah ini.

<?php
$mail->AddAddress('[email protected]');
// hapus baris di bawah ini
// jika kamu tidak ingin mengirim ke beberapa penerima
$mail->AddAddress('[email protected]');
?>

Kamu juga dapat mengirim Cc/Bcc ke penerima.

<?php
$mail->AddCC('[email protected]');
$mail->AddBCC('[email protected]');
?>

Tambahkan isi pesan. Kamu dapat menggunakan teks biasa atau template html yang telah diformat. Saya menggunakan template email HTML.

<?php
// mengambil isi pesan dari file HTML yaitu pesan.html
$message = file_get_contents('pesan.html');

// menambahkan isi pesan
$mail->MsgHTML($message);
?>

Jika kamu ingin melampirkan file, tambahkan kode di bawah ini.

<?php
// membuat obyek dari class PHPMailer
$mail = new PHPMailer(true);
?>
0

Melakuakn cek pada konfigurasi dan mengirim email.

<?php
// membuat obyek dari class PHPMailer
$mail = new PHPMailer(true);
?>
1

Jika ada masalah tentang tutorial Mengirim email dari localhost/server online menggunakan PHP, jangan ragu untuk berkomentar gan.

Localhost is used as a development server to develop a web application. All the functionality of the web application is tested on the localhost server before moving it to the production server. But, the problem arises when email functionality needs to be tested on the localhost server. Generally, the email sending feature is not working with the PHP built-in functions in localhost.

If the web application is built with PHP, the mail() function is used to send email from the script using PHP. But the PHP mail() function will not work in localhost. In this tutorial, we’ll show how you can send email from localhost in PHP. Using this example script you can send email from any localhost server (XAMPP, WAMP, or any others) using PHP.

We will use the PHPMailer library to send emails from localhost using PHP. The PHPMailer library provides the easiest way to send an email from localhost with an SMTP server using PHP. Not only the text email, but you can also send HTML email from localhost in PHP using PHPMailer.

SMTP Server Credentials:
Before getting started create an email account on your server and collect the SMTP credentials (Host, Port, Username, Password, etc.) that will require to be specified in the code later.

Send Email from Localhost with PHP

The following code snippet will send HTML email from localhost using PHPMailer.

  • Include the PHPMailer library and create an instance of this class.
  • Set SMTP credentials (host, username, password, and port).
  • Specify sender name and email ($mail->setFrom).
  • Set recipient email address ($mail->addAddress).
  • Set email subject ($mail->Subject).
  • Set the body content of the email ($mail->Body).
  • Use the send() method of PHPMailer class to send an email.
// Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Include library files
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';

// Create an instance; Pass `true` to enable exceptions
$mail = new PHPMailer;

// Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER;    //Enable verbose debug output
$mail->isSMTP();                            // Set mailer to use SMTP
$mail->Host = 'smtp.example.com';           // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                     // Enable SMTP authentication
$mail->Username = '[email protected]';       // SMTP username
$mail->Password = 'email_password';         // SMTP password
$mail->SMTPSecure = 'ssl';                  // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;                          // TCP port to connect to

// Sender info
$mail->setFrom('[email protected]', 'SenderName');
$mail->addReplyTo('[email protected]', 'SenderName');

// Add a recipient
$mail->addAddress('[email protected]');

//$mail->addCC('[email protected]');
//$mail->addBCC('[email protected]');

// Set email format to HTML
$mail->isHTML(true);

// Mail subject
$mail->Subject = 'Email from Localhost by CodexWorld';

// Mail body content
$bodyContent = '

How to Send Email from Localhost using PHP by CodexWorld

';
$bodyContent .= '

This HTML email is sent from the localhost server using PHP by CodexWorld

';
$mail->Body    = $bodyContent;

// Send email
if(!$mail->send()) {
    echo 'Message could not be sent. Mailer Error: '.$mail->ErrorInfo;
} else {
    echo 'Message has been sent.';
}

Note that: If you want to use Gmail as an SMTP server, set your Google email address as SMTP username and password as SMTP password.

Send Email via SMTP Server in PHP using PHPMailer

You can send emails with multiple attachments from localhost with PHPMailer.

  • Set file path to addAttachment() method.
// Add attachments
$mail->addAttachment('/var/tmp/file.tar.gz');
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name

Are you want to get implementation help, or modify or enhance the functionality of this script? Click Here to Submit Service Request