Sending PDF/Docx byte array as email attachment - node.js

DocuSign is calling our webhook upon signing of documents. These documents are then stored in a database. They are stored as they are received from DocuSign as PDF bytes which are base64binary encoded. Another server then calls this server to retrieve the documents. I then send the documents as an attachment via nodemailer. After opening the attachment it does not render/decode properly. It displays as the encoded base64 data and not readable text. Can someone kindly point out what I'm doing wrong?
The response body looks like this:
[ { Name: [ 'name-of-doc.docx' ], PDFBytes: [ 'JVBERi0xLjUKJfv8/f4KJVdy ... '] } ]
Here's the code:
request(options, function (err, response) {
var mailOptions = {
from: 'email#domain.com',
to: 'email#domain.com',
subject: "Test",
html: "This is a test. See attached.",
attachments:[{
filename: JSON.parse(response.body).pdfs[0].Name,
content: JSON.parse(response.body).pdfs[0].PDFBytes[0],
encoding: 'base64'
// also have tried new Buffer(...PDFBytes[0], 'base64') in content
}]
};
transporter.sendMail(mailOptions);
});

Sounds like a possible double encoding problem.
I suggest that you decode the pdf when you receive it. The base64 encoding used by Connect is just to transport the pdf from the connect system to your app.
Then, encode it again, if needed, for transportation via email.
For email, why not send it as a pdf attachment? (Vs an encoded format?)
I suggest you write a new stack overflow question of "how do I send an email with a pdf attachment using nodemailer"
(Now that I re-read your question, I guess that you're trying to focus on the nodemailer issues. The docusignapi tag tripped me up. You may want to remove that tag since your question is not about DocuSign.)

Related

Email PDF attachment content missing image

I call Gmail API using a nodejs script using the request library (not the google one).
When I write the attachment response into a pdf file, I can see that the content is incomplete (missing images, missing styles, etc...).
Code:
const attachment: IGoogleMailUserMessageAttachmentResource = await this.call(
'GET',
`/${message.id}/attachments/${part.body.attachmentId}`,
);
writeFileSync('/tmp/test.pdf', attachment.content, { flag: 'w+' });
Received file :
instead of :
Any idea ?
Thanks

How to transform a multipart/related gmail content to html or pdf

I'm using https://github.com/google/google-api-nodejs-client to get a list of threads/messages from my gmail account
I've got a multipart/related as the payload (body) of my email, how can I transform it to html or pdf?
Is there a better easy to get the content of gmail ?
I got the message like this:
const full = await gmail.users.threads.get({ userId, id: firstThread.id, format: 'full' });

How to attach a PDF to an email sent from an express app

I am trying to send an email with a PDF attachment, but the PDF cannot be opened. (How can I load a PDF from the filesystem and include that in an email that is sent from an express app?)
This is what I have now: I load the PDF from the filesystm like this: var pdfAttach = fs.readFileSync('./pdfs/test.pdf', 'binary'); and include it in the object that is sent to Mandrill like this:
{
...
attachments:
[{content: pdfAttach,
name: "testing.pdf",
type: "application/pdf"
}],
...
}
The recipient gets an email with an PDF file that he cannot open. Any ideas on how to solve this?
If you take a look to Mandrill's Messages API here, you will see that the content of the attachment must be a base64 encoded string. so the code would be:
var pdfAttach = fs.readFileSync('./pdfs/test.pdf');
...
attachments:
[{content: pdfAttach.toString('base64'),
name: "testing.pdf",
type: "application/pdf"
}],
...
Hope this helps.

mailgun incoming mail event fetch attachment url

I have a node endpoint that receives an incoming email in json, complete with any attachments from mailgun.
The attachments are in a json array (xxx.com is used for privacy)
attachments: '[{"url": "https://sw.api.mailgun.net/v3/domains/xxx.com/messages/eyJwIjpmYWxzZSwiayI6ImZhMTU0NDkwLWVmYzgtNDVlNi1hYWMyLTM4M2EwNDY1MjJlNCIsInMiOiI2NmU1NmMzNTIwIiwiYyI6InRhbmtiIn0=/attachments/0", "content-type": "image/png", "name": "ashfordchroming_logo.png", "size": 15667}]
But if i type the url in the browser:
https://sw.api.mailgun.net/v3/domains/xxx.com/messages/eyJwIjpmYWxzZSwiayI6ImZhMTU0NDkwLWVmYzgtNDVlNi1hYWMyLTM4M2EwNDY1MjJlNCIsInMiOiI2NmU1NmMzNTIwIiwiYyI6InRhbmtiIn0=/attachments/0
I get
{
"message": "Domain not found: xxx.com"
}
I wanted the simplest way to show the image attachment in HTML, I was hoping the URL would just work since mailgun store the attachment.
So I was just trying to render the url in a template from Node.
Do I need to attach auth / API key credentials to the front of the URL to do this to test and make work?
If you want to access the raw json, go to
https://sw.api.mailgun.net/v3/domains/xxx.com/messages/eyJwIjpmYWxzZSwiayI6ImZhMTU0NDkwLWVmYzgtNDVlNi1hYWMyLTM4M2EwNDY1MjJlNCIsInMiOiI2NmU1NmMzNTIwIiwiYyI6InRhbmtiIn0=/attachments/0
using username 'api' and password 'your-mailgun-privatekey'.
To do this programmatically, use the request package to read the buffer.
const rp = require("request-promise");
let file = rp.get({
uri: "attachement-url",
headers: {
"Accept": "message/rfc2822"
}
}).auth("api", "your private key")
/**Access the buffer here**/
file.on('data', (s => {
console.log(s)
}))
file.pipe(fs.createWriteStream("./my-image.jpg"))
you can pipe the file to S3 or any cloud bucket.

How to Send Pdf as MMS by Twilo Using Nodejs?

I have a Test Twilio Number which is capable to send SMS,MMS,Voice Call . I am Successful in sending SMS and Voice call .
I am facing a challenge to send PDF as MMS .. As per the TwilioDocs Accepted-mime-types PDF is a Supported Type.
While am trying to Send by using the Syntax :-
var accountSid = '<accountSid >';
var authToken = '<authToken>';
var client = require('twilio')(accountSid, authToken);
client.messages.create({
to: "<validnum>",
from: "<validFrom>",
body: "Test Message ",
mediaUrl: "http://docdro.id/GAak2pV"
mediaContentType:"pdf"
}, function(err, message) {
if(err){
console.log('Error Alert For Message '+JSON.stringify(err));
}else{
console.log(message.sid);
}
});
With the Above Code i can able to send JPG/PNG but PDF is being Failed by an Error:-
(Error: 30008) Unknown error. None
I have no clue Totally !! Somebody help me with a Saving Suggestion
Thanks,
Prasad.
Twilio developer evangelist here.
As Andy is pointing out in the comments, the URL to DropBox that you are using is actually pointing towards an HTML page that contains your PDF. You need the direct link to the PDF file itself, Twilio does not do any work to discover the PDF file within pages.
If you can host the file on S3, or anywhere else publicly, yourself then you will have more luck with this.

Resources