How to Send Email Using Mail Form in PHP

Sheeraz Gul Feb 15, 2024
  1. Install sendmail to Send Email From PHP on Local Servers
  2. Create and Send an Email Using a Mail Form in PHP
How to Send Email Using Mail Form in PHP

This tutorial will demonstrate installing the sendmail library and sending email through a PHP mail form.

Install sendmail to Send Email From PHP on Local Servers

PHP has a built-in function mail() to send emails. However, this function will not work till you install its library.

To install sendmail, follow the steps below.

  • Download and extract sendmail.

    Download sendmail from here. Then extract the zip file to C:\sendmail\.

  • Configure sendmail.ini.

    Now, open sendmail.ini from the main sendmail folder. Search and set the configuration as below.

    smtp_server=smtp.gmail.com
    
    smtp_port=587
    
    auth_username=The_email_from@gmail.com
    
    auth_password=Email Password
    
    force_sender=your_address@gmail.com
    

    This setting is for sending emails through Gmail, and you can also send them from others or your servers.

    The settings need to be set in these parameters. The auth_username and auth_password will be the email and password from where you want to send emails.

  • Configure php.ini.

    To configure, open the php.ini file and search for sendmail_path. Then set this parameter as C:\sendmail\sendmail.exe -t.

    ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
    ; http://php.net/sendmail-path
    sendmail_path ="C:\sendmail\sendmail.exe -t"
    

    Restart your local server, and it is done.

  • Test sending email.

    You can send an email with a simple one-line code to test, as shown below.

    <?php
    mail("example@hotmail.com","Test subject", "This is a test Email");
    ?>
    

    Output:

    Test Email

    As you can see, the output is a mail sent to the specified address.

Create and Send an Email Using a Mail Form in PHP

First, you must create an HTML form to send email data to a PHP code. Use that data to send emails using the mail() function.

Example:

<?php 
if(isset($_POST['submit']))
{
    $email_address = $_POST['email_address'];
    $subject = "This is a test email";

    $email_message = "The following message is sent in email:" . "\n\n" . $_POST['email_message'];
    mail($email_address,$subject,$email_message);
    echo "Email sent to ".$email_address;
    // You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<!DOCTYPE html>
<head>
    <title>PHP Mail Form</title>
</head>
<body>
    <form action="" method="post">
    Email: <input type= "email" name= "email_address"><br>
    Message:<br> <textarea rows= "10" name= "email_message" cols= "50"> </textarea> <br>
    <input type= "submit" name= "submit" value= "Send Email">
    </form>
</body>
</html>

Output:

Email sent to example@outlook.com

PHP Send Email Using Mail Form

The above code generated a form with email and message boxes. Then, the code sent the content written in the message box to the email address given in the email field.

Other fields like name, last name, and date can also be added to the form to send emails with more information.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - PHP Email