this article was originally published on Medium Sending mails is one of the tasks that you would need to do at some point when building a web application in PHP . A quick google search would bring up one of the treasured open source packages for mail sending called PHPMailer . Now, don’t get me wrong .
PHPMailer is very good at what it does as it offers more features than PHP’s default mail() function however it has some shortcomings;
- The mail would have to be sent on your own server(s) thereby using up your resources.
- Some shared hosting providers don’t allow outgoing connections on the port that PHPMailer uses to send emails.
There’s other reasons too but these are the major ones I think affect people the most. Okay then, so what do we use to send our emails ?? Sendgrid !! What’s SendGrid you may ask ?? Okay . Taken straight from their Documentation,
SendGrid is a cloud-based SMTP provider that allows you to send email without having to maintain email servers. SendGrid manages all of the technical details, from scaling the infrastructure to ISP outreach and reputation monitoring to whitelist services and real time analytics. Our goal is to make it as easy as possible to add reliable, scalable email to your application so that you can focus on building the core features of your product.
Hmm, I don’t know about you but that seems pretty sweet . SendGrid would do all of this for us ??
Well yes, SendGrid will and it is why it has more adavantages than PHPMailer . Its faster , more secure and scales with you doing little or no work . The best part about SendGrid is that it is very affordable . Here is an excerpt from their pricing page .
Send up to 40,000 emails for 30 days and 100 emails/day free forever with our trial - no credit card required
They offer a trial plan that lasts for a month and then give you 100 emails per day for free when your trial expires . If you believe that’s too small , then you can purchase a plan . If you would like to find out more , check out their pricing.
Okay, so enough talk . Let’s get to the main business . To send mails via SendGrid , we need to make use of their API . Luckily for us, SendGrid has an awesome PHP library that makes it easy for us to work with their API . The source code for the PHP library can be found here https://github.com/sendgrid/sendgrid-php.
The first step to getting started with SendGrid is to register an account with SendGrid and then obtain our API key which we would use every time we send a request to their API . Assuming we have done the above , lets move on . For the purpose of this tutorial , we would be writing a simple PHP script that sends mail via SendGrid’s API to my email address . First we make the directory we are going to be using for this tutorial and we name it
[email protected]:~$ mkdir sendgrid-test
Next step , we would use composer to install SendGrid’s PHP library in our working directory . If you don’t have composer installed (it is strongly suggested that you use composer for dependency management in PHP) and you would rather download the zip file , clone the repo and follow the instructions in the README file. Using composer to install sendgrid , we enter
composer require sendgrid/sendgrid and we would get the following output .
[email protected]:~/sendgrid-test$ composer require sendgrid/sendgrid Using version ^6.0 for sendgrid/sendgrid ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) - Installing sendgrid/php-http-client (3.7.0) Downloading: 100% - Installing sendgrid/sendgrid (6.0.0) Downloading: 100% Writing lock file Generating autoload files
Now we have installed sendgrid successfully and we can start to use it in our simple project . Next , we create a PHP script called
[email protected]:~/sendgrid-test$ touch mail.php
Since , we are using composer , we would want the script to make use of composer’s autoloading feature hence we would write this at the top of our script
<?php require 'vendor/autoload.php';
Voila ! Now that we have installed sendgrid and have made it available for us to use , let us send a simple mail .
// email address and name the mail should the mail be sent from $from = new SendGrid\Email("Example User", "[email protected]"); //subject of the email $subject = "We are learning about SendGrid "; //recipient of the email $to = new SendGrid\Email("My email", "[email protected]"); //Body of the email $body = " This is how easy it is to send mail via SendGrid "; $content = new SendGrid\Content("text/html", $body); //Send the mail $mail = new SendGrid\Mail($from, $subject, $to, $content); //Your SendGrid API key $apiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"; $sg = new \SendGrid($apiKey); //returns an object that contains the details of the response $response = $sg->client->mail()->send()->post($mail); echo $response->statusCode(); print_r($response->headers()); echo $response->body();
And there we have it . Very simple and straightforward . If you would like to customize how sendgrid sends emails more , you can read up on their SMTP API here https://sendgrid.com/docs/API_Reference/SMTP_API/index.html .
I hope you enjoyed this article ! and I hope we have seen why SendGrid is the preferred method of sending emails using PHP.