繁体   English   中英

如何在php中向电子邮件提交表单

[英]How to submit a form to an email in php

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

 <!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.
        }
    ?>
    <!DOCTYPE html>
    <head>
    <title>Form submission</title>
    </head>
    <body>
    <form action="" 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 = "acbhaskar1@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'];
    $headers = "From:" . $from;
    if(mail($to,$subject,$message,$headers)){        
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    }
    else{echo "mail has not been sent";}

    }
    ?>


  [![Output][1]][1]

  [1]: https://i.stack.imgur.com/hgY1g.png

试试吧

$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);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM