Issue
EDIT: Turns out that my problem was caused by hackers flooding my email server and that is why I wasn't receiving emails. I inserted a captcha on my form and it began working.
I have a website that uses a custom php file to submit email leads.
When someone fills out the contact form, it shows the success message, but the email never arrives in my inbox.
Here is the HTML code:
<form name="contactform" method="post" action="php/send_form_email.php">
<fieldset>
<label for="first_name">First Name<br>
<input type="text" class="llonger" name="first_name" maxlength="50" size="30" placeholder="John">
</label>
<label for="last_name">Last Name<br>
<input type="text" name="last_name" maxlength="50" size="30" placeholder="Smith">
</label>
<label for="email">Email Address<br>
<input type="email" name="email" maxlength="80" size="30" placeholder="my@email.com">
</label>
<input type="submit" value="Submit" class="submitlonger">
</fieldset>
</form>
This is the PHP code:
<?php
if(isset($_POST['email'])) {
$email_to = "email@website.com";
$email_subject = "Contact Submission";
function died($error) {
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo $error;
die();
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$email_message = "Form details below.\n\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
Solution
looks like you need to wrap your mail function in a conditional...
if(mail($email_to, $email_subject, $email_message, $headers)){
//display success...
}else{
//display fail...
}
this will suppress the erroneous 'success message'..
the mail function returns true/false but true doesn't necessarily mean it worked if it returned true... see here... http://php.net/manual/en/function.mail.php
also, if it were me I'd remove the "@" at the begining of the mail function, so that you can catch any possible syntax, or other errors that may be preventing it from working...
Edit: in context example...
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($email_to, $email_subject, $email_message, $headers)){
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}else{
?>
<!-- fail html -->
Email could not be sent...
<?php
}
EDIT2: pure php...
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($email_to, $email_subject, $email_message, $headers)){
echo 'Thank you for contacting us. We will be in touch with you very soon.';
}else{
echo 'Email could not be sent...';
}
Answered By - jsuna
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.