mailgun-js-boland
mailgun-js-boland copied to clipboard
Multiple domains
I have an account with multiple domains at mailgun and would like to instantiate the client only once for all of them since it is the same api key for all of them. Right now, since I have to specify a specific domain on the auth options, all sends are linked in the wrong logs on mailgun. I think it is bad.
You can create multiple instances of the client.
const apiKey = 'key-XXXXXXXXXXXXXXXXXXXXXXX'
const domain = 'www.mydomain.com'
const domain2 = 'www.mydomain2.com'
const mailgun = require('mailgun-js')({ apiKey, domain })
const mailgun2 = new mailgun.Maingun({ apiKey, domain: domain2 })
I know that. I just said I didn't wanted to do that because I use nodemailer-mailgun-transport and it would also require me to instantiate as many transports as they are domains. And I have an unknown domain numbers since users can register domains through my account.
On my side it is an horrible design decision I have to make only because there is no domain option on this side while it totally could. It would be easy for me to provide the domain as an option on each send request.
Please consider this!
Hmm this is not a typical usage scenario, and I'll have to think about how to do it elegantly. You can use the generic requests methods to send data to any domain.
const data1 = { ... }
const data2 = { ... }
const domain = 'www.mydomain.com'
const domain2 = 'www.mydomain2.com'
mailgun.post(`/${domain}/messages`, data, (err, body) => {
if (err) console.error(error)
mailgun.post(`/${domain2}/messages`, data2, (err, body) => {
if (err) console.error(error)
});
Will those handle attachments properly ?
(just to clarify, I'm creating an invoicing platform that will federate multiple companies and they all have their own internal emails and such, so they can declare their domains on my platform, it creates it on mailgun and returns them the configuration to add to their DNS. After that all is ready)
Yes it should handle all the data (including attachment) the same as the normal send()
.
Ok cool thanks. I'll try to go this way in the meantime. Shouldn't this bug stay open though (to remember that something should be done on this subject) ?
@bojand I can think of 2 possible ways to implement this functionality:
- Add the option to pass another parameter to the action, specifying the domain. (e.g.
mailgun.messages().send(data, newDomain, callback)
). - Add the option to add a
domain
property to the data object. (e.g.mailgun.messages.send({from, domain: newDomain}, callback)
).
Which one do you prefer?