0

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

4
  • 1
    You might want to contact Scott Motte. He is creating a library for working with SendGrid from Salesforce. What does the body of your send method look like? Commented May 21, 2014 at 0:25
  • Thank you very much for your comment @Daniel , I posted the send method. Commented May 21, 2014 at 0:45
  • 1
    Like Daniel Ballinger said I'm working on a library right now. Daniel's been helping. I don't have the answer for this right now but am working on this today.
    – scottmotte
    Commented May 21, 2014 at 18:12
  • @scottmotte can you please review this question? stackoverflow.com/questions/78072441/…
    – AAN
    Commented Feb 28, 2024 at 6:55

1 Answer 1

3

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);
}
3
  • Thank you very much for your work! I am trying to implement your solution, is working when I attach a txt, but not when I try to generate in Salesforce a pdf, and then attach it (the error I get is BLOB is not a valid UTF-8 string), which makes sense, but maybe there is a way to solve this, I will keep trying to fixit. Thank you. Commented May 22, 2014 at 4:08
  • 1
    Ok, that might be a bug (in my understanding of how to handle blobs). The code causing that error is here. It's trying to convert it toString but not coming back as UTF-8. I've posted it as an issue here and will work on fixing it.
    – scottmotte
    Commented May 22, 2014 at 22:37
  • @scottmotte , is there any solution available for this problem?
    – Hrushi
    Commented Sep 21, 2015 at 7:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.