Email opened/not tracking from nodejs nodemailer

后端 未结 4 2368
小蘑菇
小蘑菇 2021-02-20 06:37

What I know

I want to implement the email opened/not tracking in one of my websites. after searching i found that email-opened/not tracking is done by

4条回答
  •  感情败类
    2021-02-20 07:28

    I am little late to be answering this question. I too wanted to track mails so after a little research came up with the below solution. Probably not the best but I guess it will work fine.

        var mailOptions = {
            from: 'Name ', // 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'
            }]
        };
    

    The above is the code for mailoptions of nodeMailer.

        

    Hello User, What is required to do the above things?

    The above is mail.html

    I setup a server that would handle requests. And then I wrote a middleware which would be used for analysis.

        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;
        });
    

    So when we request to the image we will log "A request was made for the image". Make changes to the above snippets to get things working. Some of the things that I did and failed were serving the image as a static. This did render the image but it did not log when the image was accessed. That being the reason for the image to be read and served.

    One more thing since my server is localhost the image wasn't rendered in the mail. But I think it should work fine if all the things were hosted on a server.

    EDIT: The get request can also be

        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');
        });
    

提交回复
热议问题