Form To Email - HTML Dog
Có thể bạn quan tâm
There are numerous ways of achieving this, with numerous wild and wonderful programming languages, but we’re going to use a scripting language called PHP.
PHP stands for PHP: Hypertext Preprocessor (yeah — it’s recursive — bonkers) and it works hand-in-hand with HMTL. It’s easy and it’s prolific. In all likelihood the web hosting that is home to your HTML pages on the web will be PHP-enabled.
Step 1: Create a PHP page
If you use the extension “.php” instead of “.html” for your web page, the server hosting the page will know to execute any PHP sitting within it. So, to start, simply save an empty HTML page as “contact.php”.
New Examples Section! See all of this code stuff in action, and play around with it.
Step 2: Create a form
You can read about the ins and outs of forms in the HTML Beginner Tutorial. To jump straight in, let’s use the following super-stripped-down form HTML:
<form method="post" action="contact.php"> <textarea name="message"></textarea> <input type="submit"> </form>All quite straightforward, right? The action="contact.php" bit tells the page to send the form’s content to itself when the form is submitted. Crazy, huh? We’re doing everything here with the same page…
Step 3: Send the form’s data in an email
At the very top of the page, even before the Doctype, we are going to enter a smattering of PHP to handle the form data:
<?php if($_POST["message"]) { mail("[email protected]", "Form to email message", $_POST["message"], "From: [email protected]"); } ?>“<?php” marks the start of the PHP and “?>” marks the end. The server will attempt to execute everything in between as PHP.
This code checks if form data has been sent and, if it has, it uses the mail function to send the data as an email to “[email protected]” with the subject “Form to email message” and the message body that is the same as the form field with the name “message”. The email will appear to be from “[email protected]”.
Recap
You can try this out once you upload it (it requires the server to run — you won’t be able to run it locally).
Here’s what’s happening:
- Once the form has been submitted, the page will send the data to itself.
- The page will check if data has been sent and, if it has, it will send it on as an email.
- The browser will load the page’s subsequent HTML, including your form.
Further steps: A basic “Contact us” page
OK, how about making this a bit more like a “Contact us” form? We can add sender name and sender email address to the message and also include a confirmation message, to let the sender know their message has been sent. Here’s the whole shebang:
<?php if($_POST["submit"]) { $recipient="[email protected]"; $subject="Form to email message"; $sender=$_POST["sender"]; $senderEmail=$_POST["senderEmail"]; $message=$_POST["message"]; $mailBody="Name: $sender\nEmail: $senderEmail\n\n$message"; mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>"); $thankYou="<p>Thank you! Your message has been sent.</p>"; } ?><!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Contact form to email</title> </head> <body> <?=$thankYou ?> <form method="post" action="contact.php"> <label>Name:</label> <input name="sender"> <label>Email address:</label> <input name="senderEmail"> <label>Message:</label> <textarea rows="5" cols="20" name="message"></textarea> <input type="submit" name="submit"> </form> </body> </html>This time we’re:
- Checking the form has been sent (this time by looking for the form field named “submit”) and, if it has…
- Setting a bag-load of variables:
- Your email address
- The email subject
- The sender’s name (taken from the form)
- The sender’s email address (taken from the form)
- The message (taken from the form)
- Sending the email, using all of those variables
- Setting a variable for a confirmation message
- Loading the HTML, including the confirmation message
If the page loads and no form data has been sent then no email will be composed and no conformation message will be set so all that will happen is loading of the page’s HTML.
Từ khóa » Html Form Send Email Php
-
How To Send HTML Form Data To Email Using PHP - HMA WebDesign
-
Send Email With PHP From Html Form On Submit With The Same Script
-
PHP Form To Email Explained - HTML Form Guide
-
Send Email From A Web Form With PHP - LinkedIn
-
PHP Email Contact Form - Mailtrap
-
Send An Email On Form Submission Using PHP - FormGet
-
How To Create An Email Form With PHP
-
PHP Form Handling - W3Schools
-
How To Create An HTML Form That Sends You An Email
-
How To Send Text And HTML Emails In PHP - Tutorial Republic
-
Php Send Email Form Code Example
-
Send Email From HTML Form With PHP | PHP Contact Form - YouTube
-
How To Create A Simple HTML Contact Form - MajesticForm
-
PHP Contact Form Send Email - Contact Form With Php Mail For Your ...