Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to attach pdf from url #601

Closed
besla opened this issue Apr 14, 2016 · 11 comments
Closed

How to attach pdf from url #601

besla opened this issue Apr 14, 2016 · 11 comments

Comments

@besla
Copy link

besla commented Apr 14, 2016

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 <test@test.hr>',
    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" <test@test.hr>', // sender address
    to: 'test2@test.hr', // 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);
});
@besla
Copy link
Author

besla commented Apr 14, 2016

Anyone?

@andris9
Copy link
Member

andris9 commented Apr 14, 2016

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?

@besla
Copy link
Author

besla commented Apr 14, 2016

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

@andris9
Copy link
Member

andris9 commented Apr 20, 2016

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.

@andris9 andris9 closed this as completed Apr 20, 2016
@rishab-yumist
Copy link

rishab-yumist commented Jul 25, 2016

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' } ] }

@vdraceil
Copy link

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.

@scipper
Copy link

scipper commented Jun 5, 2018

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

@tenbabagu
Copy link

tenbabagu commented Nov 10, 2018

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.

@CloudBranch
Copy link

Has anyone solved this? It is still an issue.

@josco007
Copy link

josco007 commented Jul 21, 2019

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

@limweebeng
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants