0

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!

benlevywebdesign
  • 339
  • 3
  • 13
  • Replace `$to` with the user's email address, and adjust `$body` to contain whatever text you want... – Colin Brock Jun 02 '12 at 15:34
  • Show us yours form, and comment what do you need more detail – byCoder Jun 02 '12 at 15:35
  • I wanted to send one to me and them one to them with the data they submitted in the form that includes a little message using there name and thanking them on submit of php form. I don't know any php so spell it out for me please thanks – benlevywebdesign Jun 02 '12 at 15:36
  • @benlevywebdesign: Everything is spelled out pretty clearly [in the documentation](http://us3.php.net/manual/en/function.mail.php). It's generally expected that you put in some effort here. As it stands, it sounds like you want someone to write your code for you... – Colin Brock Jun 02 '12 at 15:42
  • @Colin I tried doing somethings and it messed up the email to me so I just want to know where to place the new code and how to get started that's all – benlevywebdesign Jun 02 '12 at 15:43

2 Answers2

1

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

das_j
  • 4,444
  • 5
  • 31
  • 47
1

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 :)

Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • How do I send a different email to the user? I want to send a custom message to them with their name and a little message while just sending me a email like I have above. – benlevywebdesign Jun 02 '12 at 15:53
  • In that case you have to just copy the code and change the email contents and subject. – Ja͢ck Jun 02 '12 at 16:00
  • ok I got it to send two messages but how do I add a custom message to the user email? – benlevywebdesign Jun 02 '12 at 16:14
  • @benlevywebdesign customizing an email is the same as making a big string, using the posted fields as a part of it ... maybe I'm not entirely following what kind of customization you'd like to do – Ja͢ck Jun 03 '12 at 04:24
  • I got it and figured it out. Is it easy to add color or bold styles to my message? – benlevywebdesign Jun 04 '12 at 16:29
  • If you're sending HTML email that should be no problem. Just keep on mind that CSS had to be applied inline because not all email clients understand style tag – Ja͢ck Jun 04 '12 at 23:43
  • Just add "\r\nContent-Type: text/html; charset=utf-8" to your `$headers` and create your HTML – Ja͢ck Jun 05 '12 at 01:40