{"id":17524,"date":"2026-04-02T12:07:37","date_gmt":"2026-04-02T06:37:37","guid":{"rendered":"https:\/\/www.skynats.com\/blog\/?p=17524"},"modified":"2026-04-02T12:07:39","modified_gmt":"2026-04-02T06:37:39","slug":"how-to-test-sendgrid-smtp-using-a-php-script","status":"publish","type":"post","link":"https:\/\/www.skynats.com\/blog\/how-to-test-sendgrid-smtp-using-a-php-script\/","title":{"rendered":"How to Test SendGrid SMTP Using a PHP Script"},"content":{"rendered":"\n<h2 class=\"wp-block-heading has-small-font-size\" id=\"h-introduction\"><strong>Introduction<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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\u2019s why it\u2019s essential to verify that your email setup works reliably before deploying to production.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Services like Twilio SendGrid help simplify email delivery, but proper testing ensures everything is configured correctly. In this blog, we\u2019ll walk through a simple way to test SendGrid SMTP using PHP.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-small-font-size\"><strong>Prerequisites<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before you begin, make sure you have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PHP installed<\/li>\n\n\n\n<li>Composer installed<\/li>\n\n\n\n<li>A SendGrid account and SMTP credentials.<br><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading has-small-font-size\"><strong>Steps to Test SMTP<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading has-small-font-size\"><strong>1. Create a project<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">With the prerequisites completed, create a project folder for this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir send_mail\n\ncd send_mail<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then initialize your project. For that if you are using SendGrid SDK, run the following Composer command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>composer require sendgrid\/sendgrid<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-small-font-size\"><strong>2. Write the PHP Script<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;re now ready to write some code to send your first email. Start by creating a new <a href=\"https:\/\/www.php.net\/\" type=\"link\" id=\"https:\/\/www.php.net\/\" target=\"_blank\" rel=\"noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-secondary-color\">PHP<\/mark><\/a> file named send_mail.php in the root of your project directory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\ndeclare(strict_types=1);\n\nrequire 'vendor\/autoload.php';\n\nuse \\SendGrid\\Mail\\Mail;\n\n$email = new Mail();\n\/\/ Replace the email address and name with your verified sender\n$email-&gt;setFrom(\n    'sender@domain.com',\n    'Example Sender'\n);\n$email-&gt;setSubject('Sending a Test mail with SendGrid is Fun');\n\/\/ Replace the email address and name with your recipient\n$email-&gt;addTo(\n    'recipient@domain.com',\n    'Example Recipient'\n);\n$email-&gt;addContent(\n    'text\/html',\n    '&lt;strong&gt;and fast with the PHP SDK.&lt;\/strong&gt;'\n);\n$sendgrid = new \\SendGrid('SENDGRID_API_KEY');\ntry {\n    $response = $sendgrid-&gt;send($email);\n    printf(\"Response status: %d\\n\\n\", $response-&gt;statusCode());\n\n    $headers = array_filter($response-&gt;headers());\n    echo \"Response Headers\\n\\n\";\n    foreach ($headers as $header) {\n        echo '- ' . $header . \"\\n\";\n    }\n} catch (Exception $e) {\n    echo 'Caught exception: '. $e-&gt;getMessage() .\"\\n\";\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Update your PHP script with your specified details:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Recipient email<\/li>\n\n\n\n<li>Sender email<\/li>\n\n\n\n<li>Password: Your SendGrid API Key<\/li>\n\n\n\n<li>Any messages you want to test<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading has-small-font-size\"><strong>3. Configure SMTP Settings<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Update your env file with SendGrid SMTP details:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Host: smtp.sendgrid.net<\/li>\n\n\n\n<li>Username: apikey<\/li>\n\n\n\n<li>Password: Your SendGrid API Key<\/li>\n\n\n\n<li>Port: 587\/467\/2525<\/li>\n\n\n\n<li>Mail encryption: tls<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading has-small-font-size\"><strong>4. Run the Script<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Run via terminal:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php test_smtp.php<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-small-font-size\"><strong>5. Verify Output<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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&#8217;t see the email, check your spam folder.<\/p>\n\n\n\n<h4 class=\"wp-block-heading has-small-font-size\"><strong>Conclusion<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Testing SendGrid SMTP with PHP is straightforward using PHPMailer. While SendGrid\u2019s official quickstart is helpful, a simple SMTP script\u2014like the one generated by Google Gemini\u2014can be more practical for quick testing and troubleshooting. Once verified, you can confidently integrate email functionality into your application.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you&#8217;re facing issues while performing a <strong>SendGrid SMTP test PHP<\/strong> 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 <a href=\"https:\/\/www.skynats.com\/upcloud-management\" type=\"link\" id=\"https:\/\/www.skynats.com\/upcloud-management\">Managed Cloud Services<\/a> provide end-to-end support\u2014from setup to ongoing monitoring\u2014so you can focus on your business while we handle the technical complexities. Get in touch with us today for fast, reliable support!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2019s why it\u2019s essential to verify that your email setup works reliably before deploying to production. Services [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1275,260,1274,302,1273],"class_list":["post-17524","post","type-post","status-publish","format-standard","hentry","category-blog","tag-managed-cloud-services","tag-php","tag-sendgrid-sdk","tag-server-management-services","tag-smtp"],"_links":{"self":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/17524","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/comments?post=17524"}],"version-history":[{"count":2,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/17524\/revisions"}],"predecessor-version":[{"id":17526,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/17524\/revisions\/17526"}],"wp:attachment":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/media?parent=17524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/categories?post=17524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/tags?post=17524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}