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.";
}
?>
Related
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
my form is working as intended but for some reason the email will only send to one of my email accounts and not the other I am putting the right email in the email field so that isn't the issue however I can't seem to see where I am going wrong I assume it's because I'm using $email to grab the email address to where the 2nd email is suppose to go...here is my php where am I going wrong?
<?php
$from = 'Pixel Wars - Press Inquiry';
$to = "my-email#gmail.com, $email";
$subject = 'Press Inquiry from Pixelwars.com';
function errorHandler ($message) {
die(json_encode(array(
'type' => 'error',
'response' => $message
)));
}
function successHandler ($message) {
die(json_encode(array(
'type' => 'success',
'response' => $message
)));
}
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = "Name: $name\r\n Email: $email\r\n\r\n Message:\r\n $message";
$pattern = '/[\r\n]|Content-Type:|Bcc:|Cc:/i';
if (preg_match($pattern, $name) || preg_match($pattern, $email) || preg_match($pattern, $message)) {
errorHandler('Header injection detected.');
}
// Check if name has been entered
if (!$_POST['name']) {
errorHandler('Please enter your name.');
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
errorHandler('Please enter a valid email address.');
}
// Check if message has been entered
if (!$_POST['message']) {
errorHandler('Please enter your message.');
}
// prepare headers
$headers = 'MIME-Version: 1.1' . PHP_EOL;
$headers .= 'Content-type: text/plain; charset=utf-8' . PHP_EOL;
$headers .= "From: $name <$email>" . PHP_EOL;
$headers .= "Return-Path: $to" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "X-Mailer: PHP/". phpversion() . PHP_EOL;
// send the email
$result = #mail($to, $subject, $body . "\r\n\n" .'------------------ '. "\r\n\n" .'Hello '.$name.' we will contact you as soon as possible about your query.' ."\n". 'Dont forget to keep visiting www.pixelwars.com for more updates and awesome content.' ."\n". 'We will email you back on the provided email below, thank you and have a nice day.' . "\r\n\n" .'-- '.$email, $headers);
if ($result) {
successHandler('Thank You! we will be in touch');
} else {
errorHandler('Sorry there was an error sending your message.');
}
} else {
errorHandler('Allowed only XMLHttpRequest.');
}
?>
Thank you in advance if anyone can crack it
You don't have $email assigned when you are defining $to so your second address is not set.
Demo: https://3v4l.org/QIIJu
Solution, move the $to assignment to later in the script. Also use error reporting, this would have thrown an undefined variable notice.
e.g.
<?php
$from = 'Pixel Wars - Press Inquiry';
$subject = 'Press Inquiry from Pixelwars.com';
....
$to = "my-email#gmail.com, $email";
$result = #mail($to, $subject, $body ....
because at this point the $email is defined. Also don't use error suppression, that is just hiding useful information. If you don't want it displayed hide the error displaying but still log them.
You need to add the multiple email address in $to
$to = "address#one.com, address#two.com, address#three.com"
Needs to be a comma delimited list of email adrresses.
mail($email_to, $email_subject, $thankyou);
Thanks
I want to send email from contact page of the my site. I google and it told to use PHP scripts as my domain is LINUX based so I cannot use ASP. I tried couple of them but not able to send. How ever I have used #ECHO to display message, which I got but not the email. Here the codes that I tried:
<?php
$userid='to.useri#example.com';
$subject='New Requirement';
$Name=$_POST['Name'];
$Email=$_POST['Email'];
$Phone=$_POST['Phone'];
$Message=$_POST['Message'];
$body= <<<EOD;
<br><hr><br>
Name: $Name <br>
Email: $Email <br>
Phone: $From <br>
Message: $Message
EOD;
$headers='From: $Email';
mail($userid,$subject,$body,$headers);
echo "Message send!!!";
?>
And I also tried :
<?php
$to = 'to.user#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: from.user#example.com' . "\r\n" .
'Reply-To: from.user#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers )) {
ECHO 'Message send successfully';
}
else {
ECHO 'Please try again, Message could not be sent!';
}
?>
Can any one tell me what am I missing here.
Change your if Condition with the below mentioned code, hope it works:-
$mail=mail($to, "Subject: $subject",$message );
if($mail){
echo "Thank you for using our mail form";
}else{
echo "Mail sending failed.";
}
try this code
$senderName="John";
$senderEmail= "test#domain.com";
$recipient = "recipient#domain.com";
$subject ="testmail";
$message="test message";
$headers = "From: " . $senderName . " <" . $senderEmail . ">";
$success = mail($recipient, $subject, $message, $headers );
Here's the script:
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$recipient = "me#christianselig.com";
$subject = "Message From Website";
$body = "[From: " . $name . "]\r\n\r\n" . $message;
$headers = "From: " . $email . "\r\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
$success = mail($recipient, $subject, $body, $headers);
echo $success;
?>
On this page: christianselig.com/contact.html
My message will send without any linebreaks. I have it set to separate the [From: xxx] section from the message with a double linebreak, so it should look like this:
[From: John]
Hey.
But it doesn't.
If I make the message multiple lines long, it also concatenates them on one line. How do I prevent this behaviour? My code used to allow it, but I broke it somehow...
Because you're sending it with an html formatting. Try:
$body = nl2br("[From: " . $name . "]\r\n\r\n" . $message);
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
}