Using PHP 5.0.4/MySQL 4.1.12/IIS 6. Have setup an Microsoft SMTP virtual server which works fine when I try to send an outgoing email via "localhost" using Outlook.

However when trying to use mail(), i get an error "Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini"

I had previously tried to use my ISPs smtp server but also got the same "failed to connect" error which is why i tried going locally.


<?php
ini_set("SMTP","localhost");
ini_set("sendmail_from","test@mydomain.com");

if (mail("testrecipient@mydomain.com","This is a test","This is the body","From: [email]test@mydomain.com[/email]\r\nContent-type: text/plain\r\n\r\n"))
{
print "Success";
}
else
{
print "Failed";
}

?> 

I don't understand why localhost works in Outlook but not with php mail()?

    I too was working/developing on localhost before porting the site to the customers live directory. So here are some suggestions for you.

    1. hard code the setting in your php.ini file instead of using
      php ini(val1,val2) look for this setting:

    [mail function]
    ; For Win32 only.
    SMTP =mySMTPdomain.com

    where mySMTPdomain is either localhost or a real domain.

    The SMTP for sendmail is below the Win32 setting.

    1. When I composed the email in my script I use

    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers .= "From: ".$_POST['Email']."\r\n";
    $to = "my_email@somewhere.com";
    $subject = "Request for Info";
    $body = "Test Email"

    if(mail($to,$subject,$body,$headers))
    //some code here to see if email sent

    I show this because it doesn't appear to me as though your headers are including all the info necessary, unless I missed something.

      6 days later

      How can I test if sending mail through localhost works in outlook?

        Well;

        Outlook is just a mail client so it still needs a valid POP3 (outgoing) and SMTP (incoming) mail server. These are setup in the PhP ini file.

        The localhost part is your web server (like apache). So what happens is you send mail from your web server (localhost) thru the pop3 server (your ISP). You can select tools/email accounts from the outlook toolbar to setup the POP3 and SMTP values provided by your ISP.

        I do not think you can set outlook's SMTP and POP3 as "localhost"

        Manual Ref: http://www.php.net/mail

        In my opinion the internet has way to many TLA's (Three letter acronyms).

          So you cannot use outlook to check here because I would need both to be set to localhost. That's unfortunate.

            6 months later

            PHP Newbie: I am having a similar problem in a slightly diffrent environment. I am not sure if the fix above applies to my situation or not, or how to implement it if it does.

            ENVIRONMENT:
            Windows 2003 SP1 Server
            IIS 6.0
            MySQL v 4.1
            PHP v 4.3.11

            It is important to note that this is a dedicated server where I am the admin, This is NOT a single shared hosting account. I do have root access to the box. I also need to make my settings generic a I will be using PHP for other domains that I host. So settings can not be specific to one website.

            BROWSER ERROR MESSAGE:
            This is returned when processing a sale in my shopping cart Zen Cart. I am told by experienced Zen Cart people that this is a PHP issue as I am also not receiving email from Zen cart as I should.

            Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\Inetpub\myclientwebsite\www\zencart\includes\classes\email.php on line 554

            Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\Inetpub\myclientwebsite\www\zencart\includes\classes\email.php on line 554

            Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\myclientwebsite\www\zencart\includes\classes\email.php:554) in C:\Inetpub\ myclientwebsite \www\zencart\includes\functions\functions_general.php on line 52

            VERIFIED PHP.INI SETTINGS
            SMTP = ‘localhost”
            smtp_port = 25
            sendmail_from = serveradmin@myserverdnsdomain.net

            I am a PHP newbie and would appreciate some guidance on how to fix this issue?

            Thanks - Jody

              Well the manual sez that you need to include the DNS name or IP of the smtp server under winders. So I'd first try 127.0.0.1 (localhost to the tcp/ip stack) or the real ip of the server.

              Other things to look out for would be whether your smtp server has been configured to relay for the domain name you are using. Also check if you have it configured to require authentication cos you'll have to supply a username and password if it does. Remember that the defaults in winders are NEVER the ones you expect or need.

              Some links that may help

              SMTP class
              MSN

                Roger,

                Thanks for the prompt reply. I have some questions about your response:

                1. What are winders?

                2. So you are saying that instead of SMTP=localhost in the php.ini it should be SMTP=127.0.0.1 or the actual IP of my mail server?

                I will check out the links you suggested and thanks - Jody

                  winders is everything in the microsoft operating systems world, a sarcastic corruptions of Windows. Childish humour I know, much like my MickeySoft perversion of microsoft, ie a company that produces Mickey Mouse software. But then I've been anti-ms since Dos 4 was released.

                  As to the ip address, yes, that's what the manual sez. Now 'localhost' will always resolve to 127.0.0.1, and should be the first thing in your hosts file. The IP stack recognises 127.0.0.1 as itself or 'this interface'. So cut out the DNS resolution and go direct to the ip address.

                    Thanks for that explination. I'm with you but stuck with them like so many others.

                    Anyway....

                    I verified SMTP port is 25 and I have tried both the 127.0.0.1 and my mial server IP addresses. Neither solved the problem.

                    I looked at the links you provided and I do no tunderstand the SMPT link but as far as the MS link, I am not using Exchange Server. I am running SmarterMail 2.6 via IIS6, if that matters.

                    Do I need to reset something after changing the PHP.ini or could this be a permissions issue or something? Any other ideas?

                    Thanks - Jody

                      Roger -

                      Are you saying with the SMTP download page link you provided that I need to install a model to allow for SMTP authentication in PHP 4.3.11? I tried to download an uncomporess the .tar file with WinZip but I can not open the file.

                        Write a Reply...