agusgambina
agusgambina

Reputation: 6669

Send emails with attachments in salesforce via sendgrid

I am trying to generate a pdf and attach to an email and send it. I am using salesforce and sendgrid. I am able to send emails but the problem is when I try to attach the pdf, the email is with the attachment, but this is broken, the file is not empty, but the pdf says is broken (I think is a problem of conversion) here is the code

    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    PageReference pdf = Page.include_attachment;
  pdf.getParameters().put('id', 'xxxxxxxxxxxxxxxxxx');
    blob fileBody = pdf.getContentAsPDF();
    efa.setBody(fileBody);
    efa.setFileName('test.pdf');
    efa.setContentType('application/pdf;charset=UTF-8;');
    send('[email protected]','Test','Body Test', fileBody);

I tryied using

pdf.getContentAsPDF();

and

pdf.getContent();

but the result is the same.

Send method

public static void send(String emailRecipient, String emailSubject, String emailBody, Blob att){
    Boolean success=true;

    //construct the body of the request
    String requestBody='';
    requestBody += 'to='+EncodingUtil.urlEncode(emailRecipient,'UTF-8');
    requestBody += '&from='+EncodingUtil.urlEncode(user.Email,'UTF-8');
    requestBody += '&fromname='+EncodingUtil.urlEncode(user.Name,'UTF-8');
    requestBody += '&subject='+EncodingUtil.urlEncode(emailSubject,'UTF-8');
    requestBody += '&text='+EncodingUtil.urlEncode(emailBody.trim(),'UTF-8');
    requestBody += '&html='+EncodingUtil.urlEncode(emailBody.trim(),'UTF-8');
    requestBody += '&api_user=xxxxx';
    requestBody += '&api_key=xxxxx';
    requestBody += '&files[attachment.pdf]=@'+ EncodingUtil.base64Encode(att);

    //construct request
    HttpRequest req = new HttpRequest();
    req.setEndpoint('https://sendgrid.com/api/mail.send.json');
    req.setMethod('POST');
    req.setBody(requestBody);
    try{
        //send request
        Http h = new Http();
        HttpResponse res = h.send(req);
        //check response
        String status = res.getStatus();
        if(status.toUpperCase()=='OK'){
            success=true;
        }
        else{
            success=false;
        }
    }
    catch(Exception e){
        success=false;
    }
}

Thank you

Upvotes: 0

Views: 2196

Answers (1)

scottmotte
scottmotte

Reputation: 634

There is now a library/devtoolkit to do this for you. At the time of this writing it is in beta, but it is working. Disclaimer: I work at SendGrid and have just recently developed the library.

sendgrid-apex

You can rewrite your code to:

...
Blob att = pdf.getContentAsPDF();
public static void send(String emailRecipient, String emailSubject, String emailBody, Blob att){

  SendGrid sendgrid = new SendGrid('username', 'password');

  SendGrid.email email = new SendGrid.Email();
  email.addTo(emailRecipient);
  email.setFrom(user.Email);
  email.setFromName(user.Name);
  email.setSubject(emailSubject);
  email.setText(emailBody.trim());
  email.setHtml(emailBody.trim());
  email.addAttachmentStream("attachment.pdf", att);

  String response = sendgrid.send(email);
}

Upvotes: 3

Related Questions