Dreevoo.com | Online Learning and Knowledge Sharing
 
Home | Programs | Programming languages | PHP & MySQL | How to Send E-mail in PHP
Guest
Click to view your profile
Topics
Programs
Languages
Recipes
Home
Shortcuts
 
 

How to Send E-mail in PHP

We will learn how to create a simple HTML form for sending e-mail and how to process this data to send e-mail message in PHP.

 
  Author: podtalje | Version: php.net | 28th October 2013 |  
 
 
1.
 

We will use Dreamweaver to create a form for sending e-mail. If you are using any other HTML editor, you can skip to step 5.

Select tab Forms where you can find Text field.

 
 
2.
 

Add three Text fields to the form.

 
 
3.
 

Because we want input field for message to be a little larger, we add another field of type Textarea.

At the end of the form we also add Submit button which is responsible for submitting the form.

 
 
4.
 

Now we must set correct names to all the fields that we added to the form.

The fields names should be sender, recipient, subject and message.

 
 
5.
 

Complete HTML code of the form can be found below:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>PHP E-mail</title>
</head>

<body>
<p><strong>Send e-mail message</strong></p>
<form method="post" action="">
<p>Sender:
   <input name="sender" type="text" id="sender">
</p>
<p>Recipient:
   <input name="recipient" type="text" id="recipient">
</p>
<p>Subject:
   <input name="subject" type="text" id="subject">
</p>
<p>Message:<br>
   <textarea name="message" cols="32" rows="7" id="message"></textarea>
</p>
<p>
   <input type="submit" name="Submit" value="Submit">
 </p>
</form> 
 <p>&nbsp; </p>
</body>
</html>


 
 
6.
 

We now also need some PHP code which will process data from the form and send e-mail message.

Add the following code at the top of the PHP document, before the <html> tag.

<?php
if (isset($_POST['recipient'])) {
  if ( !empty($_POST['sender']) && !empty($_POST['recipient']) && !empty($_POST['subject']) && !empty($_POST['message']) ) {
 
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=utf-8\r\n";
    $headers .= "From: ".$_POST['sender']." \r\n";
 
    if (mail($_POST['recipient'], $_POST['subject'], $_POST['message'], $headers)) {
        echo "E-mail was successfully sent!<br /><br />";
    } else {
        echo "E-mail was not sent. You need to fill out all the fields correctly.<br /><br />";
    }
  }
}
?>


As you can see, we first check if variable $_POST['subject'] is set. By doing this we see if there was a form submitted or was the page opened for the first time.

In the next step we verify if all fields have non-empty values.

In the variable $headers we define that we are sending e-mail message in UTF-8 encoding and also who will be shown in the message as sender of the message.

In the end we use PHP mail function to send the message and output a message if the mail was successfully sent.

 
 
7.
 

You can now upload the PHP document to a web server with PHP support and open it in browser.

Fill out the fields and click Send button.

You should now see a message that an email was successfully sent.

Also recommended:

 
 
 
   
  Please login to post a comment
   
 
 
online learning made for people
Dreevoo.com | CONTRIBUTE | FORUM | INFO