|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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> </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:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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/ |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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. |
|
|
|
|
|
|
|
|
Success from the first time when following instructions https://capybaraclicker.io. Because it has illustrations, all operations become easy. |
|
|
|
|
|
|
|
|
Gorilla Tag is a fun game that needs skill, patience, and persistence. As you gain levels, you get better at getting through tough territory and over obstacles. https://gorillatag.io/ |
|
|
|
|