/    Sign up×
Community /Pin to ProfileBookmark

mail() function does not work, how to debug?

hi,
I’m having trouble with mail function. Basically it returns false in all cases, I even tryed simplest script that is given in the manual but nothing. I was looking for debug functions and did not find any. Server support ppl say its my own prbolem πŸ™ and i think its something wrong with the server.

Does anyone know how can i debug mail function? or maybe some one had relative problem?

Thanks!

to post a comment
PHP

17 Comments(s) ↴

Copy linkTweet thisAlerts:
@NogDogMay 10.2007 β€”Β Unfortunately, the mail() function often lacks in any useful debug information. You could post your code here if you'd like us to review it for any likely problems.

Probably the most common problem I see is not using a valid "From:" email address, which on most servers must be a valid email address on that server.
Copy linkTweet thisAlerts:
@alexusauthorMay 10.2007 β€”Β ok, as manufact I didn not heve reply to or from address, so i added all that but still NO luck :-(

[code=php]
<?php
$to = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "rn" .
'Reply-To: [email protected]' . "rn" .
'X-Mailer: PHP/' . phpversion();

if(mail($to, $subject, $message, $headers)){
echo "Mail Sent";
}else{
echo "***ERROR***";
}
?>
[/code]
Copy linkTweet thisAlerts:
@NogDogMay 10.2007 β€”Β When you run that, do you get the "Mail Sent" message or the ***ERROR*** message? If you get the "Mail Sent", is there a junk folder on the recipient mail account you can check to see if it was treated as "spam"?
Copy linkTweet thisAlerts:
@alexusauthorMay 10.2007 β€”Β im getting ***ERROR***
Copy linkTweet thisAlerts:
@NogDogMay 10.2007 β€”Β Is this on a Windows or UNIX/Linux server?
Copy linkTweet thisAlerts:
@alexusauthorMay 10.2007 β€”Β FreeBSD or Linux (not sure, the provider has both of then and thes swaping the sites from one to another)
Copy linkTweet thisAlerts:
@NogDogMay 10.2007 β€”Β Same difference, just so it's not Windows. (Windows does not have the native sendmail program that the PHP mail() function calls, so requires some configuration issues that need to be addressed.)

One other thought: you might want to make sure any errors/warnings are being displayed, by adding the following to the start of the script:
[code=php]
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
[/code]
Copy linkTweet thisAlerts:
@alexusauthorMay 11.2007 β€”Β no errors outputed even after i forced the printing of all errors '-(
Copy linkTweet thisAlerts:
@kurniawanMay 11.2007 β€”Β You might want to check your webhosting, whether they apply some mail numbers resctriction or not. If the restriction is applied and you exceeded the number, of course mail() function will not work.


----------------------------------------------
http://anaknegri.com http://arifkurniawan.net http://pajaknenkin.com http://ppi-kansai.net http://kti2007.ppi-kansai.net
Copy linkTweet thisAlerts:
@alexusauthorMay 11.2007 β€”Β well thats a good thought, we have limit 500 outgoing emails per hour, but we dont use SMTP at all on our domain so we should not go over the max, although I probably should check with them just in case
Copy linkTweet thisAlerts:
@TaschenMay 11.2007 β€”Β Presuming there are no errors generated by PHP, but rather that the mail isn't being sent/accepted by sendmail:

webhosts increasingly have their systems set to require the "-f" flag

so instead of

mail($to, $subject, $message, $headers)

change to

mail($to, $subject, $message, $headers, "-[email protected]")

note there is no space between the f and the email address.

[email][email protected][/email] must (usually) be a valid email hosted on the domain from which the email is being sent.
Copy linkTweet thisAlerts:
@alexusauthorMay 11.2007 β€”Β Taschen, will try usingh that switch... what does -f do?
Copy linkTweet thisAlerts:
@saeMay 11.2007 β€”Β I think the -f switch is so that you get notified if the email gets bounced back.

so [email][email protected][/email] would send an email to [email][email protected][/email] if the mail gets bounced
Copy linkTweet thisAlerts:
@TaschenMay 11.2007 β€”Β -f is an additional parameter used by sendmail (the program many servers use to , uhm, send mail). Basically it's the "from" address. So why set the from address twice?

According to the PHP manual -f sets the X-envelope sender (who is sending the mail), the address specified by -f is a "trusted user" and will be checked against "/etc/mail/trusted-users".

This helps to mitigate against the mail function being abused. It helps to validate that the mail being sent is really from your domain and not simply using a mail function on your domain (in practice the "real" from address will be what is specified by -f as opposed to what you specify in the $from field.
Copy linkTweet thisAlerts:
@alexusauthorMay 11.2007 β€”Β somuch hope, so little luck :-(

I added the -f and nothing changed, I even tried to remove the headers and just use -f but still no luck :-(

[code=php]
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$to = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "rn" .
'Reply-To: [email protected]' . "rn" .
'X-Mailer: PHP/' . phpversion();

if(mail($to, $subject, $message, $headers, "[email protected]")){
echo "Mail Sent";
}else{
echo "***ERROR***";
}
?>

[/code]
Copy linkTweet thisAlerts:
@TaschenMay 11.2007 β€”Β What you have does it for me providing that xxxxx.com is the domain which the email is being sent from (that is, the page with the mail() script is hosted on domain xxxxx.com).

If you are hosting the script at "yyy.com" but claiming to be "xxx.com" the "-f" flag won't do anything other than to return an X-Mailor Error causing the mail not to be sent.

I presume that you are still getting the "****ERROR****" error? (I.e. mail() is returning "false" when you run the script.)
Copy linkTweet thisAlerts:
@alexusauthorMay 11.2007 β€”Β well I just contacted the Level2 support of myhosting provider and they told they they disabled mail function to prevent spamming ... oh greate so much work in debuging ? and now i have to use other ways to send mail :-(
Γ—

Success!

Help @alexus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 β€” BETA 6.2,
whats_new: community page,
up_next: more Davinciβ€’003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinciβ€’003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...