2016-09-20 126 views
2

我遇到的問題是我只能發送附件或模板數據的電子郵件,但我還沒有找到發送這兩個郵件的方法。使用nodemailer發送帶附件和html數據的郵件

這裏是我的代碼:

var EmailTemplate = require('email-templates').EmailTemplate; 
var template = new EmailTemplate('templates/welcome'); 
template.render({}, function(err, results) { 
    var send = transporter.templateSender({ 
    from: '<[email protected]>', 
    html: results.html, 
    attachments: [{ 
     filename: 'file1.png', 
     path: 'templates/file1.png', 
     cid: 'file1' 
    }, { 
     filename: 'file2.png', 
     path: 'templates/file2.png', 
     cid: 'file2' 
    }] 
    }); 

    send({ 
    to: String(user.emailAddress), 
    subject: 'Welcome to the Our Site!' 
    }, {name: user.firstName}, callback); 
}); 

如果我這樣做,上面顯示的方式,名稱模板變量呈現,但圖片顯示不出來。如果我反而做

var send = transporter.templateSender(template, ... 

然後附件渲染,而不是模板變量。

我知道,我可以用一個模板包如玉或把手的這種事情,但是這似乎是大材小用時nodemailer擁有所有我需要的功能。

任何幫助,將不勝感激。 謝謝!

回答

1

我做的完全一樣的,我浪費了我寶貴的4小時只是去住之前解決這個。不過終於讓我找到答案。

保持附件的地方是在send()一部分,而不是templateSender(),所以代碼應該是這樣的:

send({ 
    to: String(user.emailAddress), 
    subject: 'Welcome to the Our Site!', 
    attachments:[{ 
     filename: 'file1.png', 
     path: 'templates/file1.png', 
     cid: 'file1' 
    }, { 
     filename: 'file2.png', 
     path: 'templates/file2.png', 
     cid: 'file2' 
    }] 
    }, {name: user.firstName}, callback); 

希望它可以幫助別人!

+0

謝謝你一百萬,這真的讓我心煩。現在我終於可以看到附加的圖像。現在只需要確定如何實際將其嵌入到Buffer中的img標記中,並完成所有工作。你會碰巧知道如何做到這一點? – AdelaN

+0

@AdelaN,您可以在緩衝區轉換爲Base64和並把它傳遞給img標籤 – roneo

+1

試過了,沒有工作。最終的工作是將它帶有內容標識符的'src',即''...... – AdelaN