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
}
Related
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
Contact form on site use script below. It send 2 messages - to me and to the person filling in the form. In second mail I have problem with characters like "ąźćęś"....I would like to use utf-8. I set it in header, but it doesn't work. I need to use in message body html tags also.
What wrong in the code?
<?php
// Check for empty fields
if(empty($_POST['email']) ||
empty($_POST['phone']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$email_address = $_POST['email'];
$phone = $_POST['phone'];
// Create the email and send the message
$to = 'me#test.com';
$email_subject = "Mail to you";
$email_body = "Hello\n\n"."Test\n\nEmail: $email_address\n\nPhone: $phone";
$headers = "From: test#test.com\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
$to2 = "$email_address";
$email_subject2 = "=?UTF-8?B?".base64_encode("Thanks for you message")."?=";
$email_body2 = "Test mail ążźćęśó\n\nBest regards\nTester";
$headers2 .= "Content-Type: text/html; charset=UTF-8";
$headers2 = 'From: Tester <test#test.com>' . "\r\n";
mail($to2,$email_subject2,$email_body2,$headers2);
return true;
?>
I experiencing a problem with a php contact form that I normal use on our products sites.
<?php
$to = "martin#puritech.co.za";
$subject = "Reverse Osmosis";
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['tel'];
$mail = $_POST['message'];
$headers = "From: $name \r\n";
$headers .= "Email: $email \r\n";
$headers .= "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html charset=iso-8859-1 \r\n";
$message = "<html><body style='font-family: Arial;'>";
$message .= "<h1>Hello Guys! You have a request from ".$name."</h1>";
$message .= "<p>".$mail."</p>";
$message .= "<p><span style='font-weight:bold;'>From:</span> ".$name."</p>";
$message .= "<span style='font-weight:bold;'>Email:</span> ".$email."</p>";
$message .= "<span style='font-weight:bold;'>Contact Number:</span> ".$number."</p>";
$message .= "</body></html>";
$send = mail ($to, $subject, $message, $headers);
if ($send==true) {
header('Location: ../index.html');
exit;
}else {?>
<script type="text/javascript">
alert('Please check your details and resend. Thank you.');
window.location = '../index.html';
</script>
<?php
}
?>
When I click on the send button, the mail goes straight to spam and has an html doc attachment.
How do I fix this? really draining me and its starting to annoy me.
I modified the following script. Everything seems to work except the person who submits the form('to2') gets an email with ('message2' - with single space lines) followed by ('message' - with double space lines).
The recipients of ('message') works as it should in that they only get ('message' - with single space lines).
MY OBJECTIVE is for ('to2') only to receive ('message2') without being followed by ('message'). I tried positioning mail('message2') string in different locations of the scripts' logic, but, I keep getting the same results or broken. Any help with logic is appreciated.
Here is the script...
<?php
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=500">
<title>EXAMPLE</title>
</head>
<body>
<?php
if(isset($_POST['Submit'])) {
$ip_address = $_SERVER['REMOTE_ADDR'];
$Subject = 'TEST';
$Subject2 = 'SUCCESS';
$A1 = $_POST['A1'];
$Name = $_POST['Name'];
$Phone = $_POST['Phone'];
$Email = $_POST['Email'];
$message = $_POST['message'];
$message2 = $_POST['message2'];
// Type in your Email address to receive the mail
$to = 'me#example.com';
$to2 = $Name.' <'.$Email.'>';
if($Name == "" or $Phone == "" or $Email == "" ) {
echo 'One or more fields has not been filled out.<br>
Click on your browser back button once and try again.';
}
elseif(!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
echo 'The Email address could not be validated.<br>
Click on your browser back button once and verify your Email address.';
}
else { // All checks passed
$headers = "From: ".$Name." <".$Email.">\r\n";
$headers .= "Bcc: [VENDOREMAILS]\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
$headers2 = "From: Support <me#example.com>\r\n";
$headers2 .= "MIME-Version: 1.0\r\n";
$headers2 .= "Content-Type: text/html; charset=utf-8\r\n";
$message = "
Subject: $Subject<br>
Question: $A1<br>
Name: $Name<br>
Phone: $Phone<br>
Email: $Email
";
$message2 = "
Subject: $Subject2<br>
Question: $A1<br>
Name: $Name<br>
Phone: $Phone<br>
Email: $Email
"
.nl2br($message);
$sendMail = mail($to, $Subject, $message, $headers);
if($sendMail) {
echo "THANK YOU FOR YOUR SUBMISSION";
mail($to2, $Subject2, $message2, $headers2);
}
else {
echo "An error occured and the mail could not be sent.<br>
Please try again";
}
}
}
else {
header("location:example.html");
}
?>
</body>
</html>
Remove the code
.nl2br($message);
from the end of the definition of $message2. I think that should do it.
I have a problem with my mail in php. I code form to send email. I receive email on gmail but I have other mail address and I can't get email on it.
I checked in spam and there is no email also.
Below is my code.
<?php
$emailErr = "";
$endMessage = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["email"])) {
$emailErr = "Proszę uzupełnić pole e-mail";
}
else if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$namesurname = $_REQUEST['name_surname'] ;
$email = $_REQUEST['email'] ;
$number = $_REQUEST['number'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$message = $subject . ": " . $message . " " . $number . " " . $namesurname . " " . $email;
$subject = "=?UTF-8?B?".base64_encode($subject)."?=";
mail("szafor#szafor.pl", "Zamówienie pomiaru",
$message, "From: formularz#szafortest.pl \r\n"."Content-Type: text/plain; charset=UTF-8\r\n");
$endMessage = "Dziękuję za przesłanie wiadomości.";
}
}
?>
One important thing to consider with sending mail is that you should at least have the return path of the message be an email address that is actually hosted on the server that you are sending from.
You can set the From and the Reply-To address as any address, but the return-path should be set to a valid email address hosted on your server. Let's say that you want the "reply" button to send back to "this_email#wherever.com" but the server you are using hosts email for "mydomain.com". Create an email account on your server, "info#mydomain.com" for example.
$recipient = "sendto#email.com";
$subject = "Test email";
$message = "This is the message.";
$headers .= "From: Your Name Here <any_email#wherever.com>\n\r";
$headers .= "Reply-To: Your Name Here <any_email#wherever.com>\n\r";
$headers .= "Return-Path: Your Name Here <info#mydomain.com>\n\r";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .="X-Mailer: PHP/" . phpversion() . "\r\n";
$headers .="MIME-Version: 1.0\r\n";
mail($recipient, $subject, $message, $headers);
I have found that the more valid header information that I provide, the more likely the email will be delivered. Right now these headers always work for me, and I have a scheduling program that is sending email to a hundred different email addresses every day. See if that works better for you.