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 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)