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.




Sunday 24 September 2017

Remove non printable and non UTF8 characters url encoded json string in PHP/C#

I have been working on an API (Invitation) which is used to invite external users to a web application. According to invitation API, user gets an invitation where he receives an email with a hyperlink for setup the account and confirmation as shown below.

Invitation Email

The above Get started link contains some necessary details of the invited user which is encrypted and encoded. Once the link is clicked it will redirect to another page where all the decode and decryption process will be done. Sample url encoded json is shown below

https://testdemo.com/entry/Form.html?lPz2ijUnk5wmYMzUYdB40XJie0lkntq9aloNbpyF%2Bp9Hb2FIpMW1aQb9CO9avq0BE5YNjeDuU%2B

Now let's come to decode process. Most of us have experienced with a tricky problem when it comes to json decode. Once the decryption is done u will see there are some non printable characters appended with json which won't allow json string to be decoded.

json with special characters

In order to fix this issue in PHP, once the decryption is done we can remove the special characters using below code.

Code for removing special characters
Once we remove the special characters now we can decode without any problem. Likewise, we can remove the special characters in C# using below code.

string output = Regex.Replace(input, @"[^\u0009^\u000A^\u000D^\u0020-\u007E]", "*");
  • ^\u0009 means if not TAB
  • ^\u000A means if not Line Feed
  • ^\u000D means if not Carriage Return
  • ^\u0020-\u007E means if not fall into space to ~
refer ascii table if you like to make changes, remember it would strip off each non ascii character.

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 ( ...