5.3-Nano: msmtp
5.x-Nano: Introduction | Cron | DtDNS | Database Backup | msmtp
|
|
Uniform Server and Msmtp an SMTP client
It’s been a long time since I looked at SMTP clients. Generally most use the Windows registry however for portability that’s a no go. I finally narrowed it down to msmtp its flexible, relative easy to set-up more importantly its portable.
The above can be configured to use your ISP’s SMTP server however that again restricts portability. The answer is to create a free account at Google Mail (gmail) and configure msmtp to use that.
This combination allows any PHP scripts to send e-mail to your gmail account transparently. MSMTP has been integrated into Uniform Server all you need to do is configure the account as explained below.
Background
SMTP (simple mail transport protocol) was originally designed to be an open relay where an SMTP server would accept any e-mail for forwarding. This quickly became abused by spammers in retaliation ISP’s restricted open relaying. This means you cannot use the PHP function to directly send e-mail to a user. You either require your own mail server with all the complication that is associated with it or use your ISP’s SMTP server. In either situation you are restricted to a local server on a dedicated line.
Free e-mail accounts such as Google Mail remove these chains by allowing you to relay through their servers. However you must login to their servers before this privilege is granted that’s the price you pay.
That said add the above solution to DtDNS and you have a real portable server.
Configuration
PHP uses the default account as set in configuration file UniServer\msmtp\msmtprc.ini
Before running any PHP scripts that send mail at least one account in this file must be configured and set as default.
The file contains three-example configurations that will suit most requirements.
MyISP
The first account, MyISP replaces the basic sendmail settings that would have been set in the php.ini file.
It assumes your Internet service provider does not require you to authenticate. Hence all that is required is your ISP’s smtp server name and your email address.
account MyISP host smpt.tiscali.co.uk from johm.doe@tiscali.co.uk auth off |
|
To make this account the default
Change line account default : Hotmail to account default : MyISP
Note: Other accounts are ignored. PHP will use this account to send emails and your ISP will relay them to the appropriate address.
I mentioned above this account is restrictive because it ties you to your service provider hence is not portable. However it has one advantage requires minimum of configuration.
Hotmail
The second account, Hotmail is a free account that you need to create at the following address http//www.hotmail.com once signed up you will have an email address and password. These are required to configure your msmtp account.
account Hotmail tls on tls_certcheck off host smtp.live.com from john.doe123@hotmail.co.uk auth on user john.doe123@hotmail.co.uk password fred123 |
This account requires authentication hence your login details:
|
To make this account the default
Line account default : Hotmail remains unchanged
Note: Other accounts are ignored. PHP will use this account to send emails and Hotmail servers will relay them to the appropriate address.
Gmail
The third account, Gmail is a free account that you need to create at the following address http://mail.google.com/ once signed up you will have an email address and password. These are required to configure your msmtp account.
account Gmail tls on port 587 tls_certcheck off host smtp.gmail.com from john.doe777@gmail.com auth on user john.doe777@gmail.com password fred999 |
This account requires authentication hence your login details:
|
To make this account the default
Change line account default : Hotmail to account default : Gmail
Note: Other accounts are ignored. PHP will use this account to send emails and Gmail servers will relay them to the appropriate address.
General Note
I would recommend you sign-up to the above free accounts and try each in tern (change default) to determine which best suits your requirements. You can create any number of accounts however make sure the account name is unique.
Test Scripts
Create the following scripts and save them to folder UniServer\www
mail.php <html> <head><title>PHP Mail Sender</title></head> <body> <?php # Retrieve the form data $email = $_POST['email']; $subject = $_POST['subject']; $message = $_POST['message']; # Check if a $_POST value exists # If not use defaults if (empty($_POST['email'])){ $email = "xxx.xxxx@tiscali.co.uk"; } if (empty($_POST['subject'])){ $subject = "UniCenter test"; } if (empty($_POST['message'])){ $message = "A quick test from UniCenter"; } # Sends mail and report success or failure if (mail($email,$subject,$message)) { echo "<h4>Thank you for sending email</h4>"; } else { echo "<h4>Can't send email to $email</h4>"; } ?> </body> </html> |
; |
mail.html <html> <head><title>Mail sender</title></head> <body> <form action="mail.php" method="POST"> <b>Email</b><br> <input type="text" name="email" size=40> <p><b>Subject</b><br> <input type="text" name="subject" size=40> <p><b>Message</b><br> <textarea cols=40 rows=10 name="message"></textarea> <p><input type="submit" value=" Send "> </form> </body> </html> Test:
|
Integration
You can skip this section however if you are interested in how msmtp integrates into Uniform Server’s architecture read on. This section is useful for users who wish to upgrade when a newer version of msmtp is released.
Upgrade
Download the latest version (currently msmtp-1.4.18-w32.zip) from http://sourceforge.net/projects/msmtp/files/
- Unzip to any folder.
- Copy file: msmtp-1.4.18-w32\msmtp-1.4.18-w32\msmtp.exe
- To folder: UniServer\msmtp
If you are upgrading that’s all there is to do.
How it is integrated
There are three requirements for integration, inform PHP where to find the msmtp executable, inform msmtp where to find it’s configuration file. Finally inform msmtp where it’s log file is to be located.
The configuration file is located in folder UniServer\msmtp and named msmtprc.ini (could have been any other appropriate name) This configuration file contains a path to the log file it is specified as an absolute for example:
logfile "C:/some_folder/UniServer/msmtp/sendmail.log"
Note: When servers are moved this path is automatically updated.
A single line placed in php.ini configuration file caters for the first two requirements. Line has the following format:
sendmail_path = "C:/some_folder/UniServer/msmtp/msmtp.exe --file=C:/ some_folder/UniServer/msmtp/msmtprc.ini -t"
Note 1: Absolute paths with forward slashes are used. The first part instructs PHP where to find the msmtp executable second part instructs msmtp where to find its configuration file.
Note 2: When servers are moved line sendmail_path is automatically updated reflecting the new paths.
There are three php.ini files (php.ini, php.ini_delvelopment_nano and php.ini_production_nano) that require modification, parts of the sendmail section are commented out and the above line added as shown below:
[mail function] ; For Win32 only. ;SMTP = localhost ;smtp_port = 25 ; For Win32 only. ;sendmail_from = me@localhost.com ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ;sendmail_path = "/usr/bin/sendmail.exe -t" sendmail_path = "C:/some_folder/UniServer/msmtp/msmtp.exe --file=C:/some_folder/UniServer/msmtp/msmtprc.ini -t" |
Function run_location_tracker() located in file UniServer\unicon\main\includes\functions.php automatically updates paths in the three php.ini files. However file UniServer\msmtp\msmtprc.ini is new and needs adding to the above function as follows:
if (file_exists($usf_msmtp)) { // Only update if exists file_search_replace($usf_msmtp,$s_str,$base_f); // Update msmpt ini } |
Note: File $usf_msmtp is defined in UniServer\unicon\main\includes\config.inc.php. A user may delete msmtp and use an alternative hence files existence is check first, this prevents error messages.
General note
At first sight msmtp.exe looks large at 2.13 MB however it is self contained and compares favourably with alternatives such as fake send mail sendmail.exe 828 KB requires, libeay32.dll 1.06 MB and ssleay32.dll 208 KB
Summary
For a home web server or small business server the above is a very good compromise.
However for a portable server it is ideal, include this with DtDNS and you can have a portable server on-line within ten minutes with email capability.