Hi, I am trying to code a custom rules action to toggle the mimemail preference for sending html/text email.

This is my custom rules action:

function mymodule_rules_action_toggle_mimemail_preference($account) {
  // mimemail_user_presave() needs to be disabled for this to work.
  $mimemail_preference = $account->data['mimemail_textonly'];
  if ($mimemail_preference == 0) {
    $account->data['mimemail_textonly'] = 1;
  }
  else {
    $account->data['mimemail_textonly'] = 0;
  }
  user_save($account);
}

The issue I ran into is that Mimemail calls hook_user_presave(), which blocks this action from being evaluated:

function mimemail_user_presave(&$edit, $account, $category) {
  $edit['data']['mimemail_textonly'] = isset($edit['mimemail_textonly']) ? $edit['mimemail_textonly'] : 0;
}

This is necessary to update the user profile, but how do I get around this in Rules?

Comments

ptmkenny’s picture

Title: Why does Mimemail use hook_user_presave() » Why does Mimemail use hook_user_presave()?
ptmkenny’s picture

Title: Why does Mimemail use hook_user_presave()? » How can I make a custom rule to toggle the mimemail user account preference?
Issue summary: View changes

Ok, I see now this is necessary to save the user profile. So I'm editing the original issue to update my question.

TR’s picture

Status: Active » Fixed

This issue is D7-specific, as user email preferences are handled in a completely different way in D8/D9.

It seems what you're trying to do is to ignore the user-selected preference and use the admin-selected preference instead, which IMO is just wrong. IF you are going to allow the user to select a preference, then respect that choice. If you don't want your users to choose, hook_form_alter() the user profile and disallow that choice. You can hardwire that choice to allow HTML emails for everyone, in which case the admin-selected preference will always be used.

TR’s picture

Status: Fixed » Closed (fixed)