Forum Moderators: coopster

Message Too Old, No Replies

Please help with mail() not working

Worked fine one day, next day it stopped. Banging head on wall here.

         

epheterson

7:52 pm on Aug 16, 2011 (gmt 0)

10+ Year Member



No idea what could be wrong, it worked fine and all of a sudden stopped. I realized first problem was I didn't have "quotes" around the sender's name. Now I've deduced the problem is somehow related to the $message variable. If I replace $message with a simple text line, the message sends. As displayed below, bool mail() returns true, but the message never arrives. Please guide on anything I could try:

<?php

function pt_register()
{
$num_args = func_num_args();
$vars = array();

if ($num_args >= 2) {
$method = strtoupper(func_get_arg(0));

if (($method != 'SESSION') && ($method != 'GET') && ($method != 'POST') && ($method != 'SERVER') && ($method != 'COOKIE') && ($method != 'ENV')) {
die('The first argument of pt_register must be one of the following: GET, POST, SESSION, SERVER, COOKIE, or ENV');
} /* end if $method */

$varname = "HTTP_{$method}_VARS";
global ${$varname};

for ($i = 1; $i < $num_args; $i++) {
$parameter = func_get_arg($i);

if (isset(${$varname}[$parameter]))
{
global $$parameter;
$$parameter = ${$varname}[$parameter];
} /* end if isset */
} /* end for $i loop */

} /* end args >=2 */
else {
die('You must specify at least two arguments');
} /* end else */

} /* end function */


pt_register('POST','Name');
pt_register('POST','Email');
pt_register('POST','PhoneNumber');
pt_register('POST','Time');
pt_register('POST','Comments');

//Probably not a bot, process the mailing
if(strlen($Name)!=0 && htmlspecialchars($Name) == $Name){

$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));

$message = "<html><body>";

$message .= "Name: ".$Name."<br><br>";

$message .= "Email: ".$Email."<br><br>";

$message .= "Phone Number: ".$PhoneNumber."<br><br>";

$message .= "Best Time to Call: ".$Time."<br><br>";

$message .= "Comments:<br>".nl2br($Comments)."<br><br>";

$message .= "----<br>";

$message .= "This is a form-generated email sent by <a href=\"http://www.totally-bananas.net\">Totally-Bananas.net</a>.";

$message .= "</body></html>";

$message = stripslashes($message);
$message = wordwrap($message, 70);

echo $message;

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: "'. $Name . '" <' . $Email . '>' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";

if(mail('epheterson@gmail.com','Contact Request from Totally-Bananas.net!',$message,$headers)) {
//header("Location: http://" . $_SERVER['HTTP_HOST'] . "/thanks.php");
} else {
echo 'Dreadfully sorry, something has gone awry. Please hit your browser\'s back button and send your message manually to chuck@totally-bananas.net. You might also mention that you\'ve received this error.' . "<br />\n";
}


} else { // No name, probably a bot, throw it away...
header("Location: http://" . $_SERVER['HTTP_HOST'] . "/nope.php");
}

?>

penders

9:43 pm on Aug 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Have you tried different email clients... Gmail, Hotmail, Thunderbird, Outlook? If it's just one client that is failing to receive then you perhaps just have to tweak the format. If all are failing then it could be something more fundamental.

Do you need to send text/html (as opposed to simple plain text)? It is advisable to send plain text in addition to text/html in order to avoid spam filters and to be able to get through to users who have disabled rich text emails.

epheterson

9:52 pm on Aug 16, 2011 (gmt 0)

10+ Year Member



Now it's almost a challenge to get HTML working, because it was working a month ago and I prefer to deliver a pretty message. This is only being used for a simple contact us form, so it will always arrive at a GMail account and always to the same recipients. If it passes once, it will always clear the spam filter.

I'll check another client but imagine GMail is on top of things. Have you looked over the code?

penders

10:38 pm on Aug 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yeah Gmail is usually pretty good. Hotmail (and Outlook) are more troublesome I've found.

I wonder if
wordwrap($message,70)
is breaking your tags apart? Instead of wordwrap() you could try just adding LF "\n" at the end of each line, where the line would logically break. Or perhaps just apply wordwrap() to your $Comments?

Also try removing stripslashes(). I would tend to only apply stripslashes (if needed) to the individual form inputs, rather than the entire message?

...and welcome to WebmasterWorld btw! :)

epheterson

3:14 am on Aug 17, 2011 (gmt 0)

10+ Year Member



Hey Penders, I added the wordwrap after much other troubleshooting, not the issue unfortunately. I have other code which doesn't hurt being wrapped, so it's okay there.

I'll try switching to just text :-/ and using \n, and removing stripslashes though it's not being used in my tests regardless.

Thanks for the warm welcome! I've been coding since middle school but have just found your website while troubleshooting this problem.

penders

9:26 am on Aug 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've just tried your code and it works OK for me. With or without the stripslashes() and wordwrap(). Sent to my gmail account.

I hard coded your form variables with these values for testing:
$Name = 'Jim Bob'; 
$Email = 'jim@example.com';
$PhoneNumber = '123456789';
$Time = '18:00';
$Comments = <<<EOD
This is a test. This is a test. This is a test.
This is a test. This is a test. This is a test.
This is a test. This is a test. This is a test.
This is a test. This is a test. This is a test.
EOD;


Perhaps you have 'real world' test data I can try?

As you've found out, mail() returns true if it is accepted for delivery, which doesn't mean that it has been sent successfully.

Just to add, I don't think it matters whether you have quotes around the senders (From:) name.

The fact that you say it used to work for you and it appears to work for me suggests that something may have changed with your mail server? However, since you say the problem appears to be dependent on the $message, I have my doubts about this. Mail servers can be picky about the TO and FROM addresses, if they are from different domains etc. But I guess you don't get any errors?

epheterson

1:10 pm on Aug 17, 2011 (gmt 0)

10+ Year Member



>.>

I had a lengthy talk with my webhost about how I looked that code up and down and suspect the server is at fault. Here is the actual $message I echo before sending which fails for me:

<html><body>

Name: Test Name<br><br>

Email: e@mail.com<br><br>

Phone Number: 123-456-7890<br><br>

Best Time to Call: Evening<br><br>

Comments:<br>This is a test.<br><br>

----<br>

This is a form-generated email sent by <a
href="http://www.totally-bananas.net">Totally-Bananas.net</a>.

</body></html>

penders

6:51 pm on Aug 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yep, that works OK for me too.

As mentioned, mail servers/programs can be rather picky sometimes about the mail that they send. Requiring you to get it in just the right format. Sometimes it's a security thing in trying to prevent spamming (being used as an open relay etc.).

If, like you say, you can send successfully when the $message is a 'simple text line' then I would guess that some security module is interfering - but that is only a guess.

Are you able to send the whole message as 'plain text'? Does that at least work?

epheterson

7:04 pm on Aug 17, 2011 (gmt 0)

10+ Year Member



Excluding the header, or simply sending the string, does not work. May I ask what version of PHP you're using?

penders

7:44 pm on Aug 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just with From: and X-Mailer: headers and just plain text in the $message. And even that does not work?! I'm testing this on PHP 5.2.14

Have you tried valid From: and To: email addresses, perhaps relating to the domain you are running this on?

epheterson

8:01 pm on Aug 17, 2011 (gmt 0)

10+ Year Member



That is the case, and we're running 5.2.12... *scratches head*

The from feature is desired, I have tried valid ones, but I don't want to drop this functionality. I suppose I'll test...

This is my latest, simplified, test code which fails:

<?php

$Name = 'Eric L. Pheterson';
$Email = 'eph@gmail.com';
$PhoneNumber = '123-456-7890';
$Time = 'A.M.';
$Comments = 'This is a test. Using te.php';

$message = '
Name: '.$Name.'

Email: '.$Email.'

Phone Number: '.$PhoneNumber.'

Best Time to Call: '.$Time.'

Comments:
'.$Comments.'

----

This is a form-generated email sent by www.Totally-Bananas.net.';

echo $message;
$message = wordwrap($message, 50);

//$headers = 'MIME-Version: 1.0' . "\r\n";
//$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = 'From: "'. $Name . '" <' . $Email . '>' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";

if(mail('epheterson@gmail.com.com','Contact Request from Totally-Bananas.net!',$message,$headers)) {
echo 'Supposedly, it sent?';
} else {
echo 'Dreadfully sorry, something has gone awry. Please hit your browser\'s back button and send your message manually to c@totally-bananas.net. You might also mention that you\'ve received this error.' . "<br />\n";
}


?>

rocknbil

3:53 pm on Aug 18, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



epheterson@example.com.com
?

I imagine you corrected that . . .

epheterson

4:03 pm on Aug 18, 2011 (gmt 0)

10+ Year Member



Yeah, that's a mistake right before posting it here. Not the issue.

I'm up in arms with FatCow web hosting right now. They're ignoring the issue were blaming my code. It appears they have some settings wrong, or an overly-protective spam filter. Their solution?

"Perhaps, your mails are being filtered by our Spam scanners. We advice you to remove any reference to your domain and compose those mails with just text and let there be no HTML contents to ensure your mail get through our Spam scanner."

I'm about to switch to a new webhost if they don't get it together.

rocknbil

6:03 pm on Aug 19, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Before you jump . . .

One of the benchmarks of a good coder, no matter what your skill or experience level is, is that before you blame outside sources consider . . . .

Maybe it's something I'm doing or don't know about. Yet!

If it's an email or virus scanner, maybe this is happening for a very good reason. There are volumes and volumes of information about proper mail headers and mail content (did you know that many, MANY spam prevention programs will junkbox an email if it contains the word "free" or even "special offer?")

This is a particularly tough issue with email. I had one client with a full self contained mailing system - we had all sorts of delivery and junkbox issues, like this, and it took months to work through them all. Gmail and other web based mailing services are particularly dicy, as well as recipients whose servers use SpamAssassin. But I carry that stuff with me today and never would have known . . .

I'll give you one example. This is a set of headers I arrived at that appears to be trouble free - it's from a perl program so it includes the $to (you can delete X-Mailer by the way . . ):

To: $to
From: $from
Date: $mail_time <-- I remember failing a lot until I included this <shrug>
Content-Type: text/html; charset=US-ASCII
Content-Transfer-Encoding: 8bit
MIME-Version: 1.0
Subject: $sub
<html lang="en">
$msg
<html>


Step back, work through it with them, you **will** learn something and be a better coder for it.

epheterson

7:11 pm on Aug 19, 2011 (gmt 0)

10+ Year Member



Nah everything is in order, they're actually blocking any messages that have my domain name in them.

"Dear faithful follower of my website,
We have exciting new offers for you! Instead of clicking a link to find them, go to your browser and type www [dot] mydomain [dot] com into your address bar because my web host does not allow me to link to them!"

I consider that grossly unacceptable.

I've tried with and without the "www." and that makes all the difference. I did try adding the Date: but I checked the raw messages in GMail that made it through and php puts the Date: there automatically.

I've spent days tearing apart my code.