I'm currently using a mailer.php file for a minimal contact form
However I have noticed recently then when submitting the form, I am getting the following error;
HTTP Error 405.0 - Method Not Allowed
The page you are looking for cannot be displayed because an invalid method (HTTP verb) is being used.
My mailer.php file is the following
<?php
$email_to = "admin#mydomain.co.uk";
$name = $_POST["name"];
$email_from = $_POST["admin#mydomain.co.uk"];
$message = $_POST["message"];
$email_subject = "Feedback from website";
$headers = "From: " . $email_from . "\n";
$headers .= "Reply-To: " . $email_from . "\n";
$message = "Name: ". $name . "\r\nMessage: " . $message;
ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email_from);
if ($sent)
{
header("Location: https://www.mydomain.co.uk/thankyou.html/");
} else {
echo "There has been an error sending your message. Please try later.";
}
?>
The form using this file has the following form tag
<form method="post" action="mailer.php">
PHP is not my strong point so I'm not sure why this maybe happening. The form itself does not have a captcha field. Could spam be the root of the issue?
I'd appreciate anyone who can shed this light on this for me
Related
I have a php contact form which has been working reliably for several years up until Jan 2022. It uses a Google reCaptcha. Apparently it fails at the mailcheck part which is near the end and prints out the "MAIL Sending Failed" message
<?php
//dont need these
error_reporting(E_ALL);
ini_set('display_errors', 1);
// check form is submitted
if ( isset($_POST['form_submit']) ) {
// get values
$error = ''; //error variable
$visitor_name = $_POST['name'];
$visitor_email = $_POST['email'];
$visitor_message = $_POST['message'];
$captcha = $_POST['g-recaptcha-response'];
//required values
if ( empty($visitor_name) || empty($visitor_email) ) {
$error = "<h3>Name and Email are required. END.</h3>";
}
//required captcha
if ( empty($captcha) ) {
$error = "<h3>Please check the the captcha form. END.</h3>";
}
// if no errors
if( empty($error) ){
$secretKey = " *** hidden *** ";
$ip = $_SERVER['REMOTE_ADDR'];
// post request to server
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' .
urlencode($secretKey) . '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response, true);
// should return JSON with success as true
if ($responseKeys["success"]) {
echo '<h3>Captcha Success. Writing Mail...</h3>';
// write mail
$to = " *** my user email ***.com";
$email_subject = "C&H form v2c inquiry";
// To send HTML mail, the Content-type header must be set
// If the From header is not set, then Luxsci servers reject
// $headers = "";
$headers = "From: *** my email ***.com \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
$headers .= "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "X-Mailer: PHP/".phpversion();
$email_body = "message from " . $visitor_name . "<br>" .
"sender's email: " . $visitor_email . "<br>" .
"here is the message: " . $visitor_message;
// compose headers
//Send the mail
$mail_check = mail($to, $email_subject, $email_body, $headers);
if ($mail_check) {
echo "<h5>Mail sending successful. Redirecting... </h5>";
echo '<script> window.location.href = "thank_you_CG.html"; </script>';
} else {
echo "<h5>MAIL sending failed.</h5><br<br>
<h3> This is the failure message it sends out HERE </h3>";
}
} else { // if response not success
echo '<h3>reCaptcha verification failed!</h3>';
echo "Response from reCaptcha: <br>";
print_r($responseKeys);
}
} else { //if errors
echo $error;
exit;
}
}
I can also add the html but there seems to be no problem with that. As mentioned this php form was working fine for years then stopped. I've heard that the Google recaptcha uses a smaller string now?
We are receiving empty emails from our site contact form, and we don't know why.
Our form is very simple and the validation is also not so advanced, but enough for our purpose.
We have 5 fields:
Name (text input)
mail (text input)
phone (text input)
reason (select, 3 values)
message (textarea)
Then with JS we validate all fields. Finally, with submit, we send the mail with php mail function:
<?php
$name = $_POST['name'];
$mail= $_POST['mail'];
$phone= $_POST['phone'];
$reason= $_POST['reason'];
$message= $_POST['message'];
$header = 'From: ' . $mail. " \r\n";
$header .= "X-Mailer: PHP/" . phpversion() . " \r\n";
$header .= "Mime-Version: 1.0 \r\n";
$header .= "Content-Type: text/plain";
$msg.= "Name: " . $name . " \r\n";
$msg.= "Mail: " . $mail. " \r\n";
$msg.= "Phone: " . $phone. " \r\n";
$msg.= "Reason: " . $reason. " \r\n";
$msg.= "Message: " . $message. " \r\n";
$for= "ourmail#ourserver.com";
$as= "Contact form";
mail($for, $as, $msg, $header);
?>
In general, works OK. But sometimes we receive an email with ALL (including reason... which is a select!) fields empty. Something like:
Subject: Contact form
Name:
Mail:
Phone:
Reason:
Message:
How can this be?
Asumming you are sending the data through POST:
Have you considered that the action url may be directly accesed from the browser without any kind of POST data?. That would make all the "fields" empty.
If that's the case You can try a server validation in several ways:
Add a referer validation so that the url may only be accesed from the form url. Your can use $_SERVER['HTTP_REFERER'] for that purpose but keep in mind that may not always be available.
Find out if the data is sent. You can use isset for that purpose like in if(isset($_POST['name']) && isset($_POST['email']) in order to add a server validation. You can always do this $name=isset($_POST['name']) ? $_POST['name'] : 'no_name_entered' and have the email sent anyway.
Add a hidden input like "contacting" within your form and check if it's set server side: if(!isset($_POST['contacting'])) die('ERROR: Form not sent');
Anyway, keep in mind that most data you recieve can be tainted, even the ones sent through a select field.
Create server side validation using php script.
Demostration
if($_POST['Name']=="") {
$err = "Enter the name";
}
if($_POST['mail']=="") {
.......
}
......
if($err=="") {
//mail function
}
else {
echo $err;
}
Ive got a contact form that isnt sending but is outputting that the message is sent? Can anybody see a problem?
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "myemail#email.co.uk";
//begin of HTML message
$message = "
From : $name,
Email: $email,
Subject: $subject,
Message: $message ";
//end of message
// To send the HTML mail we need to set the Content-type header.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Website Enquiry";
if (isset($_POST['name'])) {
// now lets send the email.
mail($to, $subject, $message, $headers);
header('Location: ' . $_SERVER['HTTP_REFERER'] . '?e=Thankyou, we will be in touch shortly.');
} else {
header('Location: ' . $_SERVER['HTTP_REFERER'] . '?e=There was an error sending your message, Please try again.');
}
?>
The "From" header should have a syntactically correct email address. You also need to check the return value of the "mail" function.
$header .= "From: Website Enquiry <enquiry#website.com>";
PS: Please improve your code formatting.
Try to enter an email at From: in $headers.
Like $headers .= "From: youremail#provider.com" or
$headers .= "From: Website Enquiry <youremail#provider.com>"
And you should change it to
if(mail(...)) {
//success
}
else {
//email failed
}
I'm trying to solve this puzzle here. The contact form on this page (http://www.b3studios.co.uk/) uses AJAX and PHP. I copied all the code and trying to make it work (reverse engineering PHP). I'm not sure how the PHP file should look like to handle the data given by AJAX. I tried the following PHP file:
<?php
$sender = $email;
$receiver = “test#email.com”;
$email_body = “Name: $name \nEmail: $email \nMessage: $message”;
if( mail( $receiver, $subject, $email_body, “From: $sender\r\n” .
“Reply-To: $sender \r\n” . “X-Mailer: PHP/” . phpversion()) )
{
echo “Success! Your message has been sent. Thank You.”;
}
else
{
echo “Your message cannot be sent.”;
}
?>
After pressing "Send Message" it gets stuck at sending message....
Any suggestions to troubleshoot would be greatly appreciated.
I am pretty sure you copied the code with those ugly double quotes “ (MSWord quotes), you should chage to "
try
<?php
$sender = $email;
$receiver = "test#email.com";
$email_body = "Name: $name \nEmail: $email \nMessage: $message";
if( mail( $receiver, $subject, $email_body, "From: $sender\r\n" . "Reply-To: $sender \r\n" . "X-Mailer: PHP/" . phpversion()) )
{
echo "Success! Your message has been sent. Thank You.";
} else {
echo "Your message cannot be sent.";
}
?>
i wonder what i'm doing wrong? I'm working on a ftp-upload with php. if files are uploaded successfully i want to get a confirmation email. just a simple email.
if my connection to the FTP Server was a success i'm calling the sendmail() function! it's not working!
function sendmail() {
$EmailFrom = "do-not-reply#mypage.com";
$EmailTo = "myemail#mypage.com";
$Subject = "File uploaded to your FTP Server";
$Body = "Howdy, files have just been transferred to your Server.";
// Email Headers with UTF-8 encoding
$email_header = "From: " . $EmailFrom . "\r\n";
$email_header .= "Content-Type: text/plain; charset=UTF-8\r\n";
$email_header .= "Reply-To: " . $EmailFrom . " \r\n";
$success = mail($EmailTo, $Subject, $Body, $email_header);
if ($success){
print "success with EMAIL";
}
else{
print "error with EMAIL";
}
}
any idea what i'm doing wrong here? does the $EmailFrom value have to be an actual Emailaddress? It's just not working. Neither success nor error gets printed out. And nothing of my code AFTER the function-call gets executed.
thank you for your help
Well if you want to sendmail() when ftp upload is success do something like this
$status = move_uploaded_file($src,$destination);
if($status) { sendmail(); }
What this will do this, first $status will hold the boolean value whether the upload is successful, and if it suceeds then it will call your sendmail() function