Enable Dark Mode!
how-to-send-email-using-sendgrid.jpg
By: Shabeer PT

How to Send Email Using SendGrid

Technical

Email can be sent from one server to another quickly and easily using the Simple Mail Transfer Protocol (SMTP). SendGrid offers an SMTP solution that enables you to distribute email through SendGrid's servers as opposed to a client or server of your own. This means that for your SMTP requirements, you can rely on SendGrid's delivery at scale. 

To send Emails using SendGrid, we need a SendGrid key that can be purchased from the official website of SendGrid. 

You need to include the following elements in your API call:

A HOST

Web API v3 request always uses the host https://api.sendgrid.com.https://api.sendgrid.com/v3/

An AUTHORISATION HEADER

The authorization header must include An API KEY. You are able to use API Keys in addition to your account login and password to authenticate. Your account is made more secure with the addition of API Keys, which can be given particular rights to restrict access to certain portions of your account. You can generate API keys in your account. You must set a plain text header named "Authorization" with the value "Bearer XXXX," where XXXX is your API Secret Key, in order to use keys.

A REQUEST

Your payload must be submitted as JSON when you POST or PUT data to a resource.

To Send an email using the SendGrid API:

Note that the overall message size shouldn't be greater than 20MB. This includes the body of the message, any headers, and the total size of any attached files.

Copy the below code and paste it into the terminal. In the data section, specify the "to", "from", and "reply to" names and email addresses, and enter a subject and messages to be sent. If the API Key is authorized, The mail will be sent to the receiver's email id with the given subject and message.

curl --request POST \ 
--url https://api.sendgrid.com/v3/mail/send \ 
--header 'Authorization: Bearer <<YOUR_API_KEY>>' \ 
--header 'Content-Type: application/json' \ 
--data'{"personalizations":[{"to":[{"email":"<< RECEIVER_EMAIL_ID >>","name":"<< RECEIVER_NAME >>"}],"subject":"<< SUBJECT >>"}],"content": [{"type": "text/plain", "value": "<< MESSAGE >>"}],"from":{"email":"<<SENDER_EMAIL_ID>>","name":"<<SENDER_NAME>>"},"reply_to":{"email":"<<REPLY_TO_EMAIL_ID>>","name":"REPLY_TO_NAME"}}'
We can send emails to multiple receivers by adding multiple email ids and corresponding names to the ‘to’ in personalizations. We can also add carbon copy ( CC ) and blind carbon copy ( bcc ) to the emails. To do so,  add ‘cc’ and ‘bcc’ to the personalizations as given below.
curl --request POST \ 
--url https://api.sendgrid.com/v3/mail/send \ 
--header 'Authorization: Bearer <<YOUR_API_KEY>>' \ 
--header 'Content-Type: application/json' \ 
--data'{"personalizations":[{"to":[{"email":"<< RECEIVER_EMAIL_ID_1 >>","name":"<< RECEIVER_NAME_1 >>"},{"email":"<< RECEIVER_EMAIL_ID_2 >>","name":"<< RECEIVER_NAME_2 >>"},{"email":"<< RECEIVER_EMAIL_ID_3 >>","name":"<< RECEIVER_NAME_3 >>"},{"email":"<< RECEIVER_EMAIL_ID_4 >>","name":"<< RECEIVER_NAME_4 >>"}], "cc":[{"email":"<< CC_EMAIL_ID >>","name":"<< CC_NAME >>"}], "bcc":[{"email":"<< BCC_EMAIL_ID >>","name":"<< BCC_NAME >>"}], "subject":"<< SUBJECT >>"}],"content": [{"type": "text/plain", "value": "<< MESSAGE >>"}],"from":{"email":"<<SENDER_EMAIL_ID>>","name":"<<SENDER_NAME>>"},"reply_to":{"email":"<<REPLY_TO_EMAIL_ID>>","name":"REPLY_TO_NAME"}}'
To send emails using Postman API Platform, set API methode, provide the sendgrid url (https://api.sendgrid.com/v3/).In the Header, provide the ‘Authorization’and ‘Content-Type’ as keys, The Sendgrid API, and the ‘application/json’ as values. Add the data part of the given code in the body section and change the content type to JSON.  By clicking the send button, the mail will be sent and the response will be shown.
We also can send the email using the python file by the code given below. 
def send_mail(self):
   url = "https://api.sendgrid.com/v3/mail/send"
   payload = json.dumps({
       "personalizations": [
           {
               "to": [{
                    "email":"<< RECEIVER_EMAIL_ID >>",
                    "name":"<< RECEIVER_NAME >>"
                   }],
               "subject": "Hello, World"
           }
       ],
       "content": [
           {
               "type": "text/plain",
               "value": '<Message content>'
           }
       ],
       "from": {
               "email":"<<SENDER_EMAIL_ID>>", 
               "name":"<<SENDER_NAME>>"},
   })
   headers = {
       'Authorization': "Bearer " + '<api_key>' + "",
       'Content-Type': 'application/json'
   }
   response = requests.request("POST", url, headers=headers, 
                   data=payload)
To add carbon copy ( CC ) and blind carbon copy ( bcc ) to the emails. To do so,  add ‘cc’ and ‘bcc’ to the personalizations as given below.
"cc":[{"email":"<< CC_EMAIL_ID >>","name":"<< CC_NAME >>"}], "bcc":[{"email":"<< BCC_EMAIL_ID >>","name":"<< BCC_NAME >>"}]

SendGrid is a cloud-based email platform. So using SendGrid can decrease the server load and it will make email communication more efficient.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message