How to configure php mail function in wamp server

Configuring the PHP mail function in WAMP (Windows, Apache, MySQL, PHP) server involves setting up the mail server and configuring the PHP settings. Here's a step-by-step guide to help you do so:

Step 1: Install a mail server

You'll need to install a mail server on your Windows machine. There are several options available, including:

  1. WAMP's built-in mail server: WAMP comes with a built-in mail server called Mercury. To enable it, go to the WAMP icon in the system tray, click on "PHP" and then "Mercury Mail Server". Follow the installation instructions.
  2. MS Outlook: You can use Microsoft Outlook as a mail client and configure it to work with WAMP.
  3. Other mail servers: You can also install other mail servers like Sendmail, Postfix, or Exim.

Step 2: Configure PHP settings

Edit the php.ini file to configure the PHP mail settings. You can find the php.ini file in the C:\wamp\bin\php\phpX.Y.Z directory, where X.Y.Z is the version of PHP installed on your system.

  1. Open the php.ini file in a text editor (e.g., Notepad++).
  2. Search for the SMTP settings and update them as follows:
    • SMTP = localhost (or the hostname of your mail server)
    • smtp_port = 25 (or the port number of your mail server)
    • sendmail_from = [email protected] (replace [email protected] with your email address)
  3. Save the changes to the php.ini file.

Step 3: Configure the mail server

If you're using WAMP's built-in mail server (Mercury), you'll need to configure it to work with PHP. To do so:

  1. Go to the WAMP icon in the system tray and click on "Mercury Mail Server".
  2. In the Mercury Mail Server window, click on "Settings" and then "SMTP".
  3. Update the SMTP settings as follows:
  4. Click "Apply" and then "OK" to save the changes.

Step 4: Test the PHP mail function

Create a PHP file with the following code:

<?php
mail('[email protected]', 'Test Email', 'This is a test email');
?>

Replace [email protected] with the email address you want to send the email to.

Save the file with a .php extension (e.g., testmail.php) and upload it to your WAMP server's www directory.

Open a web browser and navigate to http://localhost/testmail.php. If everything is configured correctly, you should receive an email at the recipient's email address.

That's it! You should now be able to use the PHP mail function in your WAMP server.