More than one e-mail per user

Barberousse - October 20, 2009 - 21:02

Hello,

Is there a simple way for user to receive e-mails (notifications, contact form, ...) in more than one email box ? I see that the user_validate_mail function accepts only one e-mail, no list...

Any idea of modules or simple hack to achieve this ?

Barberousse.

The hack would involve

matt2000 - October 21, 2009 - 00:06

The hack would involve implementing hook_mail_alter in a custom module:

http://api.drupal.org/api/function/hook_mail_alter/6

You could use profile module for storing the additional email addresses.

Ok, thank you, I will try to

Barberousse - October 21, 2009 - 09:07

Ok, thank you, I will try to implement a hook as suggested and using fields added by the http://drupal.org/project/mailalias module.

Barberousse.

Here is what I did for Drupal 6

Barberousse - November 5, 2009 - 21:11

/**
* Implementation of hook_mail_alter().
*/
function moreusermail_mail_alter(&$message) {

  /* this is nicer */
  if (
    !empty($message['to'])
    && !strstr($message['to'], '<')
    && ($to_user = user_load(array('mail' => $message['to'])))
  ) {
    if (!empty($to_user->name)) {
      $message['to'] = $to_user->name . ' <'. trim($to_user->mail) . '>';
    }
  }

  /* this is nicer */
  if (
    !empty($message['from'])
    && !strstr($message['from'], '<')
    && ($from_user = user_load(array('mail' => $message['from'])))
  ) {
    if (!empty($from_user->name)) {
      $message['from'] = $from_user->name . ' <'. trim($from_user->mail) . '>';
    }
  }

  /* try to get recipient mail alias */
  if (
    !empty($message['to'])
    && !empty($to_user->mailalias)
  ) {
    $mailsarr = explode(",", $to_user->mailalias);
    foreach ($mailsarr as $mail) {
      if ($mail != NULL && valid_email_address($mail)) {
        $message['to'] .= ', ' . check_plain($to_user->name) . ' <' . trim($mail) . '>';
      }
    }
    if (!empty($message['params']['to'])) {
      $message['params']['to'] = $message['to'];
    }
  }

  /* try to get sender mail alias */
  if (
    !empty($message['from'])
    && !empty($from_user->mailalias)
  ) {
    if (empty($message['headers']['Sender'])) {
      $message['headers']['Sender'] = $message['from'];
    }

    $mailsarr = explode(",", $from_user->mailalias);
    foreach ($mailsarr as $mail) {
      if ($mail != NULL && valid_email_address($mail)) {
        $message['from'] .= ', ' . check_plain($from_user->name) . ' <' . trim($mail) . '>';
      }
    }

    if (!empty($message['headers']['From'])) {
      $message['headers']['From'] = $message['from'];
    }
    if (!empty($message['headers']['Reply-To'])) {
      $message['headers']['Reply-To'] = $message['from'];
    }
    if (!empty($message['params']['from'])) {
      $message['params']['from'] = $message['from'];
    }
  }

}

Where do you place the code?

sid123 - November 20, 2009 - 16:43

Hi, thanks for your code, I have the same requirement (send notifications to more mails related to one user).
Unfortunately I am less than a newbie in Drupal: where must be placed the piece of code you wrote? is it an addition to mailalias.module, an new module or something else?
Sorry for the stupid question but I do not know drupal from a programmer point of view; on the other side I've a f*g big need of this functionality, so I must be addressed on the right side :)

Thanks again, Silvio

 
 

Drupal is a registered trademark of Dries Buytaert.