简体   繁体   中英

Sending an file via email through Parse cloud module Sendgird

I am trying to send a file through email using Parse.com 's JS cloud cloud, utilizing Sendgrid. The file exists, it queries it right, it just doesn't attach it to the email

var theFile = object.get("file")
var sendgrid = require("sendgrid");
sendgrid.initialize("***", "**");
var email = sendgrid.Email({to: ['pat.doyle95@gmail.com']});
email.setFrom('guy@email.com');
email.setSubject('Payroll');
email.setText("This is the payroll file for yesterday \n \n" + theFile.url())
console.log(theFile)
email.addFile(theFile.name(), theFile).then(function(e) {
    console.log("In file add");
    console.log(e);
    console.log("this is filename " + theFile.name());
});
sendgrid.sendEmail(email);

The file exists, and it shows in the email that I receive as well. Although I can still get the file through the URL, I want to attach it in the email.

The params to addFile are incorrect, try this:

email.addFile({
  filename: theFile.name(),
  url: theFile.url()
});

So nobody else has to go through this huge hassle. The addFile() function is called synchronously and calls addFileFromBuffer() which means that if you send the email right after you add the file then the email will send before the attachment has been created. Call sendEmail() inside the function of addFile().

var theFile = object.get("file")
var sendgrid = require("sendgrid");
sendgrid.initialize("***", "**");
var email = sendgrid.Email({to: ['pat.doyle95@gmail.com']});
email.setFrom('guy@email.com');
email.setSubject('Payroll');
email.setText("This is the payroll file for yesterday \n \n" + theFile.url())
console.log(theFile)
email.addFile(theFile.name(), theFile).then(function(e) {
    console.log("In file add");
    console.log(e);
    console.log("this is filename " + theFile.name());
    sendgrid.sendEmail(email);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM