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'
};
Related
Iam able to generate a csv file with the data below. I am using a nodejs library "csv-writer" that generates the file quite well. My problem is that I need a way to return back a buffer instead of the file itself. Reason being I need to upload the file to a remote server via sftp.
How do I go ab bout modifying this piece of code to enable buffer response? Thanks.
...
const csvWriter = createCsvWriter({
path: 'AuthHistoryReport.csv',
header: [
{id: 'NAME', title: 'msg_datetime_date'},
{id: 'AGE', title: 'msg_datetime'}
]
});
var rows = [
{ NAME: "Paul", AGE:21 },
{ NAME: "Charles", AGE:28 },
{ NAME: "Teresa", AGE:27 },
];
csvWriter
.writeRecords(rows)
.then(() => {
console.log('The CSV file was written successfully');
});
...
Read your own file with fs.readFile('AuthHistoryReport.csv', data => ... );. If you don't specify an encoding, then the returned data is a buffer, not a string.
fs.readFile('AuthHistoryReport.csv', 'utf8', data => ... ); Returns a string
fs.readFile('AuthHistoryReport.csv', data => ... ); Returns a buffer
Nodejs file system #fs.readFile
You need to store your created file in a buffer using the native package fs
const fs = require('fs');
const buffer = fs.readFileSync('AuthHistoryReport.csv');
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!
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.
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.
I have a file with name "file.csv", this file have data below:
ID Full name
1 Steve
2 John
3 nam
4 Hạnh
5 Thủy
I use segment code below to parse this file to json file. But my results is not utf8
Code:
var fastCsv = require("fast-csv");
var fs = require("fs");
var iconv = require('iconv-lite');
var fileStream = fs.createReadStream("file.csv");
fastCsv
.fromStream(fileStream, {headers : ["id", "full_name"]})
.on("data", function(data){
console.log("------------------------");
console.log("data: ", data);
})
.on("end", function(){
console.log("done");
});
Results:
data: { id: '��I\u0000D\u0000', full_name: '\u0000F\u0000u\u0000l\u0000l\u0000 \u0000n\u0000a\u0000m\u0000e\u0000' }
data: { id: '\u00001\u0000',full_name: '\u0000S\u0000t\u0000e\u0000v\u0000e\u0000' }
data: { id: '\u00002\u0000',full_name: '\u0000J\u0000o\u0000h\u0000n\u0000' }
data: { id: '\u00003\u0000',full_name: '\u0000n\u0000a\u0000m\u0000' }
data: { id: '\u00004\u0000', full_name: '\u0000H\u0000�\u001en\u0000h\u0000' }
data: { id: '\u00005\u0000',full_name: '\u0000T\u0000h\u0000�\u001ey\u0000' }
data: { id: '\u0000', full_name: '' }
How to convert my result to utf8?
Your input file is encoded in UTF-16LE, but it has been read as if it were UTF-8.
Try opening the file with fs.createReadStream('file.csv', {encoding: 'utf-16le'}).
Take a look at Javascript Has a Unicode Problem
In your case you need to decode the escaped unicode chars. A library included with node called punycode can handle this.
Import punycode via:
var punycode = require("punycode");
Change:
console.log("firstName: ", data);
To:
console.log("firstName: ", punycode.ucs2.decode(data));
You might have to break down the data object further to decode it's properties but I can't tell from your answer what their structure is.