Send E-Mail messages with PHP using Authenticated SMTP A brief tutorial explaining how to send e-mail messages with PHP using authenticated SMTP: PHPMailer, SendMail, Fake SendMail and other methods

php-cgi.exe - The FastCGI process exited unexpectedly error and how to fix it

The ability to programmatically send e-mail messages with PHP is a feature used by a large amount of opensource software written using the most famous scripting language on the web: from phpBB to WordPress, from Joomla to Drupal, from MantisBT to MediaWiki there is no Forum, CMS, project or collaborative work platform that does not need to communicate with its users by sending e-mails. Not to mention, of course, the needs of any project carried out independently that includes any kind of registration, newsletter or other interaction with users.

The easiest way to send e-mails via PHP is to use the native mail() function, which is available from PHP4 and allows you to resolve everything with a simple method call in the following way:

The configuration parameters relative to the SMTP server to connect to for sending, and its port to use, must be specified in the php.ini file - the main PHP configuration file - in the following way:

The values indicated are the default ones: for additional details on each of them, read here.

An important limitation is immediately evident: there is in fact no way to specify a username and an authentication password for the SMTP server, a condition that is now indispensable in almost all the servers made available to Italian and international providers, as well as absolutely mandatory if we want to make use of a server that only accepts connections protected by secure layer SSL/TLS.

Ultimately, PHP's native functionality allows you to connect only to an SMTP server that allows you to use it without authentication. The question, and the reason why you came across this article, is: how can we solve this problem? In this article we will explore a number of possibilities, each with its own pros and cons.

PHPMailer

If the PHP application's source code has been written or modified by you, the best solution to solve the problem is to stop using the native PHP features offered by the mail() function, replacing it with a more powerful tool, utility or library with additional features: among the many on the market I would like to recommend the excellent PHPMailer, an open-source library that performs very well all the features related to sending e-mails via PHP and that will never make you regret the mail() function.

To install it simply include its classes in your project using Composer/Packagist or, if you prefer the old-fashioned way of doing these things, download the latest version from the GitHub repository and include/require the PHPMailer's Autoload class:

For additional information on installing and using PHPMailer, please refer to the PHPMail official docs.

Sendmail (Linux)

If you have the absolute need to send email via the mail() function - for example because you have one or more software written in PHP that can't do without it at all and/or doesn't have alternative send options - the easiest way is to install SendMail, an open-source program that can be installed on all major Linux distributions with the following command:

and easily configurabile in the php.ini file with the following one-liner:

Once setup, SendMail can be configured using a wizard-like command-line configuration by typing the sudo sendmailconfig  terminal command, or by manually modifying the settings in the /etc/mail/sendmail.conf  configuration file: in both ways you'll be able to specify any credentials for accessing the server, thus overcoming the limitations imposed by the native PHP functionality.

For additional info on the SendMail project, we strongly suggest to take a look at the Sendmail official website.

sSMTP and Postfix

If you’re looking for a simpler alternative to configure than Sendmail, you can also use  sSMTP - a minimalistic Mail Transfer Agent (MTA) which can be installed and configured to act as a drop-in replacement for Sendmail: although having some limitations compared to its big brother, it's definitely easier and faster to setup. To know more about it, you can read a post that we published some months ago explaining how to install and configure sSTMP instead of Sendmail.

Conversely, if you want a better and more powerful alternative, you can install Postfix, which is also a drop-in replacement for Sendmail with additional features and a more secure approach.

Fake Sendmail (Windows)

What if you don't have a Linux-based system? Don't worry, you can use a method similar to the one described in the previous paragraph also on a Windows server thanks to a tool made specifically to emulate the Sendmail functions. His name is Fake Sendmail and is available free of charge at this link. Although the author (Byron Jones) is no longer developing the project, the latest version available (made with Delphi 2007 and dated 2011) works perfectly on all major versions of Windows Server, including Windows Server 2012 R.

To install Fake Sendmail follow the instructions described in the official website: download the latest version of the program and unpack the archive in a folder of your choice (such as  C:\PHP\fake_sendmail\  ): open the sendmail.ini configuration file with a text editor and configure it with the parameters of the SMTP service you want to connect to:

Once this is done, you just have to set the appropriate parameter in the php.ini indicating the path of the executable in the following way:

For more information on the parameter, see the PHP sendmail_path official docs.

IMPORTANT: On Windows 8, 8.1, 10 and Windows Server 2012 platforms, you may need to configure the Fake Sendmail executable to run from the system with administrator privileges. To do this, simply right-click on the sendmail.exe file and, in the Compatibility tab, set the Windows XP Service Pack 3 compatibility mode and activate the Run as Administrator checkbox. Remember to add to the folder's security permissions users IUSR, IIS_IUSRS and/or Network Service - depending on how you have configured IIS - in read and write mode.

Internal SMTP server

If you need to use the native mail() function and do not want to use the method described above, you can always configure a SMTP service on your local environment for internal use, which you can use to send e-mails directly. If you use Windows I can only recommend hMailServer, a great open-source SMTP server for Windows that we have extensively covered in this post.

Regardless of the Mail Server you use, the service must be configured to accept connections without authentication on requests to send from the local IP of your web farm (or, if installed on the same machine that houses the Web Server, from localhost). In this way you will have a system "open" (for PHP) but at the same time secure, as this opening will not affect any access from external IP.

Conclusions

Here ends our overview of possible alternatives to PHP's mail() function: as always, the best choice depends solely on the specific needs of your configuration and the characteristics of the PHP products in operation on your machine. The choice of adopting libraries like PHPMailer is certainly the best when you have control of the source code, while the Sendmail or Fake Sendmail configuration is more suitable if you have installed "fixed" products like WordPress or Joomla. Of course - if you want to have as much control as possible over mailings and receptions - the choice of equipping yourself with an internal SMTP server is arguably the best one, provided that you have the necessary technical knowledge and a sufficiently reliable system.

That's it for now... happy sending!

 

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.