How to configure PHP mail() for authenticated SMTP server?

As how I stated the title, how can I make my php sendmail able to use external mail server, which needs authentication? I know I need to write a php script for this using the fsocketopen or whatever function it is called, but can someone gimme some handy ones? I have no idea how to do that.
jtcyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

alain34Commented:
the best way for you will be to use phpmailer.
this code will handles all your need for email under php, include authentification and multiple smtp server!
http://phpmailer.sourceforge.net
0
jtcyAuthor Commented:
I have already downloaded it, but how do I use it? I read from the installation that it shoud be included in the include path, but I am not sure if I am correct:

;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;

; UNIX: "/path1:/path2"  
;include_path = ".:/php/includes"
;
; Windows: "\path1;\path2"
;include_path = ".;c:\php\includes"

Thats where I put the class.phpmailer.php and class.smtp.php in. But then how do I use it? Can I send use those email functions in a php script like vbulletin, phpbb now? It still doesnt work.
0
alain34Commented:
install it in a subfolder of your script as phpmailer and that add the code below. Obvisouly, you will need to replace the variable by yours, but it is as simple as that!

      require("phpmailer/class.phpmailer.php");

      $mail = new phpmailer();

      $mail->From     = $sender;
      $mail->FromName = $sender;
      $mail->Host     = "localhost";
      $mail->Mailer   = "smtp";
      $mail->Subject  = $subject;      
      $mail->Body     = $messageHtml;
      $mail->AltBody  = $message;
      $mail->AddAddress($recipient);

      $mail->Send();

in the phpmailer folder, you just need 2 files :
- class.phpmailer.php
- class.smtp.php

You can use that in Vbulletin and phpbb, but you will need to change the code to use phpmailer.

try with a test page. I remenber that phpmailer download include sample code!
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

jtcyAuthor Commented:
Okay, I have the following:

-------------------------------------
<?php
require("class.phpmailer.php");

$mail = new phpmailer();
$mail->IsSMTP();
$mail->Host="smtp.email.com";
$mail->From="from@email.com";
$mail->AddAddress("Myemail@myemailhost.com");
$mail->Subject="first mailing";
$mail->Body="hi!";
$mail->WordWrap = 50;

if(!$mail->Send())
{
  echo "Message was not sent";
  echo "Mailer Error: " . $mail->ErrorInfo;
}

else

{
  echo "Message has been sent";
}

?>

----------------------------------------------

But when I run it on browser, it shows: Warning: fsockopen(): php_network_getaddresses: gethostbyname failed ...

I already have set the host to my smtp host in class.phpmailer.php though...
0
alain34Commented:
use in the 2 files from the download without changing them.

the only thing that I can see as a problem, is that your smtp server is not located on the same server that you use your script.
Do you know if the smtp server is installed on the same server. If the answer is yes, therefore the smtp host shoudl be localhost. if the answer is no, and if you get some error like that, it is may be simply because you are not allow to go throught the firewall to go to your smtp server.
0
jtcyAuthor Commented:
No, the smtp server is external, thats the whole problem I have been asking. I would like to know how to enable php mail function to connect to an external authenticated smtp server. In fact, my mail server is smtp.fusemail.net.

Any how?.....
0
alain34Commented:
I believe therefore that the problem is not with your email function, but more with your hosting account that prevent you to connect to an external url. Have you check with your hosting representative if it is somthing that you can do!
0
jtcyAuthor Commented:
Well~~~ there's certainly something I can do cuz I own the server. hehe....I am just not sure how to use phpmailer thing. So, I put the two classes in the same directory of the sample.php, then do I need to configure class.phpmailer.php? Or just need to configure sample.php?
0
alain34Commented:
yes that's it, and it is the way that it works for me, just by following the instruction.
The main difference is that I'm using a hosting account with the smtp server installed locally, therefore therefore there is no issue with fopensocket.
If you believe there is no firewall issue, may be you need to add the port of your smtp server to the url!
I'm afraid, it seems that your problem is really specific to your configuration!
0
Marcus BointonCommented:
You missed several bits that are required for authenticated SMTP:

<?php
require_once 'class.phpmailer.php';

$mail = new phpmailer();
$mail->IsSMTP();
$mail->Host='smtp.email.com';
$mail->SMTPAuth = true; //use authenticated SMTP
$mail->Sender='from@email.com'; //Authorised user on SMTP server - note that it's a different purpose than from header
$mail->Password='mypassword'; //Password of authorised user
$mail->From='from@email.com';
$mail->AddAddress('Myemail@myemailhost.com');
$mail->Subject='first mailing';
$mail->Body='hi!';
$mail->WordWrap = 50;

if(!$mail->Send())
{
  echo "Message was not sent";
  echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
  echo "Message has been sent";
}
?>

If you're on a unix-like platform and have a local MTA (sendmail, postfix, exim, qmail etc), you should NOT be using IsSMTP() - use IsMail, IsSendmail, IsQmail etc. Using SMTP when it's not necessary is slower and less reliable than sending directly to a local MTA - the main reason it's in phpmailer is so that Windows users don't get left altogether out in the cold.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.