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

时间:2018-08-08 03:47:35

标签: php html

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

 <!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.
        }
    ?>

2 个答案:

答案 0 :(得分:1)

    <!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

答案 1 :(得分:0)

尝试

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

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