2015-05-10 32 views
2

如何用Node.js mailgun嵌入圖像。 從閱讀的文檔,它們使用Node.js mailgun內嵌/嵌入圖像

-F [email protected]/cartman.jpg 

的問題,雖然,它的捲曲的例子。

這裏是我到目前爲止

Mailgun.sendHtmlEmail({ 
    apiKey: '..', 
    domain: '...', 
    toEmail: created.email, 
    toName: created.email, 
    subject: 'mySite update', 
    htmlMessage: '<html><img style="display:block;" class="img1" src="cid:test.png" width="600" height="64" /></html>', 
    inline: 'email1/test.png', 
    fromEmail: '[email protected]', 
    fromName: 'Admin' 
}).exec({ 
    // An unexpected error occurred. 
    error: function (err){ 
     console.log(err); 
    }, 
    // OK. 
    success: function(){ 
    } 
}); 

當這個測試,沒有附加的圖像。

我假設您必須以Node.js能夠理解的方式訪問電子郵件的內聯文件。

如果是這樣,你怎麼做?

有人可以指點我在正確的方向,請。

+0

如果有幫助,你通常應該避免在電子郵件內聯圖像。 Sendgrid在這裏有很多關於這個主題的資源:[https://sendgrid.com/blog/embedding-images-emails-facts/](https://sendgrid.com/blog/embedding-images-emails-facts/) – bvanvugt

回答

2

我建議您使用mailcomposer模塊來生成帶有附件的電子郵件消息。

mailcomposer = new MailComposer({forceEmbeddedImages: true}); 
mailcomposer.addAttachment({ 
    fileName: 'image.png', 
    filePath: IMAGES_PATH + '/image.png', 
    cid: '0o1q9i2w8u38ur.image.png' 
}); 

mailcomposer.setMessageOption({ 
    to: created.email, 
    from: '[email protected]', 
    subject: 'mySite update', 
    body: '...', 
    html: '<html>...</html>' 
}); 

你可以通過mailgun發送:

mailcomposer.buildMessage(function (err, message) { 
    if (err) { 
    // ... 
    } 

    mailgun 
    .messages() 
    .sendMime({ 
     to: created.email, 
     message: message 
    }, function (err, response) { 
     // ... 
    }); 
}); 
+0

謝謝,是的,我目前正在研究nodemailer並使用gmail帳戶發送電子郵件。 我也會嘗試這種方法。 – user2124726

+0

只是爲了澄清:這個答案中顯示的mailgun api來自[bojand/mailgun-js](https://github.com/bojand/mailgun-js)。 – justspamjustin