'Sendgrid - Email Formatting

I am new to API's. I have created an API using curl command:

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":"[email protected]","name":"John Doe"}],"subject":"Hello, World!"}],"content": [{"type": "text/plain", "value": "Heya!"}],"from":{"email":"[email protected]","name":"Sam Smith"},"reply_to":{"email":"[email protected]","name":"Sam Smith"}}'

It worked perfectly for me and it sent out the email.

I was wondering, how do I send an email body with html tags and images. Sorry if this is a basic question and asking here.



Solution 1:[1]

In your data that you are sending to the API, you have

"content": [{"type": "text/plain", "value": "Heya!"}]

This is sending your plain text email. To add HTML to this you can add another object to that array with the "type": "text/html".

"content": [{"type": "text/plain", "value": "Heya!"}, { "type": "text/html", value: "<h1>Heya!</h1>" }]

Sending images requires you to base 64 encode the contents of the image and add it to the "attachments" key within the JSON data, along with the filename, content type and content ID if you want to use it for embedding in the email.

"content": [ ... ], "attachments": [{ "content": BASE64_ENCODED_STRING, "type": "image/jpeg", "filename": "catsinhats.jpeg" }]

This blog post has more specifically on how to embed images in emails.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 philnash