notifme / notifme-sdk

A Node.js library to send all kinds of transactional notifications.
https://notifme.github.io/www/
MIT License
1.94k stars 149 forks source link

How to add different `from` Email for different providers? #64

Closed sudhirkenguva closed 4 years ago

sudhirkenguva commented 4 years ago

Hi, I'm trying to have multiple providers, and trying to give different from emails for each of them.

here is what i'm trying:

                {
                    type: "smtp",
                    pool: true,
                    host: config.SMTP.HOST,
                    port: 465,
                    secure: true, // use TLS
                    auth: {
                        user: config.SMTP.USER,
                        pass: config.SMTP.PASSWORD
                    }
                },
                {
                    type: "sendgrid",
                    apiKey: config.SENDGRID
                }
            ]

can i pass from field in each provider here in above segment? right now i'm passing it inside

  notifmeSdk
            .send({
                ...payload
            })

but in this way, i'm only able to send email with single provider, as for me every email provider is having different from emails.

BDav24 commented 4 years ago

Hi @Sudhir-Kumar-K you can try something along the lines:

notifmeSdk.send({
  email: {
    to: 'john@example.com',
    subject: 'Hi John',
    html: '<b>Hello John! How are you?</b>',
    customize: async (providerId, request) => ({
      ...request,
      from: providerId === 'email-sendgrid-provider' ? 'fromsendgrid@example.com' : 'fromsmtp@example.com'
    })
  }
})
sudhirkenguva commented 4 years ago

Thanks @BDav24!!! Its working. But I think these details are missing in documenation.

BDav24 commented 4 years ago

You're welcome. Yes, the documentation is lacking on this point.