Send form data via email and post it on: author/source: limodou/phprecord.yeah.net by: JulieMeloni translator: limodou using PHP to send data to a specified mailbox is so simple that you may wonder why people do not use it every day? The mail () function provided by PHP uses mail to send form data.
Published on: PHP Alliance of China www.phpx.com author/source: limodou/phprecord.yeah.net
Author: Julie Meloni translator: limodou
Using PHP to send data to a specified mailbox is so simple that you may wonder why people do not use
What about it? The mail () function provided by PHP uses four parameters: recipient, subject, mail information, and additional mail header. Through this short tutorial
You will learn how to set the values of these parameters, then send an email and return a confirmation page. We will implement the following two steps: Accuracy
Prepare form files and write PHP programs to process form data, send emails and return results. Note: In this example, the PHP file name suffix is used
". Php3 ". If your PHP file name suffix is different from this, such as ". phtml" and ". php", modify the file name accordingly.
Step 1: Use your frequently-used editor to create an HTML page for a form. Personally, I am used to "show/do ".
Multi-step application: if a file name starts with "show _", it is usually a form File. if it starts with "do _", it is usually replaced by PHP
. The file I created here is "show_form.html", which contains two text fields and one multi-line text field, as shown below:
E-Mail Form
Let's check this code in part. First, the form action is set to "do_sendform.php3 ". This file
Is the program file that you want to process form data and send emails. Then we use two text fields and one multi-line text field. Default,
The name of the input field is the variable name passed to "do_sendform.php3. Therefore, when the form is sent
Send three variables to "do_sendform.php3": $ sender_name, $ sender_email, and $ message. In this tutorial, you will
You send emails by yourself, so the recipient's mailbox and subject will be programmed in the PHP script.
Is it easy? OK. Let's take a look at the PHP code. Create the "do_sendform.php3" file in your editor.
Add the code at the beginning:
If you do not know whether the variable tracking option (track_vars) is enabled, adding this line of statements will ensure that at least in this script, the variable
Tracking is allowed.
The following link the form data into a long string. You can add the carriage return (n) and tab (t) in the appropriate places ). In your PHP
Add the following code snippet:
$ Msg = "Sender Name: t $ sender_namen ";
In this line, you created the $ msg variable to save the message string. First, you added the "Sender Name:" string followed by $ sender_name.
And a carriage return. After the script is executed, the variable $ sender_name is replaced by the text entered in the form.
Append a new string to $ msg. Note that you can use the. = operator when adding a new string to the original variable.
$ Msg. = "Sender E-Mail: t $ sender_emailn ";
$ Msg. = "Message: t $ messagenn ";
The last line contains two carriage returns. Here, you create a long string, like:
$ Msg = "Sender Name: t $ sender_namenSender E-Mail: t $ sender_emailnMessage: t $ messsagenn ";
No matter what, when you have to find a line of text for modification, use a connector and use a blank space to separate your code.
Is easier. Remember, blank space is your friend and will make those guys who have to edit your things think you are
Popular people.
At the beginning of this tutorial, I have already said that the mail () function has four parameters: recipient, subject, mail information, and additional mail header.
Now in your code, you can create variables to control their values-recipient (recipient), subject (topic), and mailheaders (
Email header );
$ Recipient = "you@youremail.com ";
$ Subject = "Web Site Feedback ";
Obviously, your mailbox and any theme you want are used here. Then, create a variable for the Mail header. Some basic
The mail header is "From" and "Reply-". Create a variable named $ mailheaders. add it as follows:
$ Mailheaders = "From: My Web Site <> n ";
$ Mailheaders. = "Reply-To: $ sender_emailnn ";
Use "<>" in the "From" header to indicate an empty email address. You can put the variable $ sender_name in the same way as the "Reply-To" header
"From" header, but if the user does not enter the sender_name field, an empty "From" header cannot pass through your email server. Best
Enter some general information and use the "Reply-To" header as the sender's Reply email address.
Now that you have all the things required by the mail () function, put the mail () function into your code:
Mail ($ recipient, $ subject, $ msg, $ mailheaders );
Finally, you should return some information to the user to notify them that the form has been sent. In addition, you should consider the possible
People will sit here and click the million send button to paralyze your mailbox. Add the following lines to your PHP code:
Echo" The form has been sent!";
Echo "Thank you, $ sender_name ";
Echo"
Feedback form sent
";
Echo"";
?>
Use the variable $ sender_name as the content returned to the user's screen and as a custom output. Or, maybe as a friend
Okay. In any situation, users should be friendly so that they can access your site.
All your PHP code looks like the following:
$ Msg = "Sender Name: t $ sender_namen ";
$ Msg. = "Sender E-Mail: t $ sender_emailn ";
$ Msg. = "Message: t $ messagenn ";
$ Recipient = "you@youremail.com ";
$ Subject = "Web Site Feedback ";
$ Mailheaders = "From: My Web Site <> n ";
$ Mailheaders. = "Reply-To: $ sender_emailnn ";
Mail ($ recipient, $ subject, $ msg, $ mailheaders );
Echo" The form has been sent!";
Echo "Thank you, $ sender_name ";
Echo"
Feedback form sent
";
Echo"";
?>