简体   繁体   中英

Sending an email through form type html in PHP

<?php
//if "email" variable is filled out, send email
if (isset($_REQUEST['email']))  {

    //Email information
    $admin_email = "personalemail@gmail.com";
    $email = $_REQUEST['email'];
    $subject = $_REQUEST['subject'];
    $comment = $_REQUEST['comment'];

    //send email
    mail($admin_email, "$subject", $comment, "From:" . $email);

    //Email response
    echo "Thank you for contacting us!";
}
//if "email" variable is not filled out, display the form
else  {
?>

<form method="post">
    <input name="email" type="text" class="form-control" placeholder="Enter your email address...">
    <input name="subject" type="text" class="form-control" placeholder="Subject">
    <br>
    <textarea name="comment" class="form-control" rows="3"></textarea>
    <br>
    <div class="mesbutts">
        <button type="submit" class="btn btn-primary" value="Submit">Send</button>
        <button type="reset" value="Reset" class="btn btn-default" >Clear</button>
    </div>

</form>
<?php 
} 
?>

Hi guys, I can't seem to figure out how can i make my PHP code work on sending an email through the use of form. I've already put my personal email and i cant seem to receive them. Can you help me spot the error in my syntax.

Thank you mate!

I think this is help you. But better way using library for send email by php, for example phpMailer, or over library.

   <?php
//if "email" variable is filled out, send email
if (isset($_REQUEST['email']))  {
    $headers  = 'MIME-Version: 1.0' . "\r\n"; // set mime version
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // set content-type as html
    //Email information
    $admin_email = "personalemail@gmail.com";
    $email = $_REQUEST['email'];
    $subject = $_REQUEST['subject'];
    $comment = $_REQUEST['comment'];

    //send email
    mail($admin_email, "$subject", $comment, "From:" . $email,$headers); // adding headers to mail

    //Email response
    echo "Thank you for contacting us!";
}
//if "email" variable is not filled out, display the form
else  {
?>

<form method="post">
    <input name="email" type="text" class="form-control" placeholder="Enter your email address...">
    <input name="subject" type="text" class="form-control" placeholder="Subject">
    <br>
    <textarea name="comment" class="form-control" rows="3"></textarea>
    <br>
    <div class="mesbutts">
        <button type="submit" class="btn btn-primary" value="Submit">Send</button>
        <button type="reset" value="Reset" class="btn btn-default" >Clear</button>
    </div>

</form>
<?php 
} 
?>

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