PDA

View Full Version : PHP $Mail headers


Rob_Che
05-22-2011, 09:53 PM
So - I have a mail script that I've always used. No idea where I robbed it from but it has served me well!

However, I'm finding its working less and less and I've been reliably informed by a complete stranger that $mail isn't the way the cool kids are sending mail now.

Here's what I use:

$content = "You have received a message from $senderName $surName [ $email ] \n
$message \n ";
$email = preg_replace("/[^a-zA-Z0-9s.@-_]/", " ", $email);
$senderName = preg_replace("/[^a-zA-Z0-9s]/", " ", $senderName);
$headers = "From: $senderName $surName <$email> \r\n";

mail($sendto_email, 'Contact from your website', $content, $headers);
print "
<h4>Thank you for your message ".$senderName."</h4>";
}

It works on localhost but doesn't work on my server (only on some sites which is relly annoying)

So - can someone guide me please?

Thanks in advance...
Rob

edbr
05-23-2011, 01:33 AM
$mail isn't the way the cool kids are sending mail now.
bah humbug i guess he might be talking about sendmail but mail is still fine. When you say not working do you mean not sending or not arriving?

you could be getting blocked by spam filters. add some headers


$headers="Content-Type: text/html; charset=windows-1252 \n"; //for html or
$headers="Content-Type: text/plain \n"; /// for text


$headers.="From: admin@bali-web-hosting.com \n";
$headers.="X-mailer:";

Rob_Che
05-23-2011, 06:40 AM
bah humbug I guess he might be talking about sendmail but mail is still fine.

you could be getting blocked by spam filters. add some headers


That's the ticket - "You don't have the right headers, you need to add some" is what the fella said... I didn't have a clue but I see the headers now!

When you say not working do you mean not sending or not arriving?

How do I know? It seems to run through the script and prints out the 'Thank you' message but no mail arrives (not in the mail junk bin but could be blatted at server level I guess)

I'll try adding some headers and see if that works - thanks Ed!

Rob_Che
05-23-2011, 06:59 AM
Hmmm.... still nothing.

Any debugging tips would be appreciated :)

Rob_Che
05-23-2011, 07:01 AM
My headers...


// Result on success

$content = "You have received a message from $senderName $surName [ $email ] \n
$message \n ";

$email = preg_replace("/[^a-zA-Z0-9s.@-_]/", " ", $email);
$senderName = preg_replace("/[^a-zA-Z0-9s]/", " ", $senderName);

$headers.= "From: $senderName $surName <$email> \r\n";
$headers.="Content-Type: text/plain \n"; /// for text
$headers.="From: admin@schoolsown.com \n";
$headers.="X-mailer:";

mail($sendto_email, 'Contact from your website', $content, $headers);
print "<h4>Thank you for your message ".$senderName."</h4>"
;
}

edbr
05-23-2011, 07:08 AM
well to start you have 2 from headers

edbr
05-23-2011, 07:14 AM
have a look at the available headers
http://php.net/manual/en/function.mail.php

Rob_Che
05-23-2011, 08:18 AM
well to start you have 2 from headers

Ah yes - that won't help anyone will it.

have a look at the available headers
http://php.net/manual/en/function.mail.php

Thanks Ed - I did look at the library previously but I'll dig down and try some of the library examples for testing.

Rob_Che
05-23-2011, 09:25 AM
Still not working - I've tried a simple script to test it:

var_dump (
mail('rob@XXXXXXX.com', 'My Subject', 'My message')
);

Returns true so the PHP thinks its sent. Does that mean it's server side?

edbr
05-23-2011, 09:30 AM
i suspect its spam filters. but make a page with the mail function a bnd just call it. ive got your email somewhere ill send you a mail script complete with captcha

Rob_Che
05-23-2011, 09:31 AM
Thanks Ed - the script above is my total page code (minus <?PHP ?>), no form or anything. Just for testing. Appreciate your help as always.

edbr
05-23-2011, 09:41 AM
perfectpixels.biz is that you?

edbr
05-23-2011, 09:43 AM
i sent it ,check your mail

Rob_Che
05-23-2011, 10:07 AM
Got that, thanks Ed.

Well - I'm guessing its server side then as that too works locally but not on the server. Ho hum. I get good customer service from my host but I still contact them too much...

Thanks Ed - time for an email to customer services...

edbr
05-24-2011, 12:51 AM
ok let me know what the problem is. im having a battle with cname as im setting up a domain selling site and it driving me up the proverbial, especially as i know it is not worth the effort anyway.
the mailing class is a nice one IMO i cobbled it together with the captcha function which i like because its so easy to modify ,including the embeded font. have fun with it

Rob_Che
05-24-2011, 06:44 AM
I'm a bit lost to be honest. The host says $mail is working their end and I'm not expecting them to debug my PHP (although they have have offered which is cool).

I'll try it on some different domains on the same sever and see if it might be the .htaccess or something.

Thanks Ed, I will let you know.
Rob

Rob_Che
05-27-2011, 11:38 AM
OK - quick update.

I managed to get it working, kinda. I used additional headers to specify the from and reply-to address and made sure these were valid email addresses on the same domain. This works when sending to an external email address but won't send to itself (same domain). I hear this could be due to postfix stopping them?

Cheers Ed
Rob