来自 nodejs nodemailer 的电子邮件 opened/not 跟踪

Email opened/not tracking from nodejs nodemailer

我所知道的

我想在我的一个网站中实施电子邮件 opened/not 跟踪。搜索后我发现 email-opened/not 跟踪是通过发送嵌入图像和电子邮件(通常为 1 px 透明)来完成的。当有人打开电子邮件并且他们允许图像时,我们会收到图像请求并进行跟踪。

我用什么来实现我所知道的

我正在为项目使用 MEAN 堆栈,使用 nodemailer 发送电子邮件,使用 amazon ses 作为发送服务。

问题

我可以使用上述技术发送嵌入图像,但问题出在节点邮件程序中,您必须将图像作为电子邮件附件附加到嵌入图像。所以,没有来自邮件客户端的调用返回到我在图像文件中提到的回调-url(因为电子邮件客户端已经有该文件)。我如何实现它来跟踪邮件 opened/not。如果这不能用 nodemailer 完成,请指出正确的方向。

我的专长

本人还是初学者,如有不妥之处还请多多包涵。

在包含 . 您也可以将文件附加到 SES 出站电子邮件,但建议不要这样。

编辑:此答案无用,因为 URL 在发送电子邮件之前由 nodemailer 解析。仍然会把它留在这里,因为它可能会给某人指明正确的方向。


从 nodemailer 1.3.4 版本开始,您可以使用 URLs 作为附件,例如:

attachments: [
{
  filename: 'receipt.png',
  path: 'http://example.com/email-receipt?id=1\&email=ex@ample.com',
  cid: 'receipt@example.com'
}
//...

然后,您只需将其嵌入您的 HTML:

<img src="cid:receipt@example.com">

这样就可以了。 可能有用的是,在我的例子中,URL returns 文件下载。

我还是不明白URL是nodemailer在打包时解析的,还是邮件客户端解析的。但有些事情正在做,因为我已经测试了 Thunderbird 和 Geary(桌面)、Gmail 和 Rainloop(浏览器)并且它正在工作。

但不能保证在所有情况下都有效。

我不是 100% 确定我正在 post 做的事情是否是正确的方法,但我想出了一个方法让它工作。由于这个 post 一直没有得到任何有效答案,我看到有人在看它,所以我正在回答我自己的问题

如果使用 node-mailer 将 1px 图片作为附件作为电子邮件附件发送,则无法跟踪电子邮件的打开情况,因为它会自动呈现文件并将文件与邮件一起发送。(没有对服务器进行外部调用 = 你不能探测)。像往常一样使用 node-mailer 渲染 HTML 并将带有源的图片输入到服务器中的路由,如下所示

<html>
   <body>
      <img src="http://YOUR_SITE.com/track/this/image/UNIQUE_ID_FOR_THIS_IMAGE.jpg">
   </body>
</html>

现在您的节点邮件程序中没有附件,文件也不会随电子邮件一起发送,但会在有人打开电子邮件时提取以呈现。

您可以使用 url 中的唯一键跟踪谁打开了电子邮件。

P.S。不要忘记为请求发回 (1x1)px img 文件。这样客户端就不会出现404错误了。

我来晚了回答这个问题。我也想跟踪邮件,所以经过一些研究得出了以下解决方案。可能不是最好的,但我想它会很好用。

    var mailOptions = {
        from: 'Name <email@gmail.com>', // sender address
        to: 'email2@gmail.com', // list of receivers
        subject: 'Hello', // Subject line
        text: 'Hello world', // plaintext body
        html: {
          path : 'mail.html'
        }, // html body
        attachments: [{
          filename : "fileName.txt",
          path : 'fileName.txt'
        }]
    };

以上是nodeMailer的mailoptions的代码

    <p>
      Hello User,
        What is required to do the above things?
    </p>

    <img src="http://localhost:8080/" alt="" />

以上为mail.html

我设置了一个服务器来处理请求。然后我写了一个中间件用来分析。

    var express = require('express');
    var app = express();

    app.use(function (req, res, next) {
      console.log("A request was made for the image");
      next();
    });

    app.get('/', function (req, res) {
      var fs = require('fs');
      fs.readFile('image.jpeg',function(err,data){
        res.writeHead('200', {'Content-Type': 'image/png'});
        res.end(data,'binary');
      });
    });

    var server = app.listen(8080, function () {
      var host = server.address().address;
      var port = server.address().port;
    });

因此,当我们请求图像时,我们将记录 "A request was made for the image"。对上述代码片段进行更改以使事情正常进行。我做过但失败的一些事情是将图像作为静态图像。这确实渲染了图像,但在访问图像时没有记录。这就是图像被读取和提供的原因。

还有一件事,因为我的服务器是本地主机,所以邮件中没有呈现图像。但我认为如果所有的东西都托管在服务器上应该可以正常工作。

编辑:get 请求也可以是

    app.get('/', function (req, res) {
      var buf = new Buffer([
        0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00,
        0x80, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x2c,
        0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02,
        0x02, 0x44, 0x01, 0x00, 0x3b]);
        res.writeHead('200', {'Content-Type': 'image/png'});
        res.end(buf,'binary');
    });
In send mail option you can pass the ConfigurationSetName field for tracking.

Exmaple :

 var opt = {
  to : '',
 from : '',
 ses: {
  ConfigurationSetName: 'your configuration key'
 },
 subject: '',
  html: ``
}
transporter.sendMail(opt)
                .then((res) => {
                  console.log("Successfully sent");
                })
                .catch((err) => console.log("Failed ", err))

This code works for me using nodemailer