Monday 25 September 2017

Setup SendGrid email gateway in Azure to send emails

SendGrid is an email API which is used vastly in lots of web applications and other third party APIs in order to send customized mails. SendGrid has lots of customizable features which gives full freedom to developers where developers can add their own email content,email theme,email attachments and so on. Let's see how to integrate SendGrid email API with Azure in detail. First of all, you have to create azure account, go to https://azure.microsoft.com/en-in/free/ to create a free account which can be used for 30 days.

Create Azure Account
Once you are logged in with the azure portal, click New search for SendGrid and create a SendGrid account with mandatory fields as shown below.


Search SendGrid



Create SendGrid Account


Now click on the account you have created,it will open a extended window where you can see the pricing information of your account and other essential details. You are almost come to the end of configuration. Now in order to access the email API with PHP code you need to generate API keys of SendGrid, in order to do that click "Manage" at the top of your SendGrid account. It will redirect to the below page where you can see the overall API requests and lots of other stuffs.

Manage SendGrid


Now select "Settings" at the bottom of the navigation bar to expand the options where you can see the API keys option listed. Select it and create API keys based on your requirements.

Create API Keys
 
Now let's see how to use SendGrid with PHP. First of all download the below folder, unzip and keep it inside your project folder.


Then, require the sendgrid.php file as shown below to access the methods and properties of the SendGrid API.

require("path/to/sendgrid-php/sendgrid-php.php");

Then add the below code to send email via SendGrid API.

$mail = new SendGrid\Mail();
$subject="Test Mail";
$mail->setSubject($subject);
$email = new SendGrid\Email("MyCompany", "no-reply@mycompany.com");
$mail->setFrom($email);
$personalization = new SendGrid\Personalization();
$email = new SendGrid\Email("toMail","tomail@example.com");
$personalization->addTo($email);
$email = new SendGrid\Email("ccMail","ccmail@example.com");
$personalization->addCc($email);
$email = new SendGrid\Email("bccMail","bccmail@example.com");
$personalization->addBcc($email);
$mail->addPersonalization($personalization);
$apiKey = APIKEY;                           
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
echo $response->body();
print_r($response->headers());

Note:
Subject is mandatory to send email using SendGrid API where as Cc, Bcc are optional.

For more options to be added to the mail content such as attachments, images refer the example mentioned below.


For finding errors based on the status code refer the document mentioned below.




No comments:

Post a Comment

Integrating SonarQube with Jenkins for PHP

SonarQube  is a quality management tool popularly used among 85000 companies around the world. It has four editions. Community Edition ( ...