Nodemailer: How to attach pdf from url

Created on 14 Apr 2016  路  11Comments  路  Source: nodemailer/nodemailer

Include the following information with your issue:

1) Nodemailer version v2.3.2
2) Node.js version v4.4.2
3) OS Windows 8.1
4) So, I received mail but with corrupted pdf file.
Bellow you can see my scriptcode but you should to change SMTP attributes. PDF example file is internet public document so you can use it.

CODE

var nodemailer = require('nodemailer');
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport({
    pool: true,
    host: 'mail.test.hr',
    port: 25 ,
    ignoreTLS: true,
    secure: false,
    logger: true, // log to console
    debug: true // include SMTP traffic in the logs
}, {
    // default message fields
    // sender info
    from: 'test <[email protected]>',
    headers: {
        'X-Laziness-level': 1000 // just an example header, no need to use this
    }
});
// setup e-mail data with unicode symbols
var mailOptions = {
        from: '"test" <[email protected]>', // sender address
    to: '[email protected]', // list of receivers
    subject: 'Hello', // Subject line
    text: 'Hello world', // plaintext body
    attachments: [{
        filename:'GlasIstre.pdf', 
                contentType: 'application/pdf',
        path: 'http://files.glasistre.hr/GlasIstre.pdf'
    }]
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }
    console.log('Message sent: ' + info.response);
});

Most helpful comment

I have the same problem, any solution? And I find even I send a txt file, there is also problem, the nodemailer can find the txt file on my server, but it just send with an empty txt file.

All 11 comments

Anyone?

I was able to use the same code to successfully send the PDF attachment. The file is quite large though so I'd suggest you to not fetch it from an URL but store it to a file first and then send that file from a disk.

Could you store and upload the raw source of the corrupt message so it would be possible to check how the message is corrupt?

True, when I first saved PDF file on disk and run changed script ( used attaching as file from disk) then I got correct pdf file. Second, my first example file was too large so I try to attach smaller file via URL methode using path:'url' but again I got corrupted pdf file.
Please can you try my source code with new extra small file http://www.axmag.com/download/pdfurl-guide.pdf
Corrupted file is in attachment.
pdfurl-guide.pdf

The corrupted file seems to be a HTML error response from a proxy server. Nodemailer does not support fetching URLs through proxies so you have to prefetch the file first if you want to send it.

Hello,

I tried running the same code using a PDF locally. Though the PDF appears in the email, it's corrupted (even though pdf opens on local system).

  attachments: 
   [ { filename: 'Random.pdf',
       contentType: 'application/pdf',
       path: 'assets/stickers/pdf/upch25-Jul-20160.pdf' } ] }

This happens to me too! A valid local PDF when sent as an attachment gets delivered corrupted.
I checked the contents of the file - it is no error or text.

This is still a problem to me. Exactly like @vdraceil described.

I have the same problem, any solution? And I find even I send a txt file, there is also problem, the nodemailer can find the txt file on my server, but it just send with an empty txt file.

Has anyone solved this? It is still an issue.

Hi I solved this in this way thanks to Ilya Khadykin

https://stackoverflow.com/questions/34077507/how-can-i-attach-an-image-to-an-email-from-an-url-using-nodemailer-and-request-m/34095094#34095094

var PassThrough = require('stream').PassThrough;
var nameOfAttachment = 'screenshot.png';
var imageUrlStream = new PassThrough();
request
     .get({
             proxy: 'http://YOUR_DOMAIN_NAME:3129', // if needed
             url: opts.imageUrl
         })
     .on('error', function(err) {
             // I should consider adding additional logic for handling errors here
             console.log(err);
    })
     .pipe(imageUrlStream);

var mailOptions = {
    from: opts.from, // sender address
    to: opts.to, // list of receivers
    subject: opts.subject, // Subject line
    html: opts.body, // html body
    attachments: [
        {
            filename: nameOfAttachment,
            content: imageUrlStream
       }
   ]
   };

in my case i was sending an excel file

Hi josco007,

I have use your code however I didn't put in any proxy. The output also failed due to security issue. The action is blocked by the CORS. May I now have other alternative?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

anonprophet picture anonprophet  路  3Comments

ryanrolds picture ryanrolds  路  4Comments

abhishekdgeek picture abhishekdgeek  路  5Comments

tegarkurniawan picture tegarkurniawan  路  3Comments

Nedudi picture Nedudi  路  5Comments