0

I am currently using Sails.js v1.1 and the issue I am facing is that my email domain is an EU domain. Currently there is no way to set the host parameter (* 'host' - the mailgun host (default: 'api.mailgun.net')) for mailgun-js via sails-hook-organics the only accepted parameters are:

    // Initialize the underlying mailgun API wrapper lib.
    var mailgun = Mailgun({
      apiKey: inputs.secret,
      domain: inputs.domain,
    });

Now, I can 'hot-fix' this in production but there should be a better more permanent solution which doesn't include ditching 'sails-hook-organics', at least the mailgun integration. Any suggestions are appreciated.

Numant Santz
  • 170
  • 1
  • 11

3 Answers3

1

It works for my by setting this:

mailgunDomain: 'mg.your_domain.eu',

mailgunSecret: ‘secret_key,

in config/custom.js and using these vars, sails.config.custom,mailgunSecret and sails.config.custom.mailgunDomain in api/hooks/custom/index.js, api/helpers/send-template-email.js etc.

Robertino Vasilescu
  • 1,038
  • 1
  • 7
  • 13
  • Thank you for sharing your experience with the framework! The main challenge is to set the api URL for EU domains on mailgun, which needs to be set to 'api.eu.mailgun.net'. When I set this manually in ` var mailgun = Mailgun({ apiKey: inputs.secret, domain: inputs.domain, host: "api.eu.mailgun.net" });` – Numant Santz Aug 05 '20 at 21:44
  • You don't have to set the api.mailgun.net anywhere, it is mailgun who calls the specific API depending on your domain. An example of the sails calls is in api/helpers/send-template-email.js. All you need to do is go to your mailgun dashboard and set your domain accordingly, https://imgur.com/R9hMFyx, then set everything in Sending/Domain Settings. – Robertino Vasilescu Aug 06 '20 at 07:53
1

I think that previous answer with patch a diff is an overengineering. You can simply use host parameter to set it up.

const mailGunConfig = require("mailgun-js")({
  apiKey: process.env.MAIL_GUN_API_KEY,
  domain: mydomain.org,
  host: "api.eu.mailgun.net"
});
Vlad Pavlovski
  • 1,582
  • 1
  • 14
  • 10
0

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

Numant Santz
  • 170
  • 1
  • 11