My path of attachments in nodeMailer is not working (Sails app) - node.js

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

Related

NodeJs Buffer is not attached to Nodemailer file

Currently on my project I am dealing with a situation and I feel it is a problem of syntax but I am not getting the right answer and kinda feeling stressed and tilt over it for the last three days.
I am using three libraries NodeJs, Html-pdf(phantomJS) and Nodemailer.
this is the block of code in question (everything is inside a major async function that is a resolver of graphQl)
await pdf.create(html, options).toBuffer(async (err, buffer) => {
//i = new ;
//console.log(typeof(buffer))
await buffer;
await transporter.sendMail({
from: '"Example" example#example.com', // sender address
to: `${a1.dataValues.email}`, // list of receivers
subject: `${text2}`, // Subject line
text: `${text}`, // plain text body
html: `<p> ${text} </p>`, // html body
attachments: [
{
filename: `${a1.dataValues.kitID}.pdf`,
content: buffer,
contentType: "application/pdf",
},
],
});
});
The email is sent but I get this in the browser
The attachment is sent but I guess it is "undefined" and I have been struggling with finding a way to fix this. Can someone please help me?
I tried as well to do
attachments: [
{
filename: `${a1.dataValues.kitID}.pdf`,
content: new buffer.from(buffer),
contentType: "application/pdf",
},
],
attachments: [
{
filename: `${a1.dataValues.kitID}.pdf`,
content: new buffer(buffer),
contentType: "application/pdf",
},
],
attachments: [
{
filename: `${a1.dataValues.kitID}.pdf`,
content: new buffer.from(buffer,'base64'),
contentType: "application/pdf",
},
],
but all of those crash the app in Azure. The all thing works perfectly locally, but not when deployed.

Nodemailer Message Configuration Missing 'From'

So my nodemailer transport is working fine (for the most part) however, I am now trying to embed an image. The issue is now that I have the additional 'attachments' option in my mailOptions object, it is throwing the following error.
Error: 'from' parameter is missing
this is strange since I most definitely have a from parameter. Emails send fine without an attachment. Not sure what I am doing wrong.
var mailOptions = {
from: 'confirmation#donotreply.com',
to: email,
subject: 'Account verification',
html:'<p>Hello,\n\n' + 'Please verify your account by clicking the link' + confirmationLink + '\n\n' + '<img src="uniqueqr#qr.example" alt="something went wrong"/>',
attachments: [{
filename: 'qr.png',
path: '../path/to/my/file/qr.png',
cid: 'uniqueqr#qr.example'
}],
};
Try the following:
const mailOptions = Object.freeze({
from: 'confirmation#donotreply.com',
to: email,
subject: 'Account verification',
html: '<p>Hello</p>,\n\n' + 'Please verify your account by clicking the link' + confirmationLink + '\n\n' + '<img src="cid:uniqueqr#qr.example" alt="something went wrong"/>',
attachments: [{
filename: 'image.png',
path: '/path/to/file',
cid: 'uniqueqr#qr.example'
}]
});
note the cid inside img tag

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.

nodemailer attachment is not working

i want to attach zip file. but it doesn't work any attachment.
here is my source code.
var express = require('express');
var router = express.Router();
var nodemailer = require('nodemailer');
var fs = require('fs');
var mailinfo = require('../config/mail_info').info;
var smtpTransport = nodemailer.createTransport({
host: mailinfo.host,
port: mailinfo.port,
auth: mailinfo.auth,
tls: mailinfo.tls,
debug: true,
});
router.post('/',function(req,res){
var emailsendee = req.body.emailAddress;
console.log(emailsendee);
var emailsubject = "Requested File";
var emailText = "test";
var emailFrom = 'test#test.com';
var mailOptions={
from : "test <test#test.com>",
to : emailsendee,
subject : emailsubject,
html : '<h1>' + emailText+ '</h1>';
attachments : [
{
filename : '',//i just put black make you understand esaily
path : ''//what i did is under this code
}
]
};
console.log(mailOptions);
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
res.end();
}else{
console.log(response);
res.end();
}
});
});
module.exports = router;
i tried these for attaching a file
enter code here
attachments:[{ fileName: 'test.log', streamSource: fs.createReadStream('./test.log'}]
it still sends a mail without attachment.
when this code can't read a file there is a error.
so i guess this isn't working because of reading a file.
and i read some questions on stackoverflow which has similar error with me.
i fixed path -> filepath
and fixed streamSource -> path
my nodemailer version is 4.0.1.
help me send a mail with zip file.
I'm using exactly the same version of nodemailer(4.0.1 at this moment) and I'm sending emails with attachments successfully.
Your first code snippet looks promising :)
But the second part
i tried these for attaching a file
enter code here
attachments:[{ fileName: 'test.log', streamSource: fs.createReadStream('./test.log'}]
doesn't look right at all ...
Please refer to nodemailer docs
fileName and streamSource are not a valid parameters of mailOptions object
EXAMPLE FROM DOCS
var mailOptions = {
...
attachments: [
{ // utf-8 string as an attachment
filename: 'text1.txt',
content: 'hello world!'
},
{ // binary buffer as an attachment
filename: 'text2.txt',
content: new Buffer('hello world!','utf-8')
},
{ // file on disk as an attachment
filename: 'text3.txt',
path: '/path/to/file.txt' // stream this file
},
{ // filename and content type is derived from path
path: '/path/to/file.txt'
},
{ // stream as an attachment
filename: 'text4.txt',
content: fs.createReadStream('file.txt')
},
{ // define custom content type for the attachment
filename: 'text.bin',
content: 'hello world!',
contentType: 'text/plain'
},
{ // use URL as an attachment
filename: 'license.txt',
path: 'https://raw.github.com/nodemailer/nodemailer/master/LICENSE'
},
{ // encoded string as an attachment
filename: 'text1.txt',
content: 'aGVsbG8gd29ybGQh',
encoding: 'base64'
},
{ // data uri as an attachment
path: 'data:text/plain;base64,aGVsbG8gd29ybGQ='
},
{
// use pregenerated MIME node
raw: 'Content-Type: text/plain\r\n' +
'Content-Disposition: attachment;\r\n' +
'\r\n' +
'Hello world!'
}
]
}
as you can see you should change fileName to filename and streamSource to content
// WRONG
attachments:[{ fileName: 'test.log', streamSource: fs.createReadStream('./test.log'}]
// RIGHT
attachments:[{ filename: 'test.log', content: fs.createReadStream('./test.log'}]
Good Luck! I hope this was helpful for you :)

Nodemailer.js Data URI attachment

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
}
]

Resources