Sending Mail by Apache, PHP, and Webapps

Apache with PHP is purely a web-server role, and while PHP has a mail() function – it is absolutely useless if not properly configured to connect to a real mail server, and is not even capable of doing authentication (i.e., submitting the user+password login information to the mail server account).

For PHP scripts and webapps (like WordPress, Moodle, Prestashop, Drupal, Joomla) to be able to send out e-mail, PHP must be able to first connect to a mail server or an email account.

Running and setting up a full local mail server can be a complicated task, so here are some simpler options for you –

If you have access to a Gmail, Hotmail, or Yahoo account, or an SMTP server (mail server) that’s set up to handle your domain name, you can get any Apache + PHP environment (or WAMP such as WampDeveloper Pro), to use any of these options to send out emails…

Use “msmtp”

msmtp is a local SMTP application that pretends to be a real SMTP server, but which really just uses your Gmail, Hotmail, or Yahoo account instead; or any SMTP server (mail server) you’ve configured it to use, that it transmit everything to –

* Make sure to get version 1.4.31 or above, and not v1.4.30 due to a bug.
* WampDeveloper Pro v4.2+ already comes with msmtp v1.4.31 (there is no need to download anything).

1. Download and unzip msmtp into folder –
(* C:\WampDeveloper\Tools\msmtp\msmtp.exe should be a valid path afterwards)

2. Create file C:\WampDeveloper\Tools\msmtp\msmtprc.ini and place this configuration inside –

# Default values to use for all accounts
tls_certcheck off
logfile C:/WampDeveloper/Tools/msmtp/msmtplog.txt

# Local Mail Server Account
# This is the simplest example - a mail server on the local network, standard port 25, with no encryption layer
account LocalMailServer
port 25
auth on
tls off
# the user name might be an account username, or your full email address
password user.password

# ISP Mail Server Account
# Some ISPs provide you with access to a mail server
account MyISP
host smtp.myisp.server
#port 465
auth off
#tls on
#tls_starttls off
# the user/password might or might not be required (some ISPs know your IP and give it access automatically)
# the user name might or might not be your ISP provided email address
password user.password

# Gmail Account
account Gmail
port 587
auth on
tls on
password mypassword

# Hotmail Account
account Hotmail
port 587
auth on
tls on
password mypassword 

# Yahoo Account
account Yahoo
port 465
auth on
tls on
tls_starttls off
password mypassword

# Default account to use
account default : LocalMailServer

You’ll need to –

  • Edit the above to update the drive letter WampDeveloper Pro is installed on for the “logfile” line.
  • Update the account settings for the Gmail/Hotmail/Yahoo or SMTP (mail) server/service you are going to use.
  • Update account default : LocalMailServer with the account to use by default.

The above has been set up with a number of default settings for you, and in most cases you’ll just have to update a) your username/email + password, and b) the default account to use.

3. Edit file –

Uncomment (remove the “;”), and update the path (drive letter), on lines –

sendmail_path = "C:/WampDeveloper/Tools/msmtp/msmtp.exe -d -C C:/WampDeveloper/Tools/msmtp/msmtprc.ini -t --read-envelope-from"

mail.log = "C:/WampDeveloper/Logs/Php/maillog.txt"

Comment out “SMTP =” and “smtp_port =“. This way PHP’s mail() function is using the “sendmail” application, and not trying to establish it’s own mail server connection…

;smtp_port = 25

4. Save edited files. Restart Apache for configuration changes to take effect.

Make sure your webapps and/or scripts are configured to use PHP’s mail() function, and not some specific sendmail path or SMTP server. Because this is what “msmtp” is for – getting those mail() calls, matching the “From:” address to one of the above accounts, establishing a connection using the account’s settings, and sending your mail via that account’s mail server.

This might require some testing before it starts to work perfectly, as 1) proper mail server settings are needed and 2) the PHP scripts/code you are using must be also using PHP’s mail() function with the proper Headers (From: xxx) included.

Once an email is sent, you’ll be able to see it in the email service’s “Sent” folder.

Be careful not to specify a fake From: address (which does not match your email address for the service) or your emails are going to be tagged as spam.

You’ll also have two log files that you can check –

5. Send test email…

  $from    = '';
  $to      = 'someone.else@somewhere.else';

  $subject = 'Test Subject';
  $message = 'Hello. Testing email.';
  $headers =
    'From: ' . $from . "\r\n" .
    'Reply-To: ' . $from . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

  if(mail($to, $subject, $message, $headers))
    echo 'Mail function returned successfully. Mail has probably been sent.';
    echo 'Error! Mail function failed. Mail NOT sent.';

Running a Local SMTP server (not recommended)

Some options for SMTP/mail server applications are: hmailserver, mailenable, smartermail, or the native Windows Server SMTP service (* with this one, be careful not to also install and run IIS – which can prevent Apache from binding to port 80 and 443).

You’ll need to first update with your Registrar (domain register – GoDaddy, Namcheap, etc) the DNS records of your mail-server’s domain-names ( and their IP addresses. Your IPs must be static (can’t change from time to time). Otherwise, set up a local SMTP relay.

Configure the SMTP server to do no-authentication localhost access (PHP’s mail() function does not support authentication – it can only access a SMTP server, but it can not login using a username/password).

Keep the default php.ini settings:

smtp_port = 25

Comment out “sendmail_path =“. This way PHP’s mail() function establishes it’s own mail server connection, and doesn’t attempt to use the “sendmail” application.

You might also need to specify a proper “sendmail_from =” setting to match your email address.

Use an External SMTP service

Set it up to provide service for mail of your domain name. Make sure your domain’s DNS records reflect this service’s mail server names and IP addresses.

Then do the same thing as is option A. Or if your SMTP service allows no-authentication login via a white-listing of your IP, then just fill in the php.ini settings as in option B except with the IP and Port this service provides.

Use PHPMailer script

PHPMailer is able to send mail via an SMTP host such as 1) your local or external mail server (gmail), 2) sendmail (and qmail), and 3) PHP’s mail() function.

If using the mail() option, you can integrate PHPMailer with MSMTP (* and also if using the “sendmail” option with some more work).

* This assumes your code/scripts are able to utilize something other than the PHP mail() function.

Examples can be found here:

Download and unzip PHPMailer as –

Then start using it:

$mail = new PHPMailer;

Other Alternatives

Fake Sendmail:

sendmail.exe is a simple windows console application that emulates sendmail's "-t" option to deliver emails piped via stdin.

It is intended to ease running unix code that has /usr/lib/sendmail hardcoded as an email delivery means.

It doesn't support deferred delivery, and requires an smtp server to perform the actual delivery of the messages.

5 thoughts on “Sending Mail by Apache, PHP, and Webapps”

  1. Thanks a lot for this brilliant tutorial. It gave me a lot of help and much needed inspiration when had to set up a Moodle server ( under Win 2012 server (not my favourite OS).

    Two comments:

    (1) Of the various smtp programs, msmtp seems to be the most powerful, best maintained and best documented. One can even probe the smtp server:
    msmtp -S

    A real time saver when you have to find your way around in a corporate network.

    (2) The statement in the php.ini must NOT contain white spaces – use the 8+3 alias instead:

    sendmail_path = "c:/PROGRA~2/msmtp/msmtp.exe -t"

    One can find the alias by typing (on the command line):
    dir /x

    * On my system “c:\PROGRA~2″ points to “c:\Program Files (x86)”.

  2. You show msmtp.exe should be in the folder, but there is no msmtp.exe in the zip file. I can’t find a single executable file on their sourceforge page. How the heck do you run this?????

    1. Thanks for letting me know!

      It looks like they stopped providing builds after 1.6.2, and are now only releasing the source code.

      Link to the last compiled version (1.6.2) –

      Download file –

      To build/compile a higher version, you’ll need to follow these instructions –

      Courtesy of the fixed build failures on mingw64 (thanks!) I have managed, with a great deal of assistance from one of the MXE developers, to get the new msmtp 1.6.6 into the source tree for MXE.

      Those after a Windows 32bit static binary of the new msmtp version can now simply download the latest git MXE and run:

      make msmtp MXE_PLUGIN_DIRS='plugins/apps' MXE_TARGETS='i686-w64-mingw32.static'

      This will build the 32bit binary and there is similar syntax for static 64bit binary:

      A shared build is also possible if that is desired for any reason…

      So the build part of MSMTP is now part of this project –

      1. Thank you, it works now. One small problem though:
        when I actually receive the email, it always says 0 for the subject. This is minor and can be ignored, but it would be great if there was a solution (I’m sending the emails to a Hotmail account).

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>