codeinprogress
codeinprogress

Reputation: 3501

Applying styling to html email in SendGrid Web API

I am sending email from using SendGrid's Web API using POST. The HTML email body has styling associated with it.

Screenshot of the email body

String body = 'api_user=username&api_key=pwd&to[][email protected]&subject=Message from SendGrid&html={text}&[email protected]';

    body = body.replace('{text}', emailBody);
    HttpRequest req = new HttpRequest();
    req.setEndpoint('https://api.sendgrid.com/api/mail.send.json');
    req.setMethod('POST');
    req.setbody(body);
    Http http = new Http();
    HTTPResponse response = http.send(req);

However when the email is received, it has no body, none at all. If I remove the inline CSS from the p and div the body is rendered. How do I keep the styling intact in the HTML body? Is there any other way?

Upvotes: 3

Views: 1656

Answers (1)

Justin Steele
Justin Steele

Reputation: 2250

My hunch is that this is related to the '=' in 'style="'

Try URL encoding emailBody:

body = body.replace('{text}', encodeURIComponent(emailBody));

Upvotes: 2

Related Questions