0

I'm trying to send a email by a form but its not working, everytime I try to send it asks to download the php file.

The HTML email code form is this one:

<form method="post" name="contact_form" action="contact-form-handler.php">
    <div class="form-group">
        <input type="text" class="form-control" placeholder="Name" name="name">
    </div>
    <div class="form-group">
        <input type="text" class="form-control" placeholder="Email" name="email">
    </div>
    <div class="form-group">
        <input type="text" class="form-control" placeholder="Subject" name="subject">
    </div>
    <div class="form-group">
        <textarea name="message" id="message" cols="30" rows="7" class="form-control" placeholder="Message"></textarea>
    </div>
    <div class="form-group">
        <input type="submit" class="btn btn-primary btn-send-message" value="Send Message">
    </div>

Which results the following form: enter image description here

The PHP code is the following:

<?php
$errors = '';
$myemail = 'angelribeiro.10@gmail.com';
if(empty($_POST['name'])  ||
    empty($_POST['subject']) ||
    empty($_POST['email']) ||
    empty($_POST['message']))
{
    $errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$email_subject = $_POST['subject'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>

Thanks a lot.

Mark
  • 1,852
  • 3
  • 18
  • 31
user2535338
  • 355
  • 4
  • 20
  • pos dupes: https://stackoverflow.com/questions/18379238/send-email-with-php-from-html-form-on-submit-with-the-same-script https://stackoverflow.com/questions/8803994/php-mail-not-working-for-some-reason https://stackoverflow.com/questions/2757113/php-mail-function https://stackoverflow.com/questions/11300698/sending-html-email-using-php-including-the-html-file https://stackoverflow.com/questions/14456673/sending-email-with-php-from-an-smtp-server https://stackoverflow.com/questions/18379238/send-email-with-php-from-html-form-on-submit-with-the-same-script – treyBake Dec 11 '19 at 16:15
  • Your HTML appears to be incomplete. Do you have the closing `` tag? – Funk Forty Niner Dec 11 '19 at 16:17
  • 1
    Does this answer your question? [Send email with PHP from html form on submit with the same script](https://stackoverflow.com/questions/18379238/send-email-with-php-from-html-form-on-submit-with-the-same-script) – Mark Dec 11 '19 at 16:19
  • 2
    *"every time I try to send it asks to download the php file."* - I see what this is about now. Consult the duplicate(s). Most importantly the first one. – Funk Forty Niner Dec 11 '19 at 16:21

0 Answers0