Last active
January 28, 2023 15:22
-
-
Save keithweaver/08b588d4674d5451e44527cbde99e886 to your computer and use it in GitHub Desktop.
Send an email with PHP using Sendgrid (Mail Server)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// You need to install the sendgrid client library so run: composer require sendgrid/sendgrid | |
require '/vendor/autoload.php'; | |
// contains a variable called: $API_KEY that is the API Key. | |
// You need this API_KEY created on the Sendgrid website. | |
include_once('./credentials.php'); | |
$FROM_EMAIL = 'YOUR_EMAIL'; | |
// they dont like when it comes from @gmail, prefers business emails | |
$TO_EMAIL = 'THE_PERSON_YOUR_WANT_TO_CONTACT'; | |
// Try to be nice. Take a look at the anti spam laws. In most cases, you must | |
// have an unsubscribe. You also cannot be misleading. | |
$subject = "YOUR_SUBJECT"; | |
$from = new SendGrid\Email(null, $FROM_EMAIL); | |
$to = new SendGrid\Email(null, $TO_EMAIL); | |
$htmlContent = ''; | |
// Create Sendgrid content | |
$content = new SendGrid\Content("text/html",$htmlContent); | |
// Create a mail object | |
$mail = new SendGrid\Mail($from, $subject, $to, $content); | |
$sg = new \SendGrid($API_KEY); | |
$response = $sg->client->mail()->send()->post($mail); | |
if ($response->statusCode() == 202) { | |
// Successfully sent | |
echo 'done'; | |
} else { | |
echo 'false'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I also get the same error
Fatal error: Uncaught Error: Class 'SendGrid\Email' not found in C:\wamp64\www\sendgrid_demo\index.php on line 19
Error: Class 'SendGrid\Email' not found in C:\wamp64\www\sendgrid_demo\index.php on line 19
I have install SDK from composer using
composer require sendgrid/sendgrid
on the root folder of my project (https://prnt.sc/rf44hj)`<?php
?>`