0

I'm implementing a cron job that should send a notification email to all users that meet a certain criterion. I created a Service implementing the CronInterface and hook_cron calls this service's run() method. This queries for the users that need notification. I want to loop over these user and send emails to them. These emails should contain a default message and they should be greeted with their name.

The system already uses the "Drupal Symfony Mailer". Everything in the documentation seems to migrate configurations from other implmentations.

I tried implementing a Plugin extending EmailBuilderBase with:

@EmailBuilder(
  id = "notification",
  sub_types = { "update" = @Translation("Update") },
  common_adjusters = {"email_subject", "email_body"},
)

My expectation was that I will be able to

  • add a new policy on the configuration page that belongs to this class
  • provide my notification text, using a token for the user name
  • send the email via Drupal::service('email_factory')->sendTypedEmail('notification', 'update', $user);

This doesn't work. I can't create a policy matching my Builder. The dropdown menus on policy creation don't offer options for "notification" and "update" for the type and sub-type selection.

How do I create the policy that allows me to send my email via the method call above without writing a deprecated version that gets migrated?

2
  • What does "doesn't work" mean, technically?
    – mona lisa
    Commented Oct 12, 2023 at 16:16
  • I expected the magic comment to create additional options in the dropdown menus for policy creation. I thought, there might appear an option "notification" for the type and an option "update" for the sub-type, at least after (re-)activating the module and rebuilding the cache. This didn't happen and now I'm stuck. Either something is broken or I got the concept wrong. I updated the discription.
    – flauschi
    Commented Oct 12, 2023 at 17:37

1 Answer 1

1

I created a specific type and subtype for a custom module, like this: In src/Plugin/Emailbuilder create a class MyModuleEmailBuilder extends EmailBuilderBase. Make an annotation there:

/**
 * Email Builder plug-in for the custom_email_example module.
 *
 * @EmailBuilder(
 *   id = "my_module",
 *   sub_types = {
 *     "update" = @Translation("Update"),
 *   },
 *   common_adjusters = {"email_subject", "email_body"},
 * )
 */

Then (re-)install the module. Hopes this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.