Install PHPMailer on Shared Host
In this blog post I am hoping to explain how to install PHPMailer on your shared hosting provider (like Hostgator – which is what I use).
PHPMailer is a free PHP library that is used for sending email, and it makes sending email a lot easier and safer. Properly formatting your email is very complex, and not doing it right can have steep penalties – like being marked as spam and being blacklisted. You should never send email without PHPMailer or something similar unless you really know what you are doing.
There are a few other libraries besides PHPMailer that you can use, but this is the one that I use.
We install PHPMailer to our host with another program called Composer, and to use Composer we need a program called PuTTY which is a SSH client that we install on our own computer to connect with our host.
Install PuTTY
So, the first thing that we will need is a SSH client. Because I use Windows my SSH client of choice is PuTTY which is free and open source, but there are many others available. You can get PuTTY here: http://www.putty.org/
To use PuTTY you will have to set it up to connect to your host, and you will also have to generate a SSH key pair on your host. The process is pretty well documented here: https://www.siteground.com/tutorials/ssh/putty/ but I would like to add two things. The first is that when you are connecting to your sever and it asks you for a password the curser WILL NOT MOVE, blink, or show any signs that you are typing at all. I guess it is a security feature, but it got me hung up for a few minutes and can make you think that the program froze. The second thing is that for my host I need to set the port to 2222 for it to work. The port defaults to 22, and I found several tutorials advising to leave the port set at 22 but when I do I just get an error that says “timed out”. The tutorial I recommend says to set it at 18765 but when I do that I get an error message that says “connection refused”.
Install Composer
Once we have PuTTY installed, configured, and connected to your host, it is time to get Composer. Composer is a free PHP package manager and the process of installing it to your host is detailed here:http://mazedul.com/php/install-composer-on-shared-host You only need up to the part where you check to see if Composer is installed. You do not need to install Laravel.
Install PHPMailer
Once Composer is installed it is finally time to install PHPMailer. Just type this in the command pronpt in PuTTY:
composer require phpmailer/phpmailer
That’s IT! Composer will do the rest. You can look at the open source code and get examples of how to use PHPMailer here: https://github.com/PHPMailer/PHPMailer and we will need to get the first example to set up and test PHPMailer now.
Test Example
Create a .php file called “mailtest.php” and copy the contents of the first example to it. Now, simply change the default values in the example to your own values. I was able to get all of the values from my host control panel, in the email section.
Once you get all of your own values changed upload it to your host for testing. Type the path to the file (ex. yoursite.com/mailtest.php). Since the example has verbose output set to 2 we get quite a lot of output when we check the file, but down the bottom it should say “message has been sent”, check your email to verify (if you sent it to yourself).
If you are getting errors or the mail did not send I had to change these two things:
First is to make sure the “require” path is correct. Mine had to be changed to:
require '../../vendor/autoload.php';
Next is to change this line:
$mail = new PHPMailer(true);
to
$mail = new \PHPMailer;
I put a slightly altered version of the example on my Github that reflects these changes if you just want to copy it and change the values:
You should now have a working email system that is far superior to using the simple un-secure PHP mail() function, and you also have Composer and PuTTY giving you the ability to easily install lots more useful libraries for your projects.
If you have any problems let me know, I would also be glad to do this all for you for a very reasonable price – just send me a message at ed@32hertz.com