Sending emails with PHP on cheap hosting

Many cheap or free share hosting services block the PHP mail() function so it silently does nothing. For example, I just added a contact form to a small WordPress based web site for a friend but it didn’t work as the web hoster, xtreemhost, block it (or everything gets heavily spam filtered, which amounts to the same thing).

The solution is to use a separate SMTP mail server and this is actually quite simple using one of the most popular PHP mail solutions PHPMailer and a Gmail or Google Apps email account. Most SMTP servers will block such so called “relaying” when sending from different domain to the mail server. However, if you have a GMail account you can use the Google SMTP server authenticated as that user.

In my case, I just added a new account to our family domain running on GApps and used that. I added the minimum PHPMailer files required (class.phpmailer.phpclass.smtp.php and PHPMailerAutoload.php) along with a child theme with my new mail() function in the functions.php file. Well, actually, I found the PHP override/replace functions for built-ins didn’t work so I had to edit the plugin code to use a new mail function explicitly, rather than just redefining it in the child theme. Not ideal, but an acceptable maintenance weak point.

The PHPMailer examples include one for using GMail SMTP.

This entry was posted in development, opensource, PHP, web, WordPress. Bookmark the permalink.
Skip to top

Comments

Leave a Reply

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