Send attachment via SendGrid using Parse Cloud Code Module

I am trying to send an attachment to my email using SendGrid, in this case the attachment is of type NSData. Is there a way to send an attachment using SendGrid without having an URL or that image saved in Parse? I want to go from phone straight to email w/ attachment.

Currently the email is sending successfully, just do not have an image/attachment. Thanks in advance!

Parse.Cloud.define("sendBookRequestEmail", function(request, response) {
        var Buffer = require('buffer').Buffer;
        var buffer1 = new Buffer(request.params.image);
        var b3 = buffer1.toString('base64');
       var SendGrid = require("sendgrid");  
       SendGrid.initialize("username", "password");

       SendGrid.sendEmail({
          to: "email",
          from: request.params.email,
          subject: "Requesting book",
          text: "Title: " + request.params.title + "\r\n" + "Author: " + request.params.author + "\r\n" + "ISBN: " + request.params.isbn + "\r\n" + "I want to: " + request.params.bookrequest + "\r\n" + "Notes: " + request.params.notes,
          attachments: [request.params.image]
        }, {
          success: function(httpResponse) {
            response.success("success");
             console.log(httpResponse);
          },
          error: function(httpResponse) {
             console.error(httpResponse);
          }
      });
});

Answers:

Answer

The object that you were passing to the sendMail call does not have the right structure. Try something like this:

sendEmail({
        to: "email",
        from: request.params.email,
        subject: "subject",
        text: "text",
        attachments: [{
          content: b3, // 'Some base 64 encoded attachment content'
          filename: 'some-attachment.txt',
          type: 'plain/text',
          disposition: 'attachment',
          contentId: 'mytext'
   }
 ],
});

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.