douyiavxxh02727 2012-06-02 15:32
浏览 89
已采纳

如何在提交php表单时向用户发送电子邮件

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'] . "
Email: " . $_POST['email'] . "
Website: " . $_POST['web'] . "
Message: " . $_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!

  • 写回答

2条回答 默认 最新

  • dsce23640 2012-06-02 15:46
    关注

    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']} Bcc: 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 :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据