Skip to content

How to use HTML rewrite?

Andris Reinman edited this page Sep 17, 2016 · 3 revisions

NB This feature is outdated and removed from never ZoneMTA versions. ZoneMTA has support for custom plugins that allow rewriting HTML data and more so the interface described here was not needed anymore

ZoneMTA comes bundled with an option to modify all text/html parts inside stored messages. Rewriting is disabled by default but you can enable it with the rewrite.enabled option. If enabled then every time ZoneMTA sees a MIME node with Content Type of text/html, it decodes it into a normal string and posts the content to an URL defined by rewrite.url. Whatever is returned from that url (assuming it returns with a success code of 200) is then encoded and rewritten in place of the original text/html contents.

Rewriting HTML allows you to add tracking images or replace normal links with tracking links. You could also append an "unsubscribe" link to the message etc. Rewriting happens once per message per MIME node (if the same message has multiple text/html nodes, then all of these get their own rewriting action) which means that if the message has several recipients then they all get the same rewritten copy. In the future this restriction might be removed (assumes that the message is cloned for every recipient instead of using shared copy) but for now you would need to send a separate copy for every recipient yourself.

The request against your rewriting HTTP endpoint is a POST request with content type application/x-www-form-urlencoded. The request includes the following POST arguments:

  • id is the Queue ID for the message
  • messageId is the Message-ID header value of the message (might be undefined if not set)
  • from is the MAIL FROM address from the SMTP envelope
  • to is a list of RCPT TO addresses from the SMTP envelope, joined with commas
  • html is the HTML value. This is an unicode string

Example

Example rewriter in Express.js

server.post('/test-rewrite', function(req, res) {
    let updatedHtml = req.body.html + '<p>hello world!</p>';
    res.send(updatedHtml);
});
Clone this wiki locally