Partner with experts to accelerate your digital transformation journey
Bobcares

How to Send Emails from Localhost in PHP Using WAMP

PDF Header PDF Footer

Set up Sendmail for WAMP and start sending emails from localhost with ease. Our Sendmail Support team is ready to assist you. 

Sendmail for WAMP

In this guide, we’ll show you how to send mail from localhost in PHP using WAMP, walking you through the process of setting up Sendmail, configuring your Gmail account, and creating a PHP script to send emails. By the end, you’ll have a fully functional local email system to test your PHP mail features. Let’s get started!

If you’re working on a local PHP project and need to send emails, you might wonder how to send mail from localhost in PHP using WAMP. While WAMP doesn’t come with a built-in mail server, it’s still possible to set up email functionality using a third-party tool. By configuring a simple setup, you can test email sending in your local environment before deploying your application.

Requirements to Send Mail from Localhost

Before you get started, make sure you have the following:

  • WAMP installed on your computer: This guide assumes you are already running WAMP.
  • A Gmail account: You’ll use Gmail’s SMTP server to send emails from localhost.

Setup Sendmail Folder in WAMP

First, you need to create a folder that will hold Sendmail. Here’s how you do it:

  1. Create a New Folder:- Go to the WAMP installation directory, which is usually located at C:\wamp\ or C:\wamp64\, and create a folder named sendmail.
  2. Download Sendmail:- Next, download the Sendmail program from the official website or a trusted source. Extract the contents into the sendmail folder you just created.

Configure Sendmail to Work with Gmail

After setting up the Sendmail folder, you need to configure Sendmail to use Gmail’s SMTP server for sending emails.

  1. Modify the Sendmail Configuration File:-
    Inside the sendmail folder, look for the sendmail.ini file and open it in a text editor. Update the settings as follows:
smtp_server=smtp.gmail.com
smtp_port=465
smtp_ssl=ssl
default_domain=localhost
error_logfile=error.log
debug_logfile=debug.log
auth_username=[your-gmail-username]@gmail.com
auth_password=[your-gmail-password]
pop3_server=
pop3_username=
pop3_password=
force_sender=
force_recipient=
hostname=localhost
Important: Replace [your-gmail-username] and [your-gmail-password] with your actual Gmail account details.

  1. Enable IMAP Access in Gmail:- 
  • Sign in to your Gmail account.
  • Navigate to Settings Forwarding and POP/IMAP.
  • Enable IMAP Access under the IMAP section to allow Sendmail to communicate with Gmail.
  1. Enable PHP Extensions:-
    You also need to enable some PHP extensions to allow Sendmail to work:

  • Open the php.ini file (found in your WAMP folder).
  • Search for and uncomment (remove the semicolon) the lines for php_openssl and php_sockets.
  1. Restart WAMP:- After making these changes, restart the WAMP server to ensure the changes take effect.

Update PHP Configuration to Use Sendmail

Next, you need to tell PHP where to find the Sendmail program.

  1. Update sendmail_path in PHP:- In the php.ini file, locate the sendmail_path directive and update it to this:
    sendmail_path = "C:\wamp\sendmail\sendmail.exe -t -i"
  2. Restart the Server:- Once you’ve made these changes, restart WAMP again to ensure the configuration is loaded correctly.

Create a PHP Script to Send an Email

You’re now ready to create a PHP script that will send an email from localhost.

  1. Write the Email Script:- Create a new PHP file, for example, send_email.php, in your WAMP’s www directory (or inside a subfolder of www). Add the following code to it:
<?php
$to = 'recipient@example.com'; // Change this to the recipient's email address
$subject = 'Test Email from Localhost';
$message = 'Hello, this is a test email sent from localhost using PHP!';
$headers = 'From: your-email@gmail.com' . "\r\n" .
MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=UTF-8';
if(mail($to, $subject, $message, $headers)) {
echo "Email sent successfully!";
else {
echo "Email sending failed.";
}
?>
  1. Save and Test:- Save this file as send_email.php and open it in your browser by going to http://localhost/send_email.php. If everything is configured correctly, you should see a message saying “Email sent successfully!”

Troubleshooting Tips

If the email doesn’t send or you encounter errors, here are a few things to check:

  • Check the error logs: Look at the error.log and debug.log files in the Sendmail folder for any clues.
  • Gmail Security Settings: If Gmail blocks the sign-in attempt, you might need to enable access for less secure apps in your Gmail account settings.
  • Verify PHP Settings: Double-check that php_openssl and php_sockets are enabled, as these are essential for secure communication with Gmail’s SMTP server.

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion 

In short, Sendmail for WAMP allows easy email testing on localhost by configuring Gmail’s SMTP settings. This setup streamlines development and ensures proper email functionality.

In brief, our Support Experts demonstrated how to fix the “554 5.7.1 : Relay access denied” error.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Get featured on the Bobcares blog and share your expertise with a global tech audience.

WRITE FOR US
server management

Spend time on your business, not on your servers.

TALK TO US

Or click here to learn more.

Speed issues driving customers away?
We’ve got your back!