not receiving emails from php - php

I know I'm being sent a status of '1' from this process file as my JavaScript resulting is functioning. Problem is that I'm not getting the email.
<?php
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['comment'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course,
//you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$comment) $errors[count($errors)] = 'Please enter your comment.';
//if the errors array is empty, send the mail
if (!$errors) {
//recipient - change this to your name and email
$to = 'myemail#gmail.com';
//sender
$from = $name . ' <' . $email . '>';
//subject and the html message
$subject = 'Comment from ' . $name;
$message = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table>
<tr><td>Name</td><td>' . $name . '</td></tr>
<tr><td>Email</td><td>' . $email . '</td></tr>
<tr><td>Comment</td><td>' . nl2br($comment) . '</td></tr>
</table>
</body>
</html>';
//send the mail
$result = sendmail($to, $subject, $message, $from);
//if POST was used, display the message straight away
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.';
else echo 'Sorry, unexpected error. Please try again later';
//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
echo $result;
}
//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
echo 'Back';
exit;
}
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
?>

You mentioned you are using GoDaddy. GoDaddy requires you set the sender address legitimately to match the domain of the site it is sending from or use SMTP with Authentication.
There is a huge gaping hole with this method of sending email. Spammers can easily override the From: header by inserting additional recipients.
I'm not sure how mail centric your application plans to be, but I would recommend using a package like PHPMailer or PEAR::Mail as it takes care of email handling for you at a much higher level. This let's you focus on more important parts of your application. The built-in PHP mail() feature is very limited in its abilities and as you try to extend your mail capabilities you'll run into many road blocks that the base mail() function just cannot handle without a lot of additional logic on your behalf (attachments, MIME-types, etc come to mind).

when testing mails you can test it directly to your server, php mail has a function that already runs on it. if you test it on xampp locally it will not send , unless you have set the php mailer in localhost. but for me its better to test it on server than in localhost.

Related

Fasthosts shared platform PHP mail from web page issue -F

I am trying to send email from a web page hosted on a shared platform over at fasthosts. I cannot for the life of me get this to work, I had a much more extensive script which checked the validity of email etc, but now I've been reduced to using the basic example from fasthosts and it is still not working.
Please could someone take a look and let me now where I am going wrong...
<?php
// You only need to modify the following two lines of code to customise your form to mail script.
$email_to = "contact#mywebsite.co.uk"; // Specify the email address you want to send the mail to.
$email_subject = "Feedback from website"; // Set the subject of your email.
// This is the important ini_set command which sets the sendmail_from address, without this the email won't send.
ini_set("contact#mywebsite", $email_from);
// Get the details the user entered into the form
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
// Validate the email address entered by the user
if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
// Invalid email address
die("The email address entered is invalid.");
}
// The code below creates the email headers, so the email appears to be from the email address filled out in the previous form.
// NOTE: The \r\n is the code to use a new line.
$headers = "From: " . $email_from . "\r\n";
$headers .= "Reply-To: " . $email_from . "\r\n"; // (You can change the reply email address here if you want to.)
// Now we can construct the email body which will contain the name and message entered by the user
$message = "Name: ". $name . "\r\nEmail: " . $email . "\r\nMessage: " . $message ;
// Now we can send the mail we've constructed using the mail() function.
// NOTE: You must use the "-f" parameter on Fasthosts' system, without this the email won't send.
$sent = mail($email_to, $email_subject, $message, $headers, "-f" . $email_from);
// If the mail() function above successfully sent the mail, $sent will be true.
if($sent) {
$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$name .' Thank you for contacting us.'));
die($output);
} else {
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
}
?>

Why is PHP's mail function failing to deliver intermittently? [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I have an event that fires every time a user successfully submits a form on my website. I can track the exact time that the event fired from my Google Analytics account. The time that the event fired, mirrors the delivery time of the email delivered to my inbox (yahoo mail).
However, I've noticed many times that, when the same event fires, no email is delivered to the the same email address. It never reaches its destination.
How can I fix this? What is the root of this problem?
Here is my php code:
<?php
if($_POST)
{
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die();
}
//check $_POST vars are set, exit if any missing
if(!isset($_POST["userName"]) || !isset($_POST["userLastName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userArrival"]) || !isset($_POST["userNumberPeople"])
|| !isset($_POST["userPickupLocation"]) || !isset($_POST["userRequest"]) || !isset($_POST["userTourSelection"]))
{
die();
}
//Sanitize input data using PHP filter_var().
$user_Name = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
$user_LastName = filter_var($_POST["userLastName"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
$user_TourSelection = filter_var($_POST["userTourSelection"], FILTER_SANITIZE_STRING);
$user_Arrival = filter_var($_POST["userArrival"], FILTER_SANITIZE_STRING);
$user_NumberPeople = filter_var($_POST["userNumberPeople"], FILTER_SANITIZE_STRING);
$user_PickupLocation = filter_var($_POST["userPickupLocation"], FILTER_SANITIZE_STRING);
$user_Request = filter_var($_POST["userRequest"], FILTER_SANITIZE_STRING);
$to_Email = "something#yahoo.com"; //Replace with recipient email address
$subject = 'Tour request: '.$user_TourSelection.' '; //Subject line for emails
//additional php validation
if(strlen($user_Name)<2) // If length is less than 4 it will throw an HTTP error.
{
header('HTTP/1.1 500 Name is too short or empty!');
exit();
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
{
header('HTTP/1.1 500 Please enter a valid email address!');
exit();
}
if(strlen($user_Request)<5) //check emtpy message
{
header('HTTP/1.1 500 Please explain in a few words how you would like us to assist you.');
exit();
}
// message
$message = '<strong>Name:</strong> '.$user_Name.' '.$user_LastName.' <br><br>
<strong>Date of Arrival:</strong> '.$user_Arrival.' <br><br>
<strong>Tour:</strong> '.$user_TourSelection.' <br><br>
<strong>Number of people:</strong> '.$user_NumberPeople.' <br><br>
<strong>Pick-Up Location:</strong> '.$user_PickupLocation.' <br><br>
<strong>Request:</strong> '.$user_Request.'
';
//proceed with PHP email.
$headers = 'From: '.$user_Email.'' . "\r\n" .
$headers .='Reply-To: '.$user_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();
#$sentMail = mail($to_Email, $subject, $message, $headers);
if(!$sentMail)
{
header('HTTP/1.1 500 Our server is under maintenance!<br> If this error persists please contact us directly: <strong>something#yahoo.com</strong>');
exit();
}else{
echo 'Congratulations '.$user_Name .'! ';
echo 'We have received your request and we will respond as soon as possible.';
}
}
?>
mail()'s delivery function ends when it hands off your mail to the SMTP server. Its sole responsibility is the real-world equivalent of taking your envelope and dropping it into the mailbox on the corner. The rest of the postal service (emptying that box, running it through processing centers, flying it to the recipient's country/city, etc...) is completely outside of mail()'s scope. As long as the envelope drops into the mailbox, mail() will return true and pretend it was delivered.
So... check your SMTP server's logs to see what really happened to the mail. Maybe it got marked as spam by the receiver and bounced. Maybe it's stuck in a queue somewhere, etc... Only the logs will tell you this - anything you can see/do in PHP is useless, because PHP and mail() only do maybe 1% of the email sending/delivery process, and something's wrong in that other 99%.

how to make email appear in inbox and not junk email (php)

I have written a php mail function to allow a user on my website to fill in a form and send the form to my email. as the question says the email is working once the user send the form however it only appear in my junk email folder instead, i am not a php developer but after doing some research i have noticed a lot people mentione about PHPMailer which i never heard of or used before.
i would much appreciate with a bit oh help.
$to="myemail.com";
//Errors
$nameError="";
$emailError="";
$errMsg="";
$errors="";//counting errors
$name="";
$email="";
$message="";
if(isset($_POST['send'])){
if(empty($_POST['yourname'])){ //name field empty
$nameError="Please enter your name";
$errors++; // increament errors
}else{
$name= UserInput($_POST['yourname']);
if(!preg_match("/^[a-zA-Z ]*$/", $name)){
$nameError="Only letters and white space accepted";
$errors++;
}
}
if(empty($_POST['email'])){
$emailError="Enter email";
$errors++;
}else{
$email = UserInput($_POST['email']);
if(!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email)){
$emailError="Invalid Email";
$errors++;
}
}
if(empty($_POST['msg'])){
$errMsg="Enter message";
$errors++;
}else{
$message=UserInput($_POST['msg']);
}
if($errors <=0){//No errors lets setup our email and send it
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <' . $email . '>' . "\r\n";
$text = "<p>New Message from $name </p>";
$text .= "<p>Name : $name</p>";
$text .= "<p>Email : $email</p>";
$text .= "<p>Message : $message</p>";
mail($to, "Website Contact", $text, $headers);
$success="Thank your message was submitted";
$_POST= array(); //clearing inputs fields after success
}
}
//Filter user input
function UserInput($data){
$data = trim($data);
$data = stripcslashes($data);
$data = htmlspecialchars($data);
return $data;
}
Your using the email from their posted variable in your header. When your email server receives this it's going to look like it is spoofed because it is. Your site isn't going to be one of the mail servers setup for that domain.
When setting up MX and DNS records for email you use SPF or key signing to prove who sent the message and that it came from a trusted mail server for that domain. You may want to change the from to be an email and domain you control. You are getting their email in the body anyway.
Worst case if you don't have control over SPF records you could at least mark the from email, assuming it is something you control, as always trusted so it wouldn't go into your junk mail.
$headers .= 'From: <' . $to . '>' . "\r\n";

JQuery Ajax Form Submission is remaining static and I can't see why!

Might be easier to look at this fiddle: http://jsfiddle.net/pkAGz/ and the process.php code is shown below:
<?php
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course,
//you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter a name.';
//if the errors array is empty, send the mail
if (!$errors) {
$name = $name[array_rand($name)];
$to = '$name <$name email adress if set>';
//sender
$from = $name . ' <' . $email . '>';
//subject and the html message
$subject = 'Comment from ' . $name;
$message = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table>
<tr><td>Name</td><td>' . $name . '</td></tr>
<tr><td>Email</td><td>' . $email . '</td></tr>
</table>
</body>
</html>';
//send the mail
$result = sendmail($to, $subject, $message, $from);
//echo "\n\n$name has been nominated to make the tea!\n\n";
//echo "\n\nThey will also be notified by e-mail if you entered their address.\n\n";
//if POST was used, display the message straight away
if ($_POST) {
if ($result) echo "\n\n$name has been nominated to make the tea!\n\nThey will also be notified by e-mail if you entered their address.\n\n";
else echo 'Sorry, unexpected error. Please try again later';
//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
echo $result;
}
//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
echo 'Back';
exit;
}
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
?>
Once the AJAX request is working the next step is simply work around the code to send an e-mail to the person chosen if their e-mail address was entered - a bit stuck on how to do that as I want the e-mail field to remain optional.
Then obviously, I want to return the name of the person that was picked at random and that will be it!
Thanks,
Martin
Use Firebug for Firefox, or watch the console in Chrome/Safari. So you would have seen that you have a javascript error:
Uncaught ReferenceError: comment is not defined
So the script:
//cancel the submit button default behaviours
return false;
isn't executed and the form is posted normally.

Problem with sending an email via php script?

I've had something strange happen lately with php script I use to send emails from an online contact form and just wondered if any one could shed a little light on the issue.
I've had a php script which I use on multiple websites and it has always worked fine, but for some strange reason, I tried using in on one site and it just wasn't working.
I tried fiddleing with it and eventually realised that it was something to do with the following section of code:
this is the original section of code that usually works fine, but wasn't working for some reason:
$to = 'My Name <info#mydomain.com>';
I then removed the name bit, so that the code looked like this:
$to = 'info#mydomain.com';
and now it sends the email through ok.
As I say, the top code usually works fine, so any ideas why this time I had to alter the code to get it to work?
Any possible explanations would be great :o)
Here's the full code:
<?php
require("is_email.php"); // email validation function
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ?$_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$telephone = ($_GET['telephone']) ?$_GET['telephone'] : $_POST['telephone'];
$address = ($_GET['address']) ?$_GET['address'] : $_POST['address'];
$enquiry = ($_GET['enquiry']) ?$_GET['enquiry'] : $_POST['enquiry'];
$calculation = ($_GET['calculation']) ?$_GET['calculation'] : $_POST['calculation'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Server side validation for POST data
if (!$name) $errors[count($errors)] = 'Please click back and enter your name.';
if (!$email) $errors[count($errors)] = 'Please click back and enter your email.';
else if (!is_email($email)) $errors[count($errors)] = 'Please click back as you may have entered an invalid email address.';
if (!$telephone) $errors[count($errors)] = 'Please click back and enter your telephone number.';
if (!$address) $errors[count($errors)] = 'Please click back and enter your address.';
if (!$enquiry) $errors[count($errors)] = 'Please click back and enter your enquiry.';
if ($calculation != '14') $errors[count($errors)] = 'Please click back and check you have correctly answered the simple calculation (in number format).';
//if the errors array is empty, send the mail
if (!$errors) {
//recipient - change this to your name and email
$to = 'info#mydomain.com';
//sender
$from = $name . ' <' . $email . '>';
//subject and the html message
$subject = 'Website Enquiry: ' . $name;
$email_body = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table cellpadding="5" style="color:#757575;">
<tr><td style="color:#3b5998;">Name: </td><td>' . $name . '</td></tr>
<tr><td style="color:#3b5998;">Email: </td><td>' . $email . '</td></tr>
<tr><td style="color:#3b5998;">Telephone: </td><td>' . $telephone . '</td></tr>
<tr valign="top"><td style="color:#3b5998;">Address: </td><td>' . nl2br($address) . '</td></tr>
<tr valign="top"><td style="color:#3b5998;">Enquiry: </td><td>' . nl2br($enquiry) . '</td></tr>
</table>
</body>
</html>';
//send the mail
$result = sendmail($to, $subject, $email_body, $from);
//if POST was used, display the message straight away
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.<br /><br />OK';
else echo 'Sorry, unexpected error. Please try again later';
//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
echo $result;
}
//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br />';
exit;
}
//Simple mail function with HTML header
function sendmail($to, $subject, $email_body, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$email_body,$headers);
if ($result) return 1;
else return 0;
}
?>
Hosts can vary on their requirements for sendmail and depending on what sendmail software they are using. You may take this up with your hosting company and see if there are any caveats to making it work or if they know a better format.
Use this:
$mailFrom = '"My Name" <info#mydomain.com>';
the FROM label is double quoted and a space and <email>
I edited my initial question to explain how I overcame the problem.

Categories