Nodemailer - how send Uint8Array like picture attachment - javascript

I have picture in "Uint8arrary" and I would like to send this in email attachment.
In my js:
attachments: [{
content: fileInfo.fileContent, // Uint8arrary
filename: 'project',
encoding: 'binary'
}]
But this doesn't work.

Related

Nodejs embedded image showing image in letter correctly but also as attachment

I am sending an email with nodejs trying to embed logo.png through CID to my letter but image show in letter correctly but also as attachment I don't want it appearing as attachment just in letter
let mailConfig = {
from: Testing,
html: doL,
subject: Testing,
to: email,
attachments: [
{ // file on disk as an attachment
filename: 'logo.png',
path: 'logo.png',
cid: 'logo',
}
]
};
<img src="cid:logo" height="50" width="180">

Datatables Editor node.js - Custom file validator - Get height and with of img file

I'm using datatables editor with node.js. I need a customer file validator: https://editor.datatables.net/manual/nodejs/upload#Custom-validation
Is there a way to get height and width of a uploaded img file? I want to validate height x width geometric.
.validator(
Validate.fileExtensions(
['png', 'jpg'],
'Only image files can be uploaded (png and jpg)'
)
)
.validator( file => {
console.log('file:');
console.log(file);
return true;
})
console:
{ uuid: 'fsdfe20sdfsdf1',
field: 'upload',
file:
'/tmp/express-busboy/fsdfe20sdfsdf1/upload/logo.jpg',
filename: 'logo.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
truncated: 'false',
done: 'true',
size: 16453,
extn: 'jpg',
name: 'logo' }

HTML2PDF with Nodemailer

Trying to send a generated PDF from eKoopman's HTML2PDF.js to send as an email attachment-- but can't get the resulting PDF to display. Have tried a lot of the solutions on Stack Overflow and elsewhere, but many tend to be outdated.
HTML2PDF:
html2pdf().set(opt).from(iac).toPdf().output('datauristring')
.then(function(pdf) {
emailDoc(pdf)
})
Attempts at the relevant parts of the mail options from emailDoc():
attachments: [{
filename: 'Name.pdf',
content: Buffer.from(pdf).toString('base64')
}]
attachments: [{
filename: 'Name.pdf',
content: Buffer.from(pdf).toString('base64'),
contentType: 'application/pdf'
}]
attachments: [{
filename: 'Name.pdf',
content: new Buffer(pdf, 'base64'),
contentType: 'application/pdf'
}]
attachments: [{
filename: 'Name.pdf',
content: pdf,
encoding: 'base64'
}]
Doesn't necessarily have to be the base64 method, just any combination that will make this work!
You may try to save your pdf first, then upload it using path description like,
attachments: [{
filename: 'file.pdf',
path: '/path/to/file',
contentType: 'application/pdf'
}],
Hope it helps!

how to send email with .xlsx attachment in sendgrid?

Below is the message object used to send emails.
message = {
to: toEmail,
from: emailInfo.emailFromAddress,
subject: emailInfo.emailSubjectTemplate,
attachments: [
{
filename: fileName,
content: base64str,
contentId: fileName,
disposition: "attachment"
}
],
html: emailMessageBodyTemplate
};
The content is encoded into a base64 string by the following below code.
const base64_encode = file => {
var bitmap = fs.readFileSync(file);
return new Buffer(bitmap).toString("base64");
};
I don't know where I m going wrong but I'm getting the error as follows.
message:"The content value must be a string at least one character in length."
but the content is not empty when I debug it is a base64 string.
Please help.
On this page it describes exactly your error.
I believe in this error content means your message or a text as error describes
You may not send an email with no content.
And as per the API docs,you are missing a required parameter content.
message = {
to: toEmail,
from: emailInfo.emailFromAddress,
subject: emailInfo.emailSubjectTemplate,
content:[
{
type : 'string',
value : 'message'
}
],
attachments: [
{
filename: fileName,
content: base64str,
contentId: fileName,
disposition: "attachment"
}
],
html: emailMessageBodyTemplate
};
Hope this helps.
I've encountered the same issue and in my case I used 'xlsx' npm lib, to implement the solution as follow:
const workbook = XLSX.utils.book_new();
const ws = XLSX.utils.aoa_to_sheet(data);
XLSX.utils.book_append_sheet(workbook, ws, 'Accounts');
// write the file in base64 format
const report = XLSX.write(workbook, { type: 'base64', compression: true });
const attachment = {
content: report,
filename: `MyReport.xlsx`,
type: 'text/html',
disposition: 'attachment'
};

Emails with attachments come up empty using Nodemailer

I'm trying to send an e-mail with an attachment using Nodemailer. No matter what I do, if I specify an attachments property, the e-mail comes up empty (no attachments, no html, no text). Sending an e-mail without any attachments works as expected. Here's the code I have so far:
transporter.sendMail({
to: `${toTitleCase(i.nome)} <${i.email}>`,
subject: 'Treinamentos',
text: 'hello!',
html: `Embedded image: <img src="cid:nyan#example.com"/>`,
attachments: [
{
filename: 'newimg.jpg',
path: __dirname + '/src/img/newimg.jpg',
cid: 'nyan#example.com'
}
]
}, (err, info )=> {
console.log(err);
console.log(info);
});
I have also tried using content instead of path, same result. I should also note that the err callback is empty.
I figured it out. I had set 'Content-type': 'text/html; charset=UTF-8' as the header on the transporter object. Removing that line fixed the issue.

Categories