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.
Related
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
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 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 :)
I'm using nodemailer to send email with embedded images, but the images doesn't show up in message body in some email client app (eg thunderbird).
I'm suspecting this happened because of Content-Transfer-Encoding is set to quoted-printable. So it add 3D characters (which is encoding for = character) in src property of img element:
<img src=3D"cid:61767c3c-7f99-4f0f-a15b-b5edc0f0c2c4#emailaddress.com">
How to turn off quoted-printable encoding permanently in nodemailer ? I've tried to set textEncoding: 'base64' in message options, but it seems nodemailer ignore it.
let message = {
from: {
name: 'Someone',
address: 'someone#emailaddress.com'
},
to: {
name: sender,
address: emailTo.toLowerCase()
},
subject: 'Purchased Tickets',
html: 'Some text<br><img src="cid:61767c3c-7f99-4f0f-a15b-b5edc0f0c2c4#emailaddress.com"/><br>Some more text<br><img src="cid:1a419f12-1205-49e5-b0f7-fd407c0bfa27#emailaddress.com"/><br>',
attachments: ticketList.map((t, i) => ({
filename: `ticket${i + 1}.png`,
content: t.qrCode,
cid: `${t.ticketNumber}#emailaddress.com`
})),
encoding: 'base64',
textEncoding: 'base64'
}
transporter.sendMail(message, (err, info) => {
if (err) {
console.log(err)
} else {
console.log(info)
}
})
I have the same problem in some of my emails, but it does not appear to be related to Content-Transfert-Encoding.
Indeed, the images are displayed on some of my mails with
Content-Transfer-Encoding: quoted-printable
but not all...
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
}
]