title: Send emails with attachments in salesforce via sendgrid
body: <p>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</p>
  
  <pre><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('xxxxx@xxxx.com','Test','Body Test', fileBody);
  </code></pre>
  
  <p>I tryied using </p>
  
  <pre><code>pdf.getContentAsPDF();
  </code></pre>
  
  <p>and</p>
  
  <pre><code>pdf.getContent();
  </code></pre>
  
  <p>but the result is the same.</p>
  
  <p>Send method</p>
  
  <pre><code>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 += '&amp;from='+EncodingUtil.urlEncode(user.Email,'UTF-8');
      requestBody += '&amp;fromname='+EncodingUtil.urlEncode(user.Name,'UTF-8');
      requestBody += '&amp;subject='+EncodingUtil.urlEncode(emailSubject,'UTF-8');
      requestBody += '&amp;text='+EncodingUtil.urlEncode(emailBody.trim(),'UTF-8');
      requestBody += '&amp;html='+EncodingUtil.urlEncode(emailBody.trim(),'UTF-8');
      requestBody += '&amp;api_user=xxxxx';
      requestBody += '&amp;api_key=xxxxx';
      requestBody += '&amp;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;
      }
  }
  </code></pre>
  
  <p>Thank you</p>