2

At /admin/config/people/accounts, there are several stock email messages set.

How can I use one of these when I am sending emails from a custom module?

The text that's being used in "Welcome (new user created by administrator)" is all that I need, plus there's the bonus that our client can edit this email in the future if they want to.

I can send the message easily enough using:

$params = [
   'title' => "My Title",
   'subject' => "An Account Has Been Created for you",
   'message' => Html::escape("Some text here")
 ];

$result = $mailManager->mail($module, $key, $to, $language_code, $params, NULL, true);

Is there a variable somewhere to get the pre-entered text?

3 Answers 3

0

I guess this should be possible via configuration API. Look for the suitable entry where the message body is saved, call it via the service to put it into a variable. Then you can do additional coding with the string variable. More information you can find here: Configuration override system in the docs

1
  • For the specific case of email sent by the core Drupal user module, yes the messages are stored in config. You can see them with drush config-get user.mail. But this is not true in general - it is a feature of the user module. Other modules which define pre-set messages do NOT store these message in config. In general, given a module name and a message key, it is not possible to access the text of the message, as it is probably just in code or in a template. Also, in general it is not possible to programmatically find out the message keys.
    – anonymous
    Feb 16, 2021 at 5:22
0

All emails in Drupal are uniquely identified by their sending module and key. In the case of the welcome message for admin-created users, the sending module is user and the key is register_admin_created. This is all you need. The subject and the message body is defined by the sending module and you don't need access to it in order to send the message.

If you want to send this message from your own module, the correct way to do it is: \Drupal::service('plugin.manager.mail')->mail('user', 'register_admin_created', $to, $langcode); where $to is a string containing the email address of the recipient, and $langcode is a RFC 5646 language code. There are also a few optional parameters you can pass to mail(). See the MailManager::mail() API documentation for more information about how to use mail() and what the arguments mean.

Also note that if you are calling mail() from object-oriented code, you really should inject the plugin.manager.mail service instead of using the static \Drupal call shown above. But explaining that is outside the scope of this question.

-1

Thanks for the guidance on this @dercheffe. I found what I needed in the config file user.mail.yml and was able to pull the strings outs as an array with :

$config_txt = \Drupal::config('user.mail')->get('register_admin_created');

However, I had the problem of how to replace the user and site variables in the strings from the config file.

Long story short, I didn't even need that config information.

The answer was to just use the Drupal function

_user_mail_notify('register_admin_created', $newuser);

The email is sent out based on the text in the configuration to our new user.

1
  • _user_mail_notify() is not part of the Drupal API, and should not be used by contributed modules or site-specific code. You can tell this by the '_' prefix - this is an old naming convention in PHP and Drupal which indicates the function is private and for internal use only. In the future, you should expect this to be turned into a real method on a real Drupal service, and you should expect this function to disappear without any deprecation notice (since it's not part of the public API).
    – anonymous
    Feb 16, 2021 at 2:11

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.