- How to Configure XAMPP to Send Email from Localhost with PHP
- Steps to Send an Email with the help of XAMPP
- Step 1: XAMPP Installation Directory
- Step 2: Go and Open php.ini
- Step 3: Find [mail function]
- Step 4: Pass Values
- Step 5: Open sendmail.ini
- Step 6: Find [sendmail]
- Step 7: Search and Pass Values
- Script for Sending Email
- What is XAMPP?
- How to Send Email from Localhost in PHP
- Send Email from Localhost with PHP
- How to Send Email from Localhost using PHP by CodexWorld
- How To Send Email From Localhost Using PHP
- Video Tutorial:
- Send an Email from localhost using PHP
- What you Need for setup
- Step# 1:
- Step# 2:
- Step# 3:
- Important Note:
How to Configure XAMPP to Send Email from Localhost with PHP
It is often necessary to configure the XAMPP server for sending email from the localhost using PHP. To meet that goal, you need to make changes to two files such as sendmail.ini and php.ini. Here, we will represent to you what steps you need to take to achieve that.
Steps to Send an Email with the help of XAMPP
Below, you can find the steps to configure XAMPP for sending emails from the local host with PHP.
Step 1: XAMPP Installation Directory
The first step is opening the XAMPP Installation Directory.
Step 2: Go and Open php.ini
The second step is going to C:\xampp\php and opening the php.ini file.
Step 3: Find [mail function]
In the framework of the third step, you should press ctrl + f for finding the [mail function] .
Step 4: Pass Values
Now, it is necessary to detect and pass the values, demonstrated below:
SMTP=smtp.gmail.com smtp_port=587 sendmail_from = [email protected] sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
Step 5: Open sendmail.ini
The next step is going to C:\xampp\sendmail and opening the sendmail.ini file.
Step 6: Find [sendmail]
Press ctrl + f for finding [sendmail] .
Step 7: Search and Pass Values
The seventh step considers searching and passing the values, demonstrated below:
mtp_server=smtp.gmail.com smtp_port=587 error_logfile=error.log debug_logfile=debug.log [email protected] auth_password=Your-Gmail-Password [email protected].com(optional)
Script for Sending Email
Finally, let’s see how the script for sending emails looks like:
$to_email = "[email protected]"; $subject = "Simple Email Testing via PHP"; $body = "Hello,nn It is a testing email sent by PHP Script"; $headers = "From: sender\'s email"; if (mail($to_email, $subject, $body, $headers)) < echo "Email successfully sent to $to_email. "; > else < echo "Email sending failed. "; > ?>
After following the steps above, you will manage to send an email. However, if you still face problems in sending an email, then the reason can be among the following:
- A broken port.
- A wrong password.
- The emails are sent late.
- Gmail doesn’t obtain the necessary permissions.
- Wrong email configuration.
What is XAMPP?
XAMPP is considered a free and open-source cross-platform web server solution stack package. It is designed and developed by Apache Friends. It consists of the MariaDB database, Apache HTTP Server, as well as interpreters for scripts written in the PHP and Perl programming languages.
How to Send Email from Localhost in PHP
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 = 'user@example.com'; // 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('sender@example.com', 'SenderName');
$mail->addReplyTo('reply@example.com', 'SenderName');
// Add a recipient
$mail->addAddress('recipient@example.com');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
// 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.
You can send emails with multiple attachments from localhost with PHPMailer.
// 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
If you have any questions about this script, submit it to our QA community — Ask Question
How To Send Email From Localhost Using PHP
In this post about “How to send mail from localhost in php using wamp/xampp”, sometimes we need to test the mail sending function from our development environment. We can send mail from our localhost using some mail configuration by XAMPP/LAMP/WAMP server.
First, we need to enable php_openssl php extensions from php.ini file. I am using GMAIL SMTP server to send mail from localhost and sendmail package.
It is a mail transport agent which can be found in php.ini file. The sendmail package is inbuilt in XAMPP. So if you are using XAMPP then you can easily send mail from localhost.
Video Tutorial:
If you are more comfortable in watching a video that explains about How To Send Mail From Localhost Using XAMPP, then you should watch this video tutorial.
Step 1: we need to enable php_openssl php extensions from php.ini file. For XAMPP, it is located in C:\XAMPP\php\php.ini . You can download from php_openssl.dll.
Step 2: Find the mail function in php.ini file. You can find the code like below.
[mail function] ; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury ; SMTP = localhost ; smtp_port = 25 ; For Win32 only. ; http://php.net/sendmail-from ;sendmail_from = postmaster@localhost ;sendmail_path = "\"D:\xampp\sendmail\sendmail.exe\" -t"
replace with your SMTP configuration parameters like the below,
[mail function] ; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury SMTP = smtp.gmail.com smtp_port = 25 ; For Win32 only. ; http://php.net/sendmail-from sendmail_from = phpflow@gmail.com sendmail_path = "\"D:\xampp\sendmail\sendmail.exe\" -t"
Step 3: Open sendmail.ini which is located on «c:\xampp\sendmail\sendmail.ini» and change SMTP configuration parameters.
[mail function] smtp_server=smtp.gmail.com smtp_port=25 auth_username=phpflow@gmail.com auth_password=XXXXXXX force_sender=phpflow@gmail.com
Step 4: Restart your Apache server.
Step 5: Send mail using php mail() function.
mail("parvez1487@gmail.com","Success","Send mail from localhost using PHP");
Sometimes port 25 is not open, so you need to change to 587 OR 465.
Now you have configured mail from localhost and use it.I hope this php tutorial helps to understand the working functionality of mail and sending mail from localhost using PHP SMTP.
Send an Email from localhost using PHP
In this tutorial, you will learn how to send an email from your localhost using PHP. Many developers have this problem whenever they want to test the code where they are sending an email.
You can test your code online on your server where already all the things are set up or you can test your code on localhost (on your local system or machine).
Usually, developers use some servers to run server-side language on a local machine. I am using Wamp, Xampp server on my local machine. if you are using this type of server then you need to set up a few things or follow the few steps which I mentioned below.
What you Need for setup
- You must have a server like I have Xampp on my local PC.
- I am using Gmail SMTP for setting up email from the server, so you must have any SMTP server or Gmail Account.
- You must know the username (email) and password.
- You can edit the .ini files in text editor.
- smtp_sever : The SMTP Host server name like , smtp.gmail.com
- smtp_port : The port number (Ex port: 465)
- auth_username : Your SMTP username
- auth_password : Your SMTP password
Step# 1:
In the first step, you need to open the php.ini file where you can set some SMTP detail that you will use. This file you will find in your server it could be Wamp, Xampp, Lamp any just find the file and open it. In my case, I have a XAMPP server and I installed this server on my S Derive (I am using windows OS).
In PHP we can use the mail() function to send an email but from localhost, you can’t send an email with this configuration.
- sendmail_from (Add your gamil email ID)
- sendmail_path (Give the path of your sendemail exe with -t flag)
In my case, the path of sendemail.exe is given below you must change the path with your directory structure.
[mail function] ; For Win32 only. ; http://php.net/smtp SMTP=localhost ; http://php.net/smtp-port smtp_port=25 ; For Win32 only. ; http://php.net/sendmail-from sendmail_from = learncodeweb@gmail.com ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ; http://php.net/sendmail-path sendmail_path = "\"D:\server1\sendmail.exe\" -t"
Step# 2:
In the second step, we have to update the sendemail.exe file with GMAIL SMTP settings. Click here to see the official SMTP details of GMAIL. Open this file and change the below parameters.
- smtp_server
- smtp_port
- auth_username (I mentioned learncodewed@gmail.com this email not exist)
- auth_password
- force_sender (Optional)
- force_recipient (Optional)
smtp_server=smtp.gmail.com smtp_port=25 auth_username=learncodeweb@gmail.com auth_password=************
Step# 3:
After these changes restart the server and try the below code to send an email. Other posts related to send email are listed below.
// To send HTML mail, the Content-type header must be set $headers[] = 'MIME-Version: 1.0'; $headers[] = 'Content-type: text/html; charset=iso-8859-1'; // Additional headers $headers[] = 'To: LearnCodeWeb '; $headers[] = 'From: LearnCodeWeb '; $headers[] = 'Cc: test@learncodeweb.com'; $headers[] = 'Bcc: test@learncodeweb.com'; mail('yourtestemail@email.com', 'I am testing Learncodeweb code', 'Thank you LCW to save my life', implode("\r\n", $headers));
Important Note:
If you are not able to get an email after testing the code you must set up a few things on your Gmail Account. You must enable the Less Secure App ON to click here to check the settings. And if you have already enabled the two-step verification on your account then disable the two-step verification after that you will be able to ON/OFF the Less Secure App settings.