Test PHP's Mail Function from Localhost

Test mail() function of PHP on localhost with Windows 8.1

On Windows PHP needs additional configuration.
Especially the SMTP settings are relevant.
http://www.php.net/manual/en/mail.configuration.php

The emulator "test-mail-server-tool" listens on the specified port, normally 25 and writes the mail send there into a specific folder. It takes the place of a real SMTP server, in fact it's a dummy for testing purposes. If the mail is sent, it should be in the specified folder of "tmst".

Complete Walk-Through

  1. install "test-mail-server-tool"
  2. start the tool
  3. go to tray: set port 25 and folder for the email output
  4. create new php file with the source code from the send mail example below
  5. execute the php file (in your browser or on cli)
  6. go to the defined email output folder
  7. find a "*.eml" file with the content of your email

Basic PHP example for sending mail

<?php
$from = "sender@sender.com";
$headers = "From:" . $from;
echo mail ("receiver@receiver.com" ,"headline" , "text", $headers);
?>

PHP : send mail in localhost

It is configured to use localhost:25 for the mail server.

The error message says that it can't connect to localhost:25.

Therefore you have two options:

  1. Install / Properly configure an SMTP server on localhost port 25
  2. Change the configuration to point to some other SMTP server that you can connect to

Send email from localhost running XAMMP in PHP using GMAIL mail server

Here's the link that gives me the answer:

[Install] the "fake sendmail for windows". If you are not using XAMPP you can download it here: http://glob.com.au/sendmail/sendmail.zip

[Modify] the php.ini file to use it (commented out the other lines):

[mail function]
; For Win32 only.
; SMTP = smtp.gmail.com
; smtp_port = 25

; For Win32 only.
; sendmail_from = <e-mail username>@gmail.com

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

(ignore the "Unix only" bit, since we actually are using sendmail)

You then have to configure the "sendmail.ini" file in the directory where sendmail was installed:

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log
auth_username=<username>
auth_password=<password>
force_sender=<e-mail username>@gmail.com

To access a Gmail account protected by 2-factor verification, you will need to create an application-specific password. (source)



Related Topics



Leave a reply



Submit