Need Assistance?

In only two hours, with an average response time of 15 minutes, our expert will have your problem sorted out.

Server Trouble?

For a single, all-inclusive fee, we guarantee the continuous reliability, safety, and blazing speed of your servers.

How to Test SendGrid SMTP Using a PHP Script

Table of Contents

Introduction

Testing email functionality from your server is an important step in building any web application. Even if your code is correct, issues like incorrect SMTP settings, authentication errors, or server restrictions can prevent emails from being delivered. That’s why it’s essential to verify that your email setup works reliably before deploying to production.

Services like Twilio SendGrid help simplify email delivery, but proper testing ensures everything is configured correctly. In this blog, we’ll walk through a simple way to test SendGrid SMTP using PHP. 

Prerequisites

Before you begin, make sure you have:

  • PHP installed
  • Composer installed
  • A SendGrid account and SMTP credentials.

Steps to Test SMTP

1. Create a project

With the prerequisites completed, create a project folder for this:

mkdir send_mail

cd send_mail

Then initialize your project. For that if you are using SendGrid SDK, run the following Composer command.

composer require sendgrid/sendgrid

2. Write the PHP Script

You’re now ready to write some code to send your first email. Start by creating a new PHP file named send_mail.php in the root of your project directory.

<?php

declare(strict_types=1);

require 'vendor/autoload.php';

use \SendGrid\Mail\Mail;

$email = new Mail();
// Replace the email address and name with your verified sender
$email->setFrom(
    '[email protected]',
    'Example Sender'
);
$email->setSubject('Sending a Test mail with SendGrid is Fun');
// Replace the email address and name with your recipient
$email->addTo(
    '[email protected]',
    'Example Recipient'
);
$email->addContent(
    'text/html',
    '<strong>and fast with the PHP SDK.</strong>'
);
$sendgrid = new \SendGrid('SENDGRID_API_KEY');
try {
    $response = $sendgrid->send($email);
    printf("Response status: %d\n\n", $response->statusCode());

    $headers = array_filter($response->headers());
    echo "Response Headers\n\n";
    foreach ($headers as $header) {
        echo '- ' . $header . "\n";
    }
} catch (Exception $e) {
    echo 'Caught exception: '. $e->getMessage() ."\n";
}

Update your PHP script with your specified details:

  • Recipient email
  • Sender email
  • Password: Your SendGrid API Key
  • Any messages you want to test

3. Configure SMTP Settings

Update your env file with SendGrid SMTP details:

  • Host: smtp.sendgrid.net
  • Username: apikey
  • Password: Your SendGrid API Key
  • Port: 587/467/2525
  • Mail encryption: tls

4. Run the Script

Run via terminal:

php test_smtp.php

5. Verify Output

If a 202 status code iis printed to the console, your message was sent successfully. Check the inbox of the addTo address, and you will see your demo message. If you don’t see the email, check your spam folder.

Conclusion

Testing SendGrid SMTP with PHP is straightforward using PHPMailer. While SendGrid’s official quickstart is helpful, a simple SMTP script—like the one generated by Google Gemini—can be more practical for quick testing and troubleshooting. Once verified, you can confidently integrate email functionality into your application.

If you’re facing issues while performing a SendGrid SMTP test PHP or need expert help to configure and troubleshoot email delivery, our team is here to assist. With deep expertise in SMTP setup, PHP scripting, and reliable email solutions, we ensure your emails are sent securely and efficiently. Our Managed Cloud Services provide end-to-end support—from setup to ongoing monitoring—so you can focus on your business while we handle the technical complexities. Get in touch with us today for fast, reliable support!

Liked!! Share the post.

Get Support right now!

Start server management with our 24x7 monitoring and active support team

Subscribe and get your first issue fixed for Free!

Looking for server support and 24x7 monitoring?

Have doubts? Connect with us now.