2

Is it possible to dynamically generate cases in PHP switch statements, as in this example of hook_mail() ?

/*
 * Implement hook_mail().
 */

function rsvp_mail($key, &$message, $params) {
  $guests = rsvp_query();
  foreach ($guests as $account) {
    switch($key) {
      case "invite $account->uid" :
        $message['subject'] = "And invitation for $account->name";
        $message['body'][] = 'Some body text.';
        $message['body'][] = rsvp_get_link($account);
        break;
    }
  }
}
semperos
  • 4,674
  • 28
  • 31
starsinmypockets
  • 2,264
  • 4
  • 34
  • 45
  • Can you clarify? This looks like a misuse of hook_mail in general. Please explain what you're trying to accomplish, and why you are putting this in hook_mail. hook_mail is used to populate/modify emails. There are plenty of other entry points to initiate the mail process, though. – David Fells May 03 '11 at 03:15
  • @David Fells: This is more comprehensive: http://stackoverflow.com/questions/5862170/send-one-time-login-link-via-hook-mail – starsinmypockets May 03 '11 at 03:27
  • you can lose the switch, and just ask if (strpos($key, 'invite ') == 0), or if (preg_match('!invite \d+!', $key)) – yoavmatchulsky May 03 '11 at 06:16

1 Answers1

2

If you are using the same basic email "skeleton" for all the users you're mailing and you just need to replace a few values to be user-specific (user name, profile information, etc.), you can just use tokens to get the job done.

Take a look at these two functions to see what I mean (they're side-by-side in the same file):

https://github.com/semperos/drupal-simple-subs/blob/master/simple_subs.module#L180

https://github.com/semperos/drupal-simple-subs/blob/master/simple_subs.module#L191

When you use drupal_mail(), you can assign whatever values you want to the params array that you pass to it (this is the same params array that gets passed to your implementation of hook_mail). Those values can then be added to the default mail tokens that the Token module provides out-of-the-box, as you see in my implementation of hook_mail on lines 195-198.

If you need something more complex (i.e. the primary text of your email depends on the user), you can still use this same system, but make calls to the database for more information. If you need to store a different email body per user, store that in a database table associated with the user's id, then in your mail functions query for that data dynamically.

In any event, you shouldn't have to use any kind of dynamic behavior at the level of PHP's switch statement; you should be able to pass in whatever dynamic values you need via the params array that your implementation of hook_mail expects. In general, the case statements for dealing with mail are not meant to provide dynamic behavior on a per-user or per-node basis, but rather for managing emails with very different content and uses.

semperos
  • 4,674
  • 28
  • 31
  • Thanks... using $params (after a fashion) solved my problem.. could you be bothered to look at this: http://stackoverflow.com/questions/5877373/adding-url-breaks-hook-mail-implementation? Cheers! – starsinmypockets May 04 '11 at 00:59