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

如何在提交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!

图片转代码服务由CSDN问答提供 功能建议

如何使用包含一条消息的表单向用户发送包含一条消息的数据 名字并感谢他们提交php表格。

这是我目前的PHP代码。 它目前只是向他们显示一条消息,上面写着姓名并且消息已经发送,然后向我发送电子邮件到我的电子邮件地址。

 &lt;?php 
 if  (isset($ _ POST ['submit'])){
 
 $ to =“benlevygraphics@gmail.com”; 
 $ headers =“From:”。  $ _POST ['email']; 
 $ subject =“Ben,你已被联系过......”; 
 $ body =“姓名:”。  $ _POST ['name']。  “
电子邮件:”。  $ _POST ['email']。  “
网站:”。  $ _POST ['web']。  “
消息:”。  $ _POST ['message']; 
 
 if(mail($ to,$ subject,$ body,$ headers)){
 
 echo(“&lt; p class = contactformsent&gt;”。$ _ POST [' 姓名']。“,您的邮件已发送!&lt; / p&gt;”); 
 
} 
其他{
 echo(“&lt; p class = contactformnotsent&gt;”。$ _ POST ['name']  。“,邮件传递失败...&lt; / p&gt;”); 
} 
} 
?&gt; 
   
 
 

我是php新手 我已经在线阅读了一些内容,但我仍然不明白,如果您能够清楚地了解您的示例或帮助我会非常感激。 谢谢!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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

    打赏 评论
  • doujie1908 2012-06-02 15:37

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

    打赏 评论

相关推荐 更多相似问题