2015-09-23 28 views
5

Tôi muốn đính kèm tài liệu PDF bằng cách sử dụng nodemailer và node.js, tuy nhiên, các ví dụ duy nhất tôi đang tìm kiếm tệp đính kèm với nodemailer là có tệp .txt (here).Node.js và Nodemailer: Chúng tôi có thể đính kèm tài liệu PDF vào email không?

Có ai biết liệu tệp đính kèm tài liệu PDF có được hỗ trợ bằng nodemailer không?

Ban đầu có vẻ tệp PDF có thể được đính kèm, nhưng tệp PDF đến qua email có vẻ như bị hỏng (xem hình ảnh).

enter image description here

Code: (Phỏng theo câu trả lời của Mahesh)

fs.readFile('/filePath/fileName.pdf', function (err, data) { 
if (err) throw err;             
var mailOptions = { 
    from: 'Test <[email protected]>', // sender address         
    to: 'toPersonName <[email protected]>', // list of receivers         
    subject: 'Attachment', // Subject line             
    text: 'Hello world attachment test', // plaintext body             
    html: '<b>Hello world attachment test HTML</b>', // html body            
    attachments: [ 
     { 
      filename: 'fileName.pdf',           
      contentType: 'application/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); 
}); 
console.log(data); 
}); 

ga đáp ứng:

<Buffer 25 50 44 46 2d 31 2e 33 0a 25 c4 e5 f2 e5 eb a7 f3 a0 d0 c4 c6 0a 34 20 30 20 6f 62 6a 0a 3c 3c 20 2f 4c 65 6e 67 74 68 20 35 20 30 20 52 20 2f 46 69 ... > 
Message sent: 250 2.0.0 OK 1443026036 hq8sm3016566pad.35 - gsmtp 
+0

AFAIK nó hỗ trợ tất cả các loại nội dung không phải văn bản – prasun

Trả lời

9

Có bạn có thể. Bạn phải thêm đường dẫn của tệp mà bạn đang cố đính kèm.

transporter.sendMail({ 
    from: '[email protected]'. 
    to: '[email protected]', 
    subject: 'an attached file', 
    text: 'check out this attached pdf file', 
    attachments: [{ 
    filename: 'file.pdf', 
    path: 'C:/Users/Username/Desktop/somefile.pdf' 
    contentType: 'application/pdf' 
    }], function (err, info) { 
    if(err){ 
     console.error(err); 
     res.send(err); 
    } 
    else{ 
     console.log(info); 
     res.send(info); 
    } 
    } 
); 
+0

Làm cách nào để bạn cho phép mọi người tải lên tệp PDF và đính kèm? 'Đường dẫn' sẽ là gì? – Growler

+0

Xin lỗi, tôi đã không giúp bạn. – Vishnu

+0

kiểm tra https://nodemailer.com/message/attachments/ này – Vishnu

Các vấn đề liên quan