Apply PHP to establish contact and send email

Source: Internet
Author: User
Tags valid email address contact form
Use PHP to establish contact and send email
Having a contact form on your site is critical when you need to know what your site's visitors think of your site. We will first create a simple 3-field contact form?? -Email address, name, comment. I'll use a table to adjust the 3-field and the Send button. Create a new file and paste the code underneath it. Save it as test.php and upload it to your Web server. Now, you have a simple contact form on a webpage (http://www.software8.co).

Then we need the actual PHP code to send the email when the form above is submitted. We need to define the message that should be sent to ($ toemail), and also for the message that will be sent ($ EmailSubject) to the subject of the email. Changing the youremail@site.com message should be sent to your email address and your message also adds a suitable subject. The dollar Mailheader variable is used to define the message header. We target the Content Type field from the reply and message. There are some more available, but in this case we will only use these 3 areas. Depending on your server configuration, you may need to have a valid email address for the field from your server. If you have a domain name mysite.com, then you should use a valid email address such as contact information: MySite. Com. In this example, I send an e-mail, using the actual e-mail address, submitted via the live form. Next, all data submitted through the network form is saved in the USD message_body variable from the $ _post variable. Using the NL2BR feature, you will make all the new lines in your comment box also appear on the new line in your email. We have all the data we need for our email will use the mail () function to send us an email.


$ToEmail = ' youremail@site.com ';

$EmailSubject = ' Site contact form ';

$mailheader = "From:". $_post["Email"]. " \ r \ n ";

$mailheader. = "Reply-to:". $_post["Email"]. " \ r \ n ";

$mailheader. = "content-type:text/html; Charset=iso-8859-1\r\n ";

$MESSAGE _body = "Name:". $_post["Name"]. "
";

$MESSAGE _body. = "Email:". $_post["Email"]. "
";

$MESSAGE _body. = "Comment:". NL2BR ($_post["Comment"]). "
";

Mail ($ToEmail, $EmailSubject, $MESSAGE _body, $mailheader) or Die ("Failure");

?>

What we're going to do now is to combine the Web form and email sending code to a single page. We will use an if statement to check if a table has been submitted, and if so, it will send the message that the e-mail will be displayed on the screen instead of the Web Form "Your mail is sent".

if ($_post["email"]<> ") {

$ToEmail = ' youremail@site.com ';

$EmailSubject = ' Site contact form ';

$mailheader = "From:". $_post["Email"]. " \ r \ n ";

$mailheader. = "Reply-to:". $_post["Email"]. " \ r \ n ";

$mailheader. = "content-type:text/html; Charset=iso-8859-1\r\n ";

$MESSAGE _body = "Name:". $_post["Name"]. "
";

$MESSAGE _body. = "Email:". $_post["Email"]. "
";

$MESSAGE _body. = "Comment:". NL2BR ($_post["Comment"]). "
";

Mail ($ToEmail, $EmailSubject, $MESSAGE _body, $mailheader) or Die ("Failure");

?>

Your message was sent


} else {

?>




};

?>
Industry Portal (www.software8.co) article, I hope you can message suggestions
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.