Pages

Thursday, April 1, 2010

Sending mail from Localhost

After a long time, I got this working, hence logging it on my blog so that I can refer to it later.

The softwares I have used:


  1. XAMPP
  2. Notepad
  3. Hotmail ID

It can be difficult getting the PHP’s default mail functions to work when you run an apache server on windows instead of a unix environment. I ran into this problem with my server (running XAMPP), and thought others may benefit from a quick how-to. Another slight problem I had to get working was using an SMTP server that requires SSL connections (gmail for one). I didn’t realize that most recent XAMPP releases have sendmail built in already. This takes part of the hassel out, but we still have some configuring to do.

First, ensure that you have an XAMPP release that does include the fake sendmail program. 

Next, go to the XAMPP directory (often C:\xampp\), then open the ’sendmail’ subdirectory. You’ll most likely see the following files (default): 

sendmail.exe

readme.html

sendmail.ini

sendmail_example.ini

license.html

Open the file sendmail.ini – this is where all of your SMTP server configurations will go, for example: SMTP Username, Password, Host, Port, etc 

My sendmail.ini file looks something like this:

; configuration for fake sendmail

[sendmail]

smtp_server=smtp.gmail.com

smtp_port=465

; SMTPS (SSL) support

; auto = use SSL for port 465, otherwise try to use TLS

; ssl = alway use SSL

; tls = always use TLS

; none = never try to use SSL

smtp_ssl=auto

default_domain=brettshaffer.com

error_logfile=error.log

; if your smtp server requires authentication, modify the following two lines

auth_username=

auth_password=

Note that this is a very simple setup. The variable names are fairly obvious, so simply fill in the SMTP server, port, ssl mode, username, password, and default domain / error log if you’d like.

GMail requires an SSL connection, so my config was setup to use SSL port 465 and smtp_ssl=auto. If SSL is not necessary for your SMTP server, simply set smtp_ssl=none.

Once the configuration is setup, make sure to save changes, then exit.

If you are going to use SSL connections, you will need two additional files, available here (as of March 2009) – if this link no longer works, use the contact form and let me know. 

Extract the two compressed files (libeay32.dll and ssleay32.dll) to the same directory as sendmail.exe (for me, C:\xampp\sendmail\)

Now, everything on the sendmail’s end is setup – now we just need to let the server know its there!

Edit your PHP.ini file: 

XAMPP installations often have multiple files. If you are doubtful of the correct one, you can always go through and change them all. But before you do that, change the php.ini in \xampp\apache\bin\.

Look for the following line:



sendmail_path = “C:\xampp\sendmail\sendmail.exe -t”

Uncomment the line (if already commented) by removing the semicolon at the start.

Ensure that the path to sendmail.exe is correct (this is correct for me).

Save and exit php.ini

Restart the apache server and everything should work! 

Comment if you have a question.



The above article is from the forum, but I found a more easier way of doing it.

in the sendmail.ini there is a sample account for hotmail, do the following steps:

  1. Put the concerned value in the configuration, username and password
  2. change the last line of default account to "Hotmail"
  3. comment every option in the [mail_function] of php.ini and keep sendmail_path as uncommented
  4. restart server and start sending emails
  5. You can even send on someone others email id, this is a little problem here and the cause of spam

my php.ini for mail_function:

[mail function]
; For Win32 only.
; http://php.net/smtp
;SMTP = smra.bsnl.in
; 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"

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =

; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = Off

; Log all mail() calls including the full path of the script, line #, to address and headers
mail.log = "C:\xampp\apache\logs\php_mail.log"


my sendmail.ini for the same:


# 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 rewari_19@hotmail.com
auth on
user rewari_19@hotmail.com
password YOURPASSWORD

# Set a default account
account default : Hotmail




Its working for me now. Thank you 

No comments:

Post a Comment