[PHP Tutorial 7] Forms with Emails in PHP

4

In the previous PHP tutorial on how to use GET and POST methods in PHP, you learnt how to use GET and POST methods in PHP which are used to fetch data filled by the user in a form. In this tutorial, you will learn how to send emails using PHP.

Before starting with email, ensure if the mail configuration has been done in PHP installed in your computer otherwise you will not be able to send emails.

Configuring PHP for Emails

To configure mail, open php.ini file stored in etc folder and edit the following:

[mail function]; For Win32 only
SMTP = your_email_server_name

; For Win32 only
sendmail_from = your_email_address

Mention the SMTP as the name of your server and sendmail_from as youremail address from where you want to send emails.

This tutorial will demonstrate how to send the data, filled in the form by the user, to the recipient’s mail system.

Combining what we have learnt in Tutorial 5 and Tutorial 6, we can create a form using the following code:

<form action="mail.php" method="post">
Name: <input type="text" name="name"><br>
Your E-mail: <input type="text" name = "email"><br><br>
Your Comments<br>
<textarea name="comments"></textarea><br><br>
<input type="submit" value="Submit">
</form>

Now that we have created a form where the user will be entering the details viz. name, e-mail address and comments; we create the PHP script “mail.php” where the data is going to be sent.

<?
$name=$_POST['name'];
$email=$_POST['email'];
$comments=$_POST['comments'];
$to="write_the_email_address_of_recipient_here";
$message="$name, $email commented on your site. \n$comments\n”;
if(mail($to,"comments",$message,"From: $email\n"))

{
echo "Thank you!";
}

else

{
echo "The email couldn’t be sent. Fill the data properly.";
}
?>

Save this script as “mail.php” as mentioned in the form.

Now, let us describe the above PHP script a bit. mail($to,”comments”,$message,”From: $email\n”) is the mail() function used to send the mail.

The general form of mail() function is: mail($to,$subject,$body,$headers); where $to will be used for sending the mail to the recipient, $subject will add subject to the mail, $body will store the message to be sent and $header will constitute cc or bcc which is optional.

To make this live, both the PHP script and the form created above should be uploaded.

Sending Emails using PHP

We can send emails using PHP script to anyone. Let us see how it can be done.

The mail() function described above will be used as:

mail("recipient’s email","subject","Your message","From: sender’s email\n");

We can also do this in another way (as follows):

$to = " recipient’s email ";
$subject = "subject ";
$body = " Your message ";
$headers = " From: sender’s email \n";
mail($to,$subject,$body,$headers);
echo "Mail sent to $to";

This will send the message “Your message” with the subject “subject” to “recipient’s email” from “sender’s email”. And the sender will get a message on the screen that mail has been sent to the recipient.

Error Handling

If incorrect information has been filled, to display the error; we can write:

if(mail($to,$subject,$body,$headers))

{
echo "Your mail sent to $to";
}

else

{
echo "The email couldn’t be sent. Check if the filled information is correct";
}
Thus, an error message will be displayed if the information is found to be incorrect.
Now that you know how to send mails using PHP, continue with the next PHP tutorial on how to set cookies in PHP. Also, if you have any queries, please leave a comment below.
Share.

4 Comments

    • In html also a mail script is used to get the data submitted in the form. Any server sided script can be used like Perl, PHP. So, if you are using PHP script, mail should be configured as described in the tutorial.
      The code used as:
      <form action=”mail_script.php” method=”post” enctype=”text/plain”>

      Another way of sending emails is using “mailto” as follows:
      <form action=”mailto:recipient’s email id” method=”post” enctype=”text/plain”>
      This code will use the default application for sending mails. It will work in the same way as microsoft outlook works.

Leave A Reply