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
  Click to open user profile  
thefansbuzz, 19th Feb 2023, 7:32 PM
Cheap SMM Panel Social Media Marketing is a huge field with a lot of competition. If you want to stand out from the crowd, you need to know how to find ways to make your content go viral. If you are interested in using social media to market your product, this service is perfect for you. The panel gives you access to hundreds of different social media sites, so that you can find new ways to market your business. Visit our website at: https://thefansbuzz.com/
 
 
  Click to open user profile  
kerchiefraid, 26th Jun 2023, 8:40 AM
The first is called SMTP that defines your email server address. The second is called sendmail_from which defines your own email address. https://tunnel-run.com
 
 
  Click to open user profile  
orlandoolivia, 31st Aug 2023, 9:40 AM
Selecting your seat on Spirit Airlines is easy and flexible. From extra legroom to window seats, they offer a variety of options for your comfort. Contact their customer service team at +1-845-459-2806 to learn more and get personalized assistance in securing the perfect seat for your needs. Don't forget to have your booking details ready when you call. Enjoy a comfortable flight with Spirit Airlines.

 
 
  Click to open user profile  
orlandoolivia, 1st Sep 2023, 11:10 AM
If you need assistance with your Wingo travel arrangements, our customer support team is available at https://www.aairtickets.com/espanol/wing.... We can assist you with bookings, answer any questions, and provide exceptional service. Our goal is to ensure that your journey is a memorable one. Don't hesitate to reach out to us for reliable support.
 
   
 
 
online learning made for people
Dreevoo.com | CONTRIBUTE | FORUM | INFO