Friday, January 7, 2011

Xampp Apache and SMTP

Many users us PHP to program their E.mail form. And most of them face problems with " mail()function.

Famous massage Warning: mail() [function.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:\wamp\www\TestApplication\sendmail.php on line 65, 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:\programmi\easyphp1-8\www\drupal\modules\user.module on line 394, or sendmail.php on line 14.. etc.

These massages above came from trying to send an email through a web form.
It's from your local machine PC or Laptop.. Windows or what so ever.

Someone asked me this question:
How to send mail in php localhost?

To do that you should first create a mail form. You can use example at W3schools.com or you can create your own script for that.
I'll use the example at that site .

You need to create a php file .. call it whatever you like .. example mail.php
Copy that Code and Paste it. click save and choose place where you can run it later..
example Http://localhost/test/mail.php

Now go to your director where you have the PHP Apache Server. I use XAMPP and i have it at Drive C:\xampp

  1. First Step you gotta do is configuring the SENDMAIL.ini you can find that at this location C:\xampp\sendmail ... if you use other location please WATCH THAT AND CHANGE.
  2. Open the file sendmail.ini and look inside the file concentrically you'll find it this way:
    # Example for a user configuration file

    # Set default values for all following accounts.
    defaults
    logfile "C:\xampp\sendmail\sendmail.log"

    # Mercury
    account Mercury
    host localhost
    from postmaster@localhost
    auth off

    # A freemail service example
    account Hotmail
    tls on
    tls_certcheck off
    host smtp.live.com
    from [exampleuser]@hotmail.com
    auth on
    user [exampleuser]@hotmail.com
    password [examplepassword]

    # Set a default account
    account default : Mercury
  3. Third step you need to have email that can provide you SMTP, HOST, PORT, USERNAME, PASSWORD. Many Email companies could provide you that.. in this Example I'll use GMAIL.. You can use Hotmail, Yahoo..etc.
    Just make sure you have the information above.
  4. Modify the Information at sendmail.ini with this one here.. and make sure you put your information for FROM, USER, PASSWORD
# Example for a user configuration file

# Set default values for all following accounts.
defaults
logfile "C:\xampp\sendmail\sendmail.log"

# Mercury
account Mercury
host localhost
from postmaster@localhost
auth off

# A freemail service example
account Gmail
tls on
tls_certcheck off
host smtp.gmail.com
port 587
from YOUR_EMAIL@gmail.com
auth on
user YOUR_EMAIL@gmail.com
password **********YOUR_PASSwoRd

# Set a default account
account default : Gmail

Save the file.

Now after finishing that.. you need to modify other file called PHP.ini. It's the the one that have the configuration for your Apache server.
Again I've my XAMPP at C: if you have it at D: or else where WATCH THAT :). To get into the PHP.ini you need to follow this with my example C:\xampp\php\php.ini

  1. Open PHP.ini and you'll press Ctrl+F to get the search window. type inside mail and enter twice till you see [mail function] here what we want to reach. It'll all look like :-

    [mail function]
    ; For Win32 only.
    ; http://php.net/smtp
    ; SMTP = localhost
    ; http://php.net/smtp-port
    ; smtp_port = 25

    ; For Win32 only.
    ; http://php.net/sendmail-from
    ;sendmail_from = postmaster@localhost

    ; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
    ; http://php.net/sendmail-path
    sendmail_path = "C:/xampp/sendmail/sendmail.exe -t"

  2. If it was different then this please consider that and change :) It should be the way above.
  3. Last Step Restart Apache.

    This works perfectly, but remind, that all mails you are sending via PHP now are coming "From: YOUR_EMAIL@gmail.com" (you cannot supply a different FROM: clause, even if you do so, it's overwritten).
Any Questions Please Comment and I'll try my Best to Guide you :))

BIG

1 comment:

  1. Thank you BIG00MAN for your help... I was looking to solve this matter long time ago.
    All they post at the internet was just philosophy no solution at all.
    But here i tried your tutorial and it worked fine with me ..

    Thank you again dear friend..
    Mark.J

    ReplyDelete