I am unable to send attachments. In the email, I see the attachment with the correct name, but when I click on it to open, it says, "Failed to load PDF document".
I have followed the step in demo examples.
` var attachment = new helper.Attachment();
var file = fs.readFileSync('/home/vcap/app/common/models/scc.txt');
var base64File = new Buffer(file).toString('base64');
attachment.setContent(base64File);
attachment.setType("application/pdf")
attachment.setFilename("TheBeast.pdf")
attachment.setDisposition("attachment")
mail.addAttachment(attachment);`
Hi @mevillep,
Based on the extension (scc.txt), it may be that the file is not a PDF. Can you please double check? Thanks!
With Best Regards,
Elmer
Thanks for your prompt reply. Let me try that.
attachment.setContent('myBase64FileHere');
attachment.setFilename("TheBeast.pdf");
attachment.setDisposition("attachment");
attachment.setType('application/pdf');
mail.addAttachment(attachment);
I am trying to put a base 64 file as an argument in the first line. For some reason I am unable to work it out. Anything that I am doing wrong?
How are you generating the base64 encoding?
So, I have a base64 file in my database. And I am querying it to be able to send as an attachment.
It looks something like this, "data:application/pdf;base64,myActualBAse64File". And It doesn't work.
Now, when I use only "myActualBAse64File" like attachment.setContent('myActualBAse64File')
It does not work.
I am not sure what wrong I am doing here.
I also used some online tool to convert text to base64, and use that. Still no luck.
Can you please try removing the header (this part: data:application/pdf;base64,) and try again? Thanks!
Yeahh, I tried that too. I had removed it and tired, It had still not worked.
like >> attachment.setContent('myActualBAse64File');
Can you please send the file you are having trouble with to [email protected]?
HI @mevillep,
I just sent you an email with my findings, please let me know how it goes. Thanks!
Hi @thinkingserious
I am facing exact same issue with attaching PDFs in email.
The email I get has the attachments but its just a blank page, not the file I sent.
Also I checked the contents of attachments, it has some gibberish which does not open with a PDF reader.
Here is sample code for reference. Can you help me with this ?
var helper = require('sendgrid').mail;
var fileUrl = " A s3 File URL for PDF file"
var filename = "filename.pdf"
var mail = new helper.Mail(); // Main mail object
// Do a ton of stuff with mail object.
// fetch attachment file from s3 and add it to mail object.
S3.get(fileUrl, function(err, attach) {
if (err || !attach)
return callback(err);
if (!err && attach) {
var att = new helper.Attachment()
// encode content of file in base64. required by SG.
var encodedString = new Buffer(attach).toString('base64');
att.setContent(encodedString);
att.setType("application/pdf")
att.setFilename(filename);
att.setDisposition("attachment");
mail.addAttachment(att);
var toSend = mail.toJSON();
// send the mail object to send function.
// sg_send(sendgrid, toSend, callback);
}
});
Hi @harsh5692,
Can you please send us ([email protected]) the original file and the base64 excoded string. Please send the latter in a .txt file.
Usually the issue is improper base64 encoding.
With Best Regards,
Elmer
Here is what worked for me:
mkdir nodejs
cd nodejs
mkdir node_modules
npm install --save sendgrid
cp ~/Downloads/your.pdf .
vi temp.js
node temp.js
Following is the contents of temp.js:
var helper = require('sendgrid').mail;
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
var fs = require('fs');
var mail = new helper.Mail();
var email = new helper.Email('[email protected]', 'Example User');
mail.setFrom(email);
mail.setSubject('Hello World from the SendGrid Node.js Library');
var personalization = new helper.Personalization();
email = new helper.Email('[email protected]', 'Example User');
personalization.addTo(email);
mail.addPersonalization(personalization);
var content = new helper.Content('text/html', '<html><body>some text here</body></html>')
mail.addContent(content);
var attachment = new helper.Attachment();
var file = fs.readFileSync('your.pdf');
var base64File = new Buffer(file).toString('base64');
attachment.setContent(base64File);
attachment.setType('application/pdf');
attachment.setFilename('my_file.pdf');
attachment.setDisposition('attachment');
mail.addAttachment(attachment);
var request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON(),
});
sg.API(request, function(err, response) {
console.log(response.statusCode);
console.log(response.body);
console.log(response.headers);
});
I'm guessing your original .pdf is not being loaded correctly.
I am facing the same issue as @harsh5692 is getting
Hello @Abdul993,
Do you have a code sample you can share?
Thanks!
With Best Regards,
Elmer
Evening, I am also struggling with this, my attachments come through with correct name and bytesize but wont open, prompted with "damage file / incorrectly decoded".
Here is my code:
const msg = {
to: req.body.sendEmail,
from: req.body.email,
subject: 'Lets attach it',
html: output,
attachments: [
{
content: new Buffer(req.body.filetoSync).toString('base64'),
filename: req.body.filetoSync,
type: 'plain/text',
disposition: 'attachment',
contentId: 'mytext'
},
],
};
I have the same problem: attachment sent via email but when I tried to open it - appeared notification, which said that the file is broken. C# code;
var attachment = new[] { Attachment.FromStream(file, "Certificate of analisys.pdf" ) };
I've tested method Attachment.FromLocalFile and used worked local pdf file. When I opened it from email attachment - it's turned broken. I'm pretty sure that it's a bug from strongrid side.
Most helpful comment
Here is what worked for me:
Following is the contents of temp.js:
I'm guessing your original .pdf is not being loaded correctly.