简体   繁体   中英

How to submit a form to an email in php

When I tried to submit the form , It's been sent successfully , But I didn't get any data in the email . Here's my code :

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

try it

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

check your INBOX otherwise check your spam

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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