Node.js Nodemailer does not generate attachment content - javascript

I'm trying to automate emails with nodemailer. The problem is that I can't get attachments to work. Here's a sample script
var mailOptions = {
from: 'apples#gmail.com', // sender address
to: newClient.a22, // list of receivers
cc: managerEmail, // list of receivers
bcc: financeEmail, // list of receivers
subject: newClient.a4 + ' |' + monthofService +' | Revision ID: '+revisionId, // Subject line
html: printoutfile.replace(/(\n)/g, '<br>'),
attachments : [ { // utf-8 string as an attachment
fileName: 'check.txt',
contents: 'checking that some attachments work...'
}]
};
Everything works except for the attachment parts. Even the filename of the mailer
Any help would be appreciated, thanks

Its content not contents. Check the sample code on the nodemailer wesite http://www.nodemailer.com/
var mailOptions = {
from: 'apples#gmail.com', // sender address
to: newClient.a22, // list of receivers
cc: managerEmail, // list of receivers
bcc: financeEmail, // list of receivers
subject: newClient.a4 + ' |' + monthofService +' | Revision ID:'+revisionId, // Subject line
html: printoutfile.replace(/(\n)/g, '<br>'),
attachments : [ { // utf-8 string as an attachment
fileName: 'check.txt',
content: 'checking that some attachments work...'
}]
};

Related

Use Mailgun to send a pdf attachment from Base64 string

I have a pdf that is being generated by another function, which returns a Base64 string. I would like to then attach it to a Mailgun email as attachment, which is built into Meteor and Mailgun. I see that there is a lot of examples of attaching a file from the file system, but I don't see anything using Base64.
I have a method that generates a Base64 string and concatonates with prefix in order to convert Base64 to PDF:
//returns base64 string: looks like "YW55IGNhcm5hbCBwbGVhc3VyZQ=="
const base64AttachmentString = 'data:application/pdf;base64,' + generatePdfBase64();
import { Email } from "meteor/email";
Email.send({
to: "email#example.com",
from: "John Smith <johnsmith#example.com>",
subject: "Sending Base64 as PDF",
html: generatedHTMLTemplate,
attachment: base64AttachmentString
});
Is there a way to send a Base64 attachment where Mailgun will recognize it as a PDF? I know this is possible with other mailers such as Nodemailer and SendGrid.
It seems like meteor's Email requires you to add the attachments keys, which should be an array of attachments.
As for the options for the attachments - there are multiple:
{ // 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/andris9/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='
}
Specifically in your example you can use:
const base64AttachmentString = 'data:application/pdf;base64,' + generatePdfBase64();
import { Email } from "meteor/email";
Email.send({
to: "email#example.com",
from: "John Smith <johnsmith#example.com>",
subject: "Sending Base64 as PDF",
html: generatedHTMLTemplate,
attachments: [
{
path: base64AttachmentString
}
]
});

Batch emailing with Mailgun and Node.js

I'm using the Mailgun node.js module to send batch emails out.
According to the big yellow warning message in the official docs, specifying recipient vars should result in sending "each recipient an individual email with only their email in the to field."
However, my recipients can see all of the "to" addresses. I am successfully using recipient-vars to set the email subject for users, so it does appear that these are being read in correctly.
Note that I am sending an HTML email using MIME. I tried this with the more straight-forward plain text variation, and it did appear to work.
Is anyone able to help me understand why my recipients can see all other recipients? Am I doing something wrong, or does this functionality not work for Mailgun MIME emails?
// recipients
var recipients = ['email1#email1.com', 'email2#email2.com', 'email3#email3.com'];
var recipientVars = {
'email1#email1.com': {
id: 1,
subject: 'Subject 1'
},
'email2#email2.com': {
id: 2,
subject: 'Subject 2'
},
'email3#email3.com': {
id: 3,
subject: 'Subject 3'
}
};
// options
var options = {
from: 'Me <me#me.com>',
to: recipients,
'recipient-variables': recipientVars,
subject: '%recipient.subject%',
text: myMailText,
html: myMailHTML,
headers: {
'X-Mailgun-Recipient-Variables': JSON.stringify(recipientVars)
}
};
// create mail
var mail = new nodemailer(options);
// send mail
mail.compile().build((err, message) => {
var mailData = {
to: recipients,
message: message.toString('ascii'),
'recipient-variables': recipientVars
};
mailgun.messages().sendMime(mailData, (err, res) => {
console.log(res);
});
});
Seems this functionality doesn't work with sendMime() method, but it does with regular send() method even without any mail compilation. Here is working code snippet:
const mailgun = require('mailgun-js')({
apiKey: 'api_key',
domain: 'domain'
});
const recipients = ['email1#gmail.com', 'email2#gmail.com'];
const recipientVars = {
'email1#gmail.com': {
id: 1,
subject: 'Subject 1',
name: 'Name 1'
},
'email2#gmail.com': {
id: 2,
subject: 'Subject 2',
name: 'Name 2'
}
};
const envelope = {
from: 'Sender <sender#gmail.com>',
to: recipients,
subject: '%recipient.subject%',
html: 'Hey <strong>%recipient.name%<strong>',
'recipient-variables': recipientVars,
};
mailgun.messages().send(envelope, function (error, body) {
console.log(body);
});
As you would notice all placeholders are populated and <strong> tag properly rendered.

email is going empty when attaching an image with it

let message = {
to: toEmail,
from: liveCampaign.emailFromAddress,
subject: liveCampaign.emailSubjectTemplate,
text: "hi",
html: '<img src="content_id:myimagecid" alt = "graphics">',
files: [
{
filename: "Christmas2.png",
content: ("image" | Buffer),
content_id: "myimagecid",
contentType: "image/png"
}
]
};
i have modified my code but still not able to send the mail .i am getting response error from sendgrid, please somebody help me can somebody provide a syntax of email with attachment in sendgrid

Hyperlink not shown in gmail with email composer

I am trying to send html as a body with email composer. html body contains hyperlink but it is not working in gmail. Gmail display plain text only.
var email = {
to: sendersemail+';',
cc: '',
bcc: '',
attachments: [cordova.file.externalApplicationStorageDirectory+'Report.pdf'],
subject: '',
body: '<p>the brain out your I think you'll find it helpful too.</p>',
isHtml: true
};
$cordovaEmailComposer.open(email).then(null, function () {
// user cancelled email
});
use html instead of body!
var email = {
to: sendersemail+';',
cc: '',
bcc: '',
attachments: [cordova.file.externalApplicationStorageDirectory+'Report.pdf'],
subject: '',
html: '<p>the brain out your I think you'll find it helpful too.</p>'
};
$cordovaEmailComposer.open(email).then(null, function () {
// user cancelled email
});

dpd-email / nodemailer wont attatch file

Trying to att. a file using dpd-email (its using nodemailer)
by examples in nodemailer (https://github.com/andris9/Nodemailer#attachments)
This just gives me a file of 'attachment-1.bin' 0kb of size
tried using diffrent post examples from the nodemailer site, but same result.
Using chrome postman
http://localhost:99/email
to:"asa#me.me"
subject: "test"
text: "test"
attachments: [
{filename: "test.tx"', content:"hello world", contentType:"text/plain"}
]
In NodeMailer, attachments is an array of attachment objects. In your above code snippet though, you're setting attachments to an object instead of an array.
Taken directly from E-mail Message Fields
attachments - An array of attachment objects
Change
attachments: {
filename: 'text.bin',
content: 'hello world!',
contentType: 'text/plain'
}
To
attachments: [
{
filename: 'text.bin',
content: 'hello world!',
contentType: 'text/plain'
}
]

Categories

Resources