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
Related
I have a form posting to /submit-form route and I need to pass req.body.revenue value to be used in the email template that goes out after the form submit.
const email = req.body.email;
const revenue = req.body.revenue;
// Prepare the nodemailer
let transporter = nodemailer.createTransport({
host: 'smtp.mailgun.org',
port: 587,
secure: false,
tls: { cipers: 'SSLv3' },
auth: {
user: 'postmaster#sandbox.mailgun.org',
pass: 'xyz',
}
});
// Prepare the email data
const data = {
from: 'Excited User <me#samples.mailgun.org>',
to: email,
subject: 'Hello Pixel, Where those cookies at?',
text: 'Gonna load the pixel next in the HTML',
html: { path: 'pixel-tracker-email.html' }
};
// Send the mail via Nodemailer
transporter.sendMail(data)
.then(msg => console.log(msg))
.catch(err => console.log(err));
console.log('Email sent via Nodemailer!');
I have pug set up for my email template and have tried changing the html value to something like this:
// Prepare the email data
const data = {
from: 'Excited User <me#samples.mailgun.org>',
to: email,
subject: 'Hello Pixel, Where those cookies at?',
text: 'Gonna load the pixel next in the HTML',
html: var html = pug.renderFile('path/to/email-template.pug', {revenue});
};
And then I'm using #{revenue} in the actual email-template.pug file but the value isn't coming through.
I ended up figuring it out like this
html: pug.renderFile(__dirname + '/views/tracking.pug', { email: email, revenue: revenue })
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.
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
};
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
}
]
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