vendure-ecommerce / vendure

The commerce platform with customization in its DNA.
https://www.vendure.io
MIT License
5.42k stars 952 forks source link

Receiver in Email Plugin Handler #1891

Open stefanfeldner opened 1 year ago

stefanfeldner commented 1 year ago

Describe the bug I have a custom handler extending the emailVerificationHandler and I want to set the recipient to a fixed email from the globalTemplateVars. The handler is in a separate file and imported into the vendure-config.ts. I'm filtering out the existing emailVerificationHandler and parsing in the customHandler instead.

export const customHandler = emailVerificationHandler
  .setRecipient(_ => `{{ receiver }}`)

But if I do it like that, I get the following error:

No recipients defined
[run:*worker] Error: No recipients defined

I defined the variable and plugin like this:

EmailPlugin.init({
      handlers: [...defaultEmailHandlers.filter(handler => handler.listener.type !== 'email-verification'), customHandler],
      globalTemplateVars: {
        receiver: 'myemail@gmail.com',
      },
    }),

It currently looks like it is not possible to set a recipient directly like this because TS expects the event.

.setRecipient(`{{ receiver }}`)

Argument of type 'string' is not assignable to parameter of type '(event: AccountRegistrationEvent) => string'.ts(2345)

To Reproduce Follow the above-mentioned setup or put the emailVerificationHandler directly in vendure-config.ts and try to assign the recipient.

Expected behavior Should work similarly to the .setFrom() function where I can pass a string / globalTemplateVars variable into the .setRecipient()

Environment:

michaelbromley commented 1 year ago

I've just looked into this, and the problem we have is that the EmailGenerator.generate() interface does not receive the "recipient" value. Changing this interface to also allow it to interpolate the recipient would unfortunately be a breaking change so I'm wondering if there is a workable alternative?

Is there anything that prevents you from, rather than using the handlebars interpolation for the recipient, you just define a constant value and use that?

const RECIPIENT = 'myemail@gmail.com';

// ...

export const customHandler = emailVerificationHandler
  .setRecipient(_ => RECIPIENT)
stefanfeldner commented 1 year ago

Hey, thank you for checking it out.

The problem here is that we have the handlers in an external handlers.ts file and would like to use the variables defined in the EmailPlugin vendure-config.ts but since it's not a Service or something similar we can't inject the PLUGIN_INIT_OPTIONS to grab it.

But we could just export a regular variable in the vendure-config.ts, import it in handlers.ts, and use it as you mentioned above if changing the interface is creating bugs.