1

There are a few email templates in account user emails page: "/admin/config/people/accounts".

How can i add custom emails from my own module? I would like to have&edit all email templates in the same place.

2
  • With hook_form_alter, maybe? :)
    – Mołot
    Sep 8 '14 at 11:33
  • i don't think this is the best (or possible) solution ..
    – gregab
    Sep 8 '14 at 12:02
1

This form is defined in core user module, user.admin.inc file:

/**
 * Form builder; Configure user settings for this site.
 *
 * @ingroup forms
 * @see system_settings_form()
 */
function user_admin_settings() {

Particularly, line 444 starts email template section:

$form['email'] = array(
    '#type' => 'vertical_tabs',
  );
  // These email tokens are shared for all settings, so just define
  // the list once to help ensure they stay in sync.
  $email_token_help = t('Available variables are: [site:name], [site:url], [user:name], [user:mail], [site:login-url], [site:url-brief], [user:edit-url], [user:one-time-login-url], [user:cancel-url].');

  $form['email_admin_created'] = array(
    '#type' => 'fieldset',
    '#title' => t('Welcome (new user created by administrator)'),
    '#collapsible' => TRUE,
    '#collapsed' => (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) != USER_REGISTER_ADMINISTRATORS_ONLY),
    '#description' => t('Edit the welcome e-mail messages sent to new member accounts created by an administrator.') . ' ' . $email_token_help,
    '#group' => 'email',
  );
  $form['email_admin_created']['user_mail_register_admin_created_subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#default_value' => _user_mail_text('register_admin_created_subject', NULL, array(), FALSE),
    '#maxlength' => 180,
  );

As you can see, there is no foreach, no external hook call, nothing. Just raw fields hardcoded. The only way is to add your fields using hook_form_alter(). Make sure you also add your validators and submit handler to take care of your new fields, because user module will not do it for you.

6
  • 1
    What would you suggest then, for custom email templates with translations?
    – gregab
    Sep 8 '14 at 12:28
  • @gregab language_list('enabled') and foreach? Something like that, I think.
    – Mołot
    Sep 8 '14 at 12:41
  • @gregab I know this is a really old post, but I'm trying to do the same thing in D8 and your post is the only one I've found that exactly matches my need. How can I find out what you ended up doing? Apr 7 '20 at 20:00
  • @Mołot "Make sure you also add your validators and submit handler to take care of your new fields" Can you please elaborate and provide more information on this?
    – BlondeSwan
    Jan 29 at 16:23
  • @BlondeSwan this answer is now seven years old. You can't reasonably expect me to still be thinking about the topic ;) If you have a follow up question, please ask it as a question, so people still active in Drupal community can help you.
    – Mołot
    Jan 29 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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