?
Solved

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

Posted on 2005-03-18
10
Medium Priority
?
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

777 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