Finally found a way around this issue. It is a combination of a sails-hook-organics
issue and a mailgun-js (npm module)
issue. My problem is that the mailgun domain is in the EU zone,so when I use the default sailsjs config variables (mailgunDomain, mailgunSecret)
to send emails, it I get a 'Forbidden' response.
This is due to the host
parameter mailgun-js
expects or falls back to the default which is api.mailgun.net
. For EU zone domains this should be api.eu.mailgun.net
and there is no way to include it in the current implementation of sails and the config options. The way to permanently solve this is to 'patch' the mailgun-js
file node_modules/mailgun-js/lib/mailgun.js
. Because I am using heroku I want this to be persistent on every deploy. The easy way to achieve this is by creating a postinstall
script in package.json
like so "postinstall": "patch --forward node_modules/mailgun-js/lib/mailgun.js < patches/mailgun.patch && grunt build"
.
The contents of the mailgun.patch file is a diff created by issuing this command diff -Naur node_modules/mailgun-js/lib/mailgun.js patches/mailgun.js > patches/mailgun.patch
which gets a diff on the two files, the latter being a copy of my node_modules/mailgun-js/lib/mailgun.js file but with an edit on line 25 which reads : this.host = 'api.eu.mailgun.net';
which is what this is all about, setting this host param to fix the EU zone Forbidden response.
Credits to Mats Byrkjeland's article for the patching method https://opensource.christmas/2019/4