简体   繁体   中英

How do I send an email to the user on submit of php form

How do I send an email to the user with the data they submitted in the form that includes a little message using there name and thanking them on submit of php form.

Here is my current php code. It currently just shows them a message that says there name and that the message has been sent and then sends me an email to my email address.

<?php
    if(isset($_POST['submit'])){

        $to = "benlevygraphics@gmail.com";
        $headers = "From: " . $_POST['email'];
        $subject = "Ben, you have been contacted...";
        $body = "Name: " . $_POST['name'] . "\nEmail: " . $_POST['email'] . "\nWebsite: " . $_POST['web'] . "\nMessage: " . $_POST['message'];

        if(mail($to, $subject, $body, $headers)){

        echo("<p class=contactformsent>".$_POST['name'].", your message has been sent!</p>");

        }
        else{
           echo("<p class=contactformnotsent>".$_POST['name'].", Message delivery failed...</p>");
        }
   }
?>

I am new to php and I have read stuff online and I still don't understand so if you could be clear in your examples or help I would greatly appreciate it very much. Thanks!

Look into your php.ini beacuse you have to enter a SMTP Server. At my file it begins in line 1087 with "[mail function]"

Assuming your current code is already working fine, you can do this to send yourself an email together with the recipient:

  1. Set $to to $_POST['email']
  2. Set $headers to "From: {$_POST['email']}\\r\\nBcc: benlevygraphics@gmail.com"
  3. Adjust $body and $subject to your needs.

Btw, I can't say this often enough; make sure that your page has some form of CSRF protection.

How to properly add CSRF token using PHP

The above is just one way, there are others, just search for it :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM