本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

如何使用 php 向电子邮件提交表单

发布于2024-12-12 18:13     阅读(560)     评论(0)     点赞(21)     收藏(5)


当我尝试提交表单时,它已成功发送,但我没有在电子邮件中收到任何数据。这是我的代码:

 <!DOCTYPE html>
            <head>
               <title>Form submission</title>
            </head>
            <body>
               <form action="email.php" method="post">
                  First Name: <input type="text" name="first_name"><br>
                  Last Name: <input type="text" name="last_name"><br>
                  Email: <input type="text" name="email"><br>
                  Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
                  <input type="submit" name="submit" value="Submit">
               </form>
            </body>
            </html>

    <?php 
    if(isset($_POST['submit'])){
        $to = "aryanpallive@gmail.com"; // this is your Email address
        $from = $_POST['email']; // this is the sender's Email address
        $first_name = $_POST['first_name'];
        $last_name = $_POST['last_name'];
        $subject = "Form submission";
        $subject2 = "Copy of your form submission";
        $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
        $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

        $headers = "From:" . $from;
        $headers2 = "From:" . $to;
        mail($to,$subject,$message,$headers);
        mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
        echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
        // You can also use header('Location: thank_you.php'); to redirect to another page.
        }
    ?>

解决方案


试一试

$to = 'aryanpallive@gmail.com';
$subject = "Form submission";
$message = 'testing';

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";
mail($to,$subject,$message,$headers);

检查你的收件箱,否则检查你的垃圾邮件




所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.qianduanheidong.com/blog/article/538571/a0ca4695a79a7fd54e15/

来源:前端黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

21 0
收藏该文
已收藏

评论内容:(最多支持255个字符)