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

chris85
  • 23,846
  • 7
  • 34
  • 51
Redondo Velasco
  • 103
  • 1
  • 8
  • You need use header for function mail and set `Content-type` lock example number 4 for you task in manual http://php.net/manual/en/function.mail.php – Naumov Mar 27 '16 at 16:05
  • Possible duplicate of [PHP mail form doesn't complete sending e-mail](http://stackoverflow.com/questions/24644436/php-mail-form-doesnt-complete-sending-e-mail) – Qirel Mar 27 '16 at 16:05
  • hi @Naumov how exactly can you do that? sorry im a noob in PHP – Redondo Velasco Mar 27 '16 at 16:07

1 Answers1

1

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 
} 
?>
Naumov
  • 1,167
  • 9
  • 22
  • i can't still to recieve an email. Do i need to put something in the html header? Or do i need to make my website hosted first? – Redondo Velasco Mar 28 '16 at 02:44
  • You site install in localhost? you can hosted your website if you server (localserver) do not have set up (postfix, sendmail) or over application for send email. – Naumov Mar 28 '16 at 11:00