Nodemailer.js Data URI attachment - node.js

I am trying to send a DataURI attachment via nodemailer
This is my code:
var mailOptions = {
from: 'Testing' <test#test.com>',
to: recipient,
bcc: 'test2#test.com',
subject: 'Testing Attachment functionality',
attachments: [
{
filename: 'attachment',
filePath: dataURI
},
],
html: '<p> Check the attachment</p>'
}
I receive a mail with the attachment, but it is a blank file the size of some bytes. For example, if I send the DataURI of a PNG file, I get a DAT file in my mailbox.
Has anybody encountered this issue?

It seems that my code was wrong. I updated to the most recent version of Nodemailer (v1+) and used the following code:
attachments: [
{
path: dataURI
}
]

Related

cannot send pdf attachement using nodemailler

I am using nodemailer to send email with pdf attachment, the email is sent successfully, but the pdf attachment is broken when downloaded through the email. Following is my mailOptions:
let mailOptions = {
from: 'writingtest#email.xxx.com', // sender address mailfrom must be same with the user
to: receiveEmail, // list of receivers,use , to split
cc:'service#xxx.com', // copy for receivers
subject: 'writing test from xxx.com', // Subject line
text: 'Hello world', // plaintext body
replyTo: 'service#xxx.com', //send reply email address
attachments: [
{
filename: 'test.pdf',
filePath: '../../files/test.pdf',
contentType: 'application/pdf',
}
],
};
And the info received from callback is:
message sent: 250 Data Ok: queued as freedom ###envid=19950767140

Sending base64 encoded PDF through Nodemailer using PDFkit

I have a function that returns me a base64 encode PDF, and I would like to send this as an attachement PDF file using nodemailer.
Regarding the nodemailer documentation, I found this example:
const mailOptions = {
from: 'email1#gmail.com', // sender address
to: 'email2#gmail.com', // list of receivers
subject: 'Simulation', // Subject line
html: '<p>SALUT</p>', // plain text body
filename: 'file.pdf',
attachments: [
content: Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD/' +
'//+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4U' +
'g9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC',
'base64'
),
cid: 'note#example.com' // should be as unique as possible
},
However, this did not work for me. Am i missing something ?
Ok, it was all a formatting issue.
Here is how to use B64 on Nodemailer:
const mailOptions = {
from: 'email1#gmail.com', // sender address
to: 'email2#gmail.com', // list of receivers
subject: "Hey this is a subject example", //subject
attachments: [
{
filename: `myfile.pdf`,
content: 'THISISAB64STRING',
encoding: 'base64',
},
],
};
then just send it the classic way.

Sending an array of attachments with node.js

I need to send an email using nodemailer with several attachments, but the number of those attachments must be defined by the size of an array. I know that nodemailer can send more than one attachment, but I have no idea how to send a variable number of attachments.
Here my code:
const files = get(req, "body.data.files");
files.forEach(file => {
senderMail.send(file, {
name: get(req, "body.data.files.name"),
url: get(req, "body.data.files.url")
});
});
let mailOptions = {
from: "Me", //
sender address
to: data.personal.user_email, // list of receivers
subject:
"An email with attachments"
text: "someText",
html: "",
attachments: [
{
filename: name,
path: url
}
]
};
Some data is obtained from a JSON.
Prepare an array in the Nodemailer format and then attach it to the mail object.
const files = get(req, "body.data.files");
const attachments = files.map((file)=>{
return { filename: file.name, path: file.url };
});
let mailOptions = {
from: "Me", //
sender address
to: data.personal.user_email, // list of receivers
subject:
"An email with attachments"
text: "someText",
html: "",
attachments: attachments
};

Send attachment with PDF extension but its not working

I am trying to create a dynamic file and send that to the user in the attachment but after downloading it's not opening.
Showing an error saying "Failed to load PDF
In content, I am sending the required data.
Here is my code
router.get('/file',function(req, res){
var filename1='invoice.pdf';
filename1 = encodeURIComponent(filename1);
var mailOptions={
to: 'userMail#gmail.com',
subject: 'Speaker Added',
from: "admin#gmail.com",
headers: {
"X-Laziness-level": 1000,
"charset" : 'UTF-8'
},
html: '<p style="color:#0079c1;">Hello'+' '+'My Name'+'</p></br>'
+'<p style="color:#0079c1;">Thank you for choosing HOWDY.<p></br>'
+'<p>Click on the link below to activate your account and get redirected to HOWDY</p></br>',
attachments: [
{
'filename':filename1,
'content': 'data',
'contentType':'application/pdf',
'contentDisposition':'attachment'
}
]
}
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'admin#gmail.com',
pass: 'admin56789'
}
});
transporter.sendMail(mailOptions, function(error, response){
if(error){
return res.send(error);
}
else{
res.send({
state:'success',
message:"Send"
});
}
});
})
"
You are sending "data" as content of the file. If you right-click your file, and choose open with notepad (or basic text editor) you'll see.
Nodemailer's attachments expect to receive the data of the file if you use the content option. You might prefer to use path which can be either the filepath or a URL.
Read more here.
Your attachment:
attachments: [
{
'filename':filename1,
'content': 'data',
'contentType':'application/pdf',
'contentDisposition':'attachment'
}
]
Should be closer to:
attachments: [
{
'filename':filename1,
//Either get filecontent
'content': filecontent,
//Or the file full path
'path':filepath,
'contentType':'application/pdf',
'contentDisposition':'attachment'
}
]

My path of attachments in nodeMailer is not working (Sails app)

I'n building a web app by using Sails framework.
I am unable to attach an image in email.
Here is my mail options,
var mailOptions = {
from: 'mymail#gmail.com',
to: to,
subject: subject,
html: html,
attachments: [{
filename: "login-logo.png",
filePath: "./assets/images/login-logo.png",
cid: "logo-mail"
}]
};
and my ejs where I attach an image,
<img src="cid:logo-mail" />
At filePath I try
filePath: "./assets/images/login-logo.png"
filePath: "/assets/images/login-logo.png"
filePath: "/images/login-logo.png"
filePath: "./images/login-logo.png"
But didn't work.
I don't know how to fix it. I really need some help.
Thanks.
I had the same problem and this works for me:
var mailOptions = {
from: 'mymail#gmail.com',
to: to,
subject: subject,
html: html,
attachments: [{
filename: 'Logo.png',
filePath: 'assets/images/Logo.png',
cid: 'logo'
}]
}
I used nodemailer 0.7.1

Resources