Solved

php mail() function not working to send to Hotmail

Posted on 2012-03-22
14
825 Views
Last Modified: 2012-06-27
I have a php script that will successfully send email to other email servers, but not to Hotmail. It doesn't even trap it as spam - just does not receive it.

In my reading, I found reference to the fact that Hotmail will not receive a message generated by the php mail() function. I find that impossible to believe, but it shows others are seeing a problem (and what are the odds they are trying from the same hosting company.)

I think it might be a problem with headers - header references to the sender's email address not being identical.

My website is hosted at WebHostingHub.com (maybe they are blacklisted at Hotmail?)

Supposedly (and this makes sense), the spam filters look for messages that obviously appear spoofed. The way WebHostingHub sends out email, it is using my main email account, and I can't change that behavior - and don't want to use that (admin) email address at all in the headers. I did create another email account, and put that in the "from" and "reply-to" headers, but from the mailserver logs the hosting company tech was reading, I think it still has the main/admin email account listed in the path (even though I have the "from" email address that I want, listed in the visible header.)

Any help on this will be greatly appreciated!

Dennis
0
Comment
Question by:dtleahy
  • 8
  • 5
14 Comments
 

Author Comment

by:dtleahy
ID: 37754010
Here is very simplified code, that will not get a message sent to my Hotmail account:

<?php

$to = "aRealEmailAddress@hotmail.com";
$subject = " Test Subject";
$message = "Test Messsage";
$headers = 'From: activation@ResetButton2012.org' . "\r\n" .
    'Reply-To: activation@ResetButton2012.org' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();


 if (mail($to, $subject, $message, $headers)) {
   echo("<p>Message successfully sent!</p>");
  } else {
   echo("<p>Message delivery failed...</p>");
  }
  
?>

Open in new window

0
 

Author Comment

by:dtleahy
ID: 37755058
Update:
I am looking into PEAR right now - perhaps to circumvent the problem instead of beating my head against the wall.

Dennis
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 375 total points
ID: 37756035
This script works to send from my Godaddy account to my Hotmail and Yahoo accounts.  Hotmail did mark it as 'suspicious'.  One of the problems with Hotmail is that it bases it's filtering on the 'reputation' of your email address.  I've had my accounts for many years now and don't have problems anymore.  http://mail.live.com/mail/troubleshooting.aspx

<?php
error_reporting(E_ALL);
ini_set('display_errors','On');

# some settings of POST vars
if (!isset($_POST['send']))  $send = ''; else $send = $_POST['send'];
if (!isset($_POST['toText'])) $toText = ''; else $toText = $_POST['toText'];
if (!isset($_POST['ccText'])) $ccText = ''; else $ccText = $_POST['ccText'];
if (!isset($_POST['subjectText'])) $subjectText = ''; else $subjectText = $_POST['subjectText'];
if (!isset($_POST['msgText'])) $msgText = ''; else $msgText = $_POST['msgText'];
if (!isset($_POST['ccText'])) $ccText = ''; else $ccText = $_POST['ccText'];
if (!isset($_POST['bccText'])) $bccText = ''; else $bccText = $_POST['bccText'];
if (!isset($_POST['nameText'])) $nameText = ''; else $nameText = $_POST['nameText'];
if (!isset($_POST['fromText'])) $fromText = ''; else $fromText = $_POST['fromText'];

if ($send == "") {
    $title="Test Email Page";
    $announce="---";
}
else {
	if($fromText === "") die("No name!");
  $toText="youremail@yourdomain.com";
	$title="Test Email Page";
  $announce="Your Message has been Sent!";
	$header = "From: ".$fromText."\r\n";
//	$header .= "Cc: ".$ccText."\n";
	$header .= "Reply-To : ".$fromText."\r\n";
	$header .= "Return-Path : ".$fromText."\r\n";
	$header .= "X-Mailer: PHP\r\n";
	$header .= "MIME-Version: 1.0\r\n";
	$header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
//	ini_set(sendmail_from,$fromText);  
	mail($toText, $subjectText, $msgText, $header, '-f'.$fromText);
//	ini_restore(sendmail_from);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title><?php echo($title)?></title>
<style type="text/css">
<!-- 
A:link { color: #999999; }
A:visited { color: #999999; }
A:hover {color: #0099ff;}
-->
</style>
<script type="text/javascript">
<!--
function check()
{
var at=document.getElementById("fromText").value.indexOf("@");
var eml=document.getElementById("fromText").value;
var nam=document.getElementById("nameText").value;
var alerttxt="";
var submitOK="true";

if (eml.length < 5 || at == -1)
    {
    alerttxt=alerttxt+"Please enter a valid e-mail address!\r\n";
    submitOK="false"
    //return false;
    }
if (nam.length < 3)
    {
    alerttxt=alerttxt+"Please enter your name.\r\n";
    submitOK="false"
    //return false;
    }
if (submitOK=="false")
    {
    alert(alerttxt);
    return false;
    }

}
// -->
</script>
</head>

<body bgcolor="#ddeedd">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" summary="" width="580">
<tr><td align="center">

<?php
if ($send != "") {
   	echo ("To: ".$toText."<br>\r\nSubject: ".$subjectText."<br>\r\n".$msgText."<br>\r\n".$header);
		}
?>

<p><b><font color="#000000" size="5">Test Email</font></b></p>
<font size="4" color="#000000">

<form method="POST" action="Email.php" onsubmit="return check();">
    <p><font size="3"><b>Name: <input type="text" name="nameText" id="nameText" size="46"></b></font></p>
    <p><font size="3"><b>Email: <input type="text" name="fromText" id="fromText" size="46"></b></font></p>
    <input type="hidden" name="subjectText" value="Web Mail">
    <p><font face="Arial" size="3"><b>Message Text:</b></font></p>
    <p><font face="Arial" size="3"><b><textarea rows="6" name="msgText" cols="60"></textarea></b></font></p>
    <p><font size="3"><b><input type="submit" value="Send" name="send" style="font-family: Arial; font-size: 12pt; font-weight: bold"></b></font></p>
    <input type="hidden" name="state" value="1">
  </form>
  <b><font face="Arial" size="4" color="#e00000"><?php echo($announce)?></font></b><br><br>

</font>
</td></tr>
</table> 
</div>


</body>
</html>

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 50

Assisted Solution

by:Steve Bink
Steve Bink earned 125 total points
ID: 37757885
I've never had problems with mail not getting to Hotmail, though I have seen it marked as spam or suspicious, and usually for a specific reason that I could control.  Here are some suggestions for you:

1) Use a third-party mailer class (like PHPMailer or HTMLMimeMail) to construct the message.  It may just be that you're missing something from the specification that Hotmail does not like.

2) Check your mail logs (if accessible) to find out how Hotmail's servers are responding when the message delivery is attempted.  They may yield some clues.

3) Use proper addressing.  Spoofing "From" addresses, even with legitimate reason, is one of the huge red flags mail providers look for.  Also, make sure your MX and SPF records are configured properly.
0
 

Author Comment

by:dtleahy
ID: 37759127
Thank you all for the suggestions. I'm looking at all these and some other ways...

Dennis
0
 

Author Comment

by:dtleahy
ID: 37762791
(The way I have it set up) PEAR does not work.

Error:
"Validation failed for: activation@mydomain.org "
(the real email account was used in testing, of course)

(here is the code I tried, but all data was real when I tried)
<?php
require_once "Mail.php";

$from = "activation@mydomain.org <activation@mydomain.org>"; 
$to = "Human Being <real@thedomain.com>";
$subject = "Test Subject Joining";
$body = "Thank you for joining."; 
$host = "mail.mydomain.org"; 
$username = "activation@mydomain.org"; 
$password = "EmailAccountPW"; 
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);  		
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username,'password' => $password));
 
$mail = $smtp->send($to, $headers, $body);
 
if (PEAR::isError($mail)) {
	echo("<p>" . $mail->getMessage() . "</p>");
	} 
else 
	{   					
##Send activation Email:
	echo "An email has been sent to ".$to." with an activation key. Please check your mail to complete the authentication.";		
	}
					
?>

Open in new window


A support person at my web hosting provider said that he sees the messages in the email server log files, so the account and password are are good. But the support person says he also sees that they are not being successfully sent. Something is still missing, or not set up properly...

...can anyone help me either figure this out quickly, or recommend a "bulletproof" way to send emails programatically that will get through. This is not advertising or spam -these are validation/activation emails, and obviously it is critical that the email goes through every time.

Thanks!

Dennis
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 375 total points
ID: 37763132
There is no truly "bulletproof" way to send emails.  To improve your chances of getting thru, the email address you are sending from needs to be a recognized address on a mail server that can be looked up and have an SPF record.  We usually recommend checking the status of your mail server here:  http://www.mxtoolbox.com/  

And all the major mail services have pages to help you send to them.

Microsoft Hotmail / Live Mail:  http://mail.live.com/mail/troubleshooting.aspx

Yahoo: http://help.yahoo.com/kb/index?page=topics&y=PROD_MAIL_ML&locale=en_US&topics=TOP_POST

Gmail: https://support.google.com/mail/bin/answer.py?hl=en&answer=81126
0
 

Author Comment

by:dtleahy
ID: 37763557
Great answer, Dave. Obviously, not the one I wanted to hear, but a bucket of cold water in the face is usually wiser than a carrot on a stick. :~)

If it was not so damn hard for a php beginner to read the open source code that phpbb forum uses, I would dive in there and find out what they use. Generally speaking (having set up several forums using the free phpbb forum application), the activation emails always, always go through. That is written in php.

I did dig through a lot of the Microsoft troubleshooting, but as is typical with those guys, you need a day and a half to dedicate to all the myriad possibilities. Nothing leaps out at me, but then, I'm too "green" in this programmatic mail functionality to see the forest or the trees. Ya gotta love this form from the boys and girls in Redmond (https://support.msn.com/eform.aspx?productKey=edfsmsbl&ct=eformts&wa=wsignin1.0&st=1&wfxredirect=1)

I think I am going to dig through the phpbb code...

Dennis
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 375 total points
ID: 37763797
Like I said before, it's not the PHP code but your email addresses reputation that counts with Hotmail.  Most new websites I have set up are unable to send email from a new email address to Hotmail/Live Mail at first.  The big 3 email services are more concerned with blocking spam than with delivering email and Hotmail is the most difficult.

Since you have a Hotmail account, send some emails from that account to the account you're trying to use so at least Hotmail has some record of it being an acceptable email address.
0
 

Author Comment

by:dtleahy
ID: 37763894
Good thinkin', Dave. (about sending out mail, priming the pump) I did hear what you were saying about reputation being a factor, bit I'll treat it as the main factor.

I suppose I'll have to create a few gmail accounts and Yahoo accounts too. (I have a Hotmail and a Live.)

Dennis

p.s. I could never work on a group project like phpbb. Great forum software, but the code drives me crazy to look at.
0
 

Author Closing Comment

by:dtleahy
ID: 37763900
Thanks very much for the help!
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37764345
You're welcome.  The code for things like Wordpress and Joomla drive me nuts also.  And phpMyAdmin.
0
 

Author Comment

by:dtleahy
ID: 37769752
DaveBaldwin said:
Since you have a Hotmail account, send some emails from that account to the account you're trying to use so at least Hotmail has some record of it being an acceptable email address.

Just thought I'd mention that after just a few successful emails sent from my new domain to my Hotmail account, now programmatic emails from my domain are going through to Hotmail.

So, it didn't take nearly as long as expected. Thanks again, Dave.

Dennis
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37770248
Good, glad you got it working.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now