3

I'm using the Mime Mail module to send HTML e-mails to my users. I have created a template, mimemail-message.tpl.php, and placed it in my theme's directory. This template defines header and footer links and is applied to every outgoing e-mail.

However, I have a multilingual site and I want to customize these footers per language. My PHP skills are limited but I'm fairly confident I can do this with a switch statement if I can get the e-mail recipient's language.

I'm having trouble figuring out how to get the user's language. The Mime Mail module provides a variable, $recipient, which is set to the e-mail address of the user. Is it possible to load a user based on the e-mail address and then get the user's language?

3

I don't think there's an API function to do exactly that, but a single database query would get you that information:

// Query the database
$args = array(':email' => $email_address);
$lang = db_query('SELECT language FROM {users} WHERE mail = :email', $args)->fetchField();

// If there's no language string use LANGUAGE_UNDEFINED instead.
// This is also a fallback method to use the default language 
// if the user is not found in the database.
if (!$lang || empty($lang)) {
  $lang = LANGUAGE_UNDEFINED; // Or whatever your default language is
}

If you need the user object itself for whatever reason, you can use a slight variation on the above:

$args = array(':email' => $email_address);
$uid = db_query('SELECT uid FROM {users} WHERE mail = :email', $args)->fetchField();

if ($uid) {
  $account = user_load($uid);
  $lang = $account->language;
}

You could also take advantage of the $conditions parameter of user_load_multiple(), but this method has been deprecated so it might not be the best idea to use it:

$users = user_load_multiple(array(), array('mail' => $email_address));
if (!empty($users)) {
  $account = array_shift($users);
  $lang = $account->language;
}

Last but not least, you could use an EntityFieldQuery, although this would probably be overkill for this particular task. It is a useful thing to know though:

$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'user')
  ->propertyCondition('mail', $email_address);

$results = $query->execute();
if (isset($results['user'])) {
  $users = user_load_multiple(array_keys($results['user']));
  if (!empty($users)) {
    $account = array_shift($users);
    $lang = $account->language;
  }
}
1

I'm doing it this way in the mimemail-message.tpl.php, much simpler, not sure if it's the correct way but it works.

global $language;   
switch ($language->language)  //detect the interface language
{
 case "zh-hant":   // for chinese taiwan
   $header = " ";
   $footer = " ";
   break;

 case "zh-hk":     // for chinese hong kong
   $header = " ";
   $footer = " ";
   break;

 case "zh-sg":     // for chinese singapore
   $header = " ";
   $footer = " ";
   break;

 case "zh-hans":   // for chinese china
   $header = " ";
   $footer = " ";
   break;

 default:  // for english and other languages
   $header = " ";
   $footer = " ";
}

print $header;
print $body;
print $footer;

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.