tags:

views:

15

answers:

2

Hello guys,

As we know it is possible to send emails using php mail() function. but in some servers it requires SMTP authentication (ie we have to provide an email address and password). How can I determine through a PHP script that the server require authentication or not.

Thanks

A: 

Why not just test it out by sending a mail using SMTP authentication? You can easily use one of the following libraries to do so:

  1. PHPMailer.
  2. Pear Mail Package.
shamittomar
Yes it possible, but my question is how can I determine that I can send a mail without smtp authentication buy CODE (PHP CODE) ?
john
Check if your `mail()` function returns `false`. If it does for a simple mail to a valid address, then you need SMTP authentication.
shamittomar
+1  A: 

The PHP mail() function only returns true or false, signifying whether the e-mail was successfully handed off to the mailer or not. It isn't possible to get any extra information, because PHP doesn't even handle the mailing. It hands it off to sendmail or whatever is configured in PHP.ini.

To determine the reason for failure, you must use another method. I'd like to second shamittomar's suggestion of Pear's Mail package. It is excellent.

If you use Mail::send() in Pear, it will either return true or a PEAR_Error() object which you can call the getCode() method on and figure out if you have an SMTP authentication error.

See this for more details: http://pear.php.net/manual/en/package.mail.mail.send.php

Brad
upvoted over shamittomar for explanation of boolean mail() function.
Tim