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.