The code starts by loading Composer's autoloader, which makes all of the required classes available to our code.PHPMailer – A full-featured email creation and transfer class for PHP A request (when submitting data to a resource via POST or PUT, you must submit your request body in JSON format).An API key passed in an Authorization Header.A host (the host for Web API v3 requests is always ).The helper library abstracts this away, reducing the code you must write. Your API call must have the following components. $headers = array_filter($response->headers()) Įcho 'Caught exception: '. Printf("Response status: %d\n\n", $response->statusCode()) $sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')) Replace the email address and name with your Recipient' $email->setSubject('Sending with Twilio SendGrid is Fun') We'll break down each piece of this code in the following Sender' You can copy this code to the send_mail.php file, modify the values passed to the setFrom and addTo methods, and run the code to see what happens. The following PHP block contains all the code needed in your send_mail.php file to successfully deliver a message with the Twilio SendGrid Mail Send API. Start by creating a new PHP file named send_mail.php in the root of your project directory. You're now ready to write some code to send your first email. Installing sendgrid/sendgrid (7.9.2): Extracting archiveġ package suggestions were added by new dependencies, use `composer suggest` to see details. Installing sendgrid/php-http-client (3.14.0): Extracting archive Installing starkbank/ecdsa (0.0.4): Extracting archive Downloading sendgrid/php-http-client (3.14.0) Package operations: 3 installs, 0 updates, 0 removals Installing dependencies from lock file (including require-dev) Locking sendgrid/php-http-client (3.14.0) Lock file operations: 3 installs, 0 updates, 0 removals Loading composer repositories with package information Running composer update sendgrid/sendgrid Once your API key is assigned to an environment variable - this quickstart uses SENDGRID_API_KEY - you can proceed to the next step. You can edit the permissions assigned to an API key later to work with additional services. To complete this tutorial, you can create a Restricted Access API key with Mail Send > Full Access permissions only, which will allow you to send email and schedule emails to be sent later. Visit our API Key documentation for instructions on creating an API key and storing an API key in an environment variable. If your API key is compromised, you can also cycle it (delete and create another) without changing your other account credentials. Unlike a username and password - credentials that allow access to your full account - an API key is authorized to perform a limited scope of actions. See the 2FA section of our authentication documentation for instructions. You can enable 2FA with SMS or by using the Authy app. Twilio SendGrid requires customers to enable Two-factor authentication (2FA). For more account options, see our pricing page. When you sign up for a free SendGrid account, you'll be able to send 100 emails per day forever. Skip the prerequisites Sign up for a SendGrid account You can skip ahead if you've already completed these tasks. Prerequisitesīe sure to perform the following prerequisites to complete this tutorial. In this quickstart, you'll learn how to send your first email using PHP and the Twilio SendGrid Mail Send API.
0 Comments
Leave a Reply. |