Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2005-03-18
10
Medium Priority
?
298 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:jtcy
  • 5
  • 4
10 Comments
 
LVL 16

Expert Comment

by:alain34
ID: 13573162
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
 

Author Comment

by:jtcy
ID: 13573911
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
 
LVL 16

Expert Comment

by:alain34
ID: 13574388
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jtcy
ID: 13574660
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
 
LVL 16

Expert Comment

by:alain34
ID: 13574763
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
 

Author Comment

by:jtcy
ID: 13574778
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
 
LVL 16

Expert Comment

by:alain34
ID: 13574930
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
 

Author Comment

by:jtcy
ID: 13574988
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
 
LVL 16

Expert Comment

by:alain34
ID: 13575053
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
 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 200 total points
ID: 13575976
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question