Posted: 2013-10-31 18:32:20
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…
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 defaults 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 host mail.lan.host port 25 auth on tls off from email@example.com # the user name might be an account username, or your full email address user user.name 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 from firstname.lastname@example.org # 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 user user.name password user.password # Gmail Account account Gmail host smtp.gmail.com port 587 auth on tls on from email@example.com user firstname.lastname@example.org password mypassword # Hotmail Account account Hotmail host smtp.live.com port 587 auth on tls on from email@example.com user firstname.lastname@example.org password mypassword # Yahoo Account account Yahoo host smtp.mail.yahoo.com port 465 auth on tls on tls_starttls off from email@example.com user firstname.lastname@example.org password mypassword # Default account to use account default : LocalMailServer
You’ll need to –
account default : LocalMailServerwith 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 = 127.0.0.1 ;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…
<?php $from = 'email@example.com'; $to = 'firstname.lastname@example.org'; $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.'; else echo 'Error! Mail function failed. Mail NOT sent.'; ?>
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 (smtp.your.domain.name) 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 = 127.0.0.1 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.
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.
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
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
Examples can be found here:
Download and unzip PHPMailer as –
Then start using it:
<?php require('C:/WampDeveloper/Tools/PHPMailer/PHPMailerAutoload.php'); $mail = new PHPMailer; ... ?>
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.