Alternative to PHPMailer: SendGrid PHP API

Introduction

In this post, I’m going to show you an amazing alternative to PHPMailer: the SendGrid PHP API.

I know. We all love PHPMailer to send emails through PHP.

It’s free and superb.

But the problem is that it requires so many things to configure and it can become very frustrating for those who are just getting started. If you are interested in finding a more simple alternative to PHPMailer, then this article is for you.

No worries there will be zero SMTP configuration steps in this guide. This makes it ideal for those who don’t want to set up a Gmail two-factor authentication for getting their App Password in Gmail SMTP. However, if you want to try PHPMailer, then check out our guide to using PHPMailer in 2022.

And just you know I like to work with SendGrid. This is just a personal choice. No promotion or anything. And in the future, I will bring more such guides with different services for PHP send mail functionality.


Why SendGrid API PHP example?

You might be wondering why I am going with Sendgrid PHP API, not its SMTP configuration. The answer is simple: Sendgrid Web API is super easy and beginner friendly to use. And by the end of this article, you will understand this statement.

The next reason to use Sendgrid is they offer you 100 free emails per day. Which is awesome for testing and even for deploying a PHP mail service on a small website. And you can upgrade if you want in the future.

Lastly why I recommend Sendgrid for beginners because you can easily configure and use it in “Localhost” of your Laptop or PC with no hassle at all.

Yup! You don’t need to dive deep into the PHP.ini file of Xampp or Wamp. Our SendGrid API PHP example will work with your preferred PHP localhost seamlessly. Zero headaches I promise!

So let’s jump into this exciting process and send PHP mails:


Step 1: SignUp with SendGrid and get the API key

Before you run away at the name of an API key, let me tell you generating an API key is way too easy. And I will guide you through the whole process. Let’s go:

  • 1st: Do simple signup at Sendgrid.Com and provide basic info as asked. They will ask for email verification so do complete it. And you mostly need to do two-factor authentication as well.
  • After verification, you get into the dashboard of SendGrid and it will look like this:
  • Create a Sender and do its verification.
  • Now go to your Setting tab and click on it. Here you will see the API Keys option. Go there and then generate your first API Key. They will ask you to give it a name.

Step 2: Setting Files for PHP Mail

Once you have the API key, go to the Email API tab and click on Integration Guide. SendGrid will ask you which Programming Langauge you will use. Choose PHP and you get redirected to a page where all info is available.

Note: I’m using localhost in this case but on a live environment, the process is the same as well.

First, we created the “yourkey.php” file in our folder. You can name this file anything but keep it noted

<?php
define("APIKEY", "Your API KEY Will Be HERE");

Next, we build our logic file that will process the PHP send mail:

I am naming this file: “sendemail.php“.

<?php
$emailto = $_POST['email'];

require 'yourkey.php';
require 'vendor/autoload.php';

$email = new \SendGrid\Mail\Mail(); 
$email->setFrom("Sender Email-id in SendGrid", "Sender Name in SendGrid");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("$emailto", "Recipient");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
    "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid(APIKEY);
echo "<pre>";
try {
    $response = $sendgrid->send($email);
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '. $e->getMessage() ."\n";
}

Now finally we link this logic file to our UI. I’m as usual using Bootstrap 5 for a simple design that only takes a recipient email.

Our “index.html” file.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Send Emails with Sendgrid Web API</title>
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>
    .card {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      padding: 2rem;
    }
  </style>
</head>

<body>
  <br>
  <div class="container">

    <div class="card">
      <h1>Send Email</h1>
      <div class="card-body">
        <form action="sendemail.php" method="post">
          <div class="mb-3">
            <label class="form-label">Enter Recipient Email address</label>
            <input type="email" class="form-control" name="email">
          </div>
          <button type="submit" class="btn btn-primary">Send Now</button>
        </form>
      </div>
    </div>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>

Step 3: Run and see the magic!

Truly now that we have prepared all files, just run the file in localhost and see how our script of PHP send mail works like a charm.

You can also download our code from GitHub by clicking the below button:


Conclusion

Finally, we hope in this blog post we have shown enough to help you make use of SendGrid PHP API.

Unlike PHPMailer, SendGrid offers many other benefits, including the ability to send large emails and track opens and clicks through their mail clients. With today’s article, you should have learned another easy method to send PHP mail. And this method works superbly with your PHP localhost as well.

You can also check our earlier post on how to use APIs with 2 examples. Where we build a weather web app as well. Ta-da! Friends see you soon! Keep coding!

Leave a Comment