Use correct headers "From $email" is not valid! Valid Name-value-Pairs of headers are Name: Value\r\n
Use wp_mail instead of mail if you using WordPress, otherwise use a PHPMailer Class. If your using wp_mail, set the headers as an Array
validate your input correctly. Dont use yor function isEmail, use PHP's own filter_var
No receiving mails? Try it out: Go to the wp-login.php and reset your passwort! Mails still not send, your server is not configured for sending mails contact your Server admin!
My contact.php form in wordpress does not send the message
Question
I have a file, contact.php containing PHP code and an html form but when I want to send a message I can't. I try to find the error code or problem within the code but I can't find either.
This is entire my contact.php file, please help me identify the problem.
<?php
// http://www.infotuts.com/bootstrap-3-contact-form/
// Function for email address validation
function isEmail($verify_email) {
return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i", $verify_email)
);
}
$error_name = false;
$error_email = false;
$error_message = false;
if (isset($_POST['contact-submit'])) {
// Initialize variables for the form fields
$name = '';
$email = '';
$website = '';
$message = '';
$receiver_email = '';
// Get the name
if (trim($_POST['contact-author']) == '') {
$error_name = true;
} else {
$name = trim($_POST['contact-author']);
}
// Get the email
if (trim($_POST['contact-email']) == '' || !isEmail(trim($_POST['contact-email']))) {
$error_email = true;
} else {
$email = trim($_POST['contact-email']);
}
// Get the website
$website = trim($_POST['contact-url']);
// Get the message
if (trim($_POST['contact-message']) == '') {
$error_message = true;
} else {
$message = stripcslashes(trim($_POST['contact-message']));
}
// Check if we have errors
if (!$error_name && !$error_email && !$error_message) {
// Get the received email
$receiver_email = 'rerraw@gmail.com';
$subject = 'You have been contacted by' . $name;
$body = "You have been contacted by $name. Their Message is:". PHP_EOL . PHP_EOL;
$body .= $message . PHP_EOL . PHP_EOL;
$body .= "You can contact $name via email at $email.";
if ($website != '') {
$body .= "and visit their website at $website";
}
$body .= PHP_EOL . PHP_EOL;
$headers = "From $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Contetnt-type text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: qouted-printable" . PHP_EOL;
if (wp_mail($receiver_email, $subject, $body, $headers)) {
$email_sent = true;
} else {
$email_sent_error = true;
}
}
}
?>
<?php if (isset($email_sent) && $email_sent = true) : ?>
<h3><?php _e('Success', 'Yallanpe-Theme'); ?></h3>
<p><?php _e('You have successfully sent the message.', 'Yallanpe-Theme'); ?></p>
<?php elseif (isset($email_sent_error) && $email_sent_error = true) : ?>
<h3><?php _e('Error!', 'Yallanpe-Theme'); ?></h3>
<p><?php _e('We coudnt send the message at this time please try again later.', 'Yallanpe-Theme'); ?></p>
<?php else : ?>
<div id="contact-us" class="col-lg-4 col-md-6 col-sm-6 col-xs-12">
<h4>Contact Us</h4>
<form action="" method="POST" id="contact-form" role="form">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input value="<?php if (isset($_POST['contact-author'])) echo $_POST['contact-author']; ?>" type="text" class="form-control" name="contact_author" id="contact_author" placeholder="First Name" />
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span> </span>
<input value="<?php if (isset($_POST['contact-email'])) echo $_POST['contact-eamil']; ?>" type="text" class="form-control" name="contact_email" id="contact_email" placeholder="Email Address" />
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span></span>
<input value="<?php if (isset($_POST['contact-url'])) echo $_POST['contact-url']; ?>" type="text" class="form-control" name="contact_url" id="contact_url" placeholder="Wbsite URL" />
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-comment"></span></span> <textarea class="form-control" name="contact_message" id="contact_message" cols="60" rows="5" placeholder="Write Your Message Here"><?php if (isset($_POST['contact-message'])) echo $_POST['contact-message']; ?></textarea>
</div>
</div>
<input type="hidden" name="contact-submit" id="contact-submit" value="true" />
<input type="submit" class="btn btn-primary" value="Send Message" />
</form>
</div>
<?php endif; ?>
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow