I'm working on a D7 version of a D6 module. (Date Reminder, if it makes any difference.) A fundamental feature of the module is sending email to users, where the email uses token replacement for nodes (among others) to generate the content.

Basically, the system admin provides an email template, using tokens, to define an email that will be sent before the date in a node. (This is a simplification, but will serve for this discussion.)

The D6 version uses Messaging to handle most of the email construction and sending tasks, but that seems to be only marginally working for D7. Rather than depend one something defunct, I plan to handle the sending myself using drupal_mail().

I have it kind of working, but I'm not really happy with what I had to do. The problem is, I'd like to support both html and plain text email. For html, I'd want, for example, token [node:summary] to format as it would when displayed on a page. For plain text, I'd want it to format as plain text, such as drupal_html_to_text() would do.

I'm successfully generating plain text email by expanding tokens, then running drupal_html_to_text() over the result. I feel like there must be a better way. I've seen some drupal_mail() tutorials, but nothing that covers this fine point.

Does anyone have any suggestions? Maybe pointers to modules that do this well?

Comments

nevets’s picture

The Examples for Developer module includes email examples.

dwillcox’s picture

I had looked at the "examples" email example already. It's pretty basic. In particular, no token expansion for messages, and no worrying about filtering.

Yes, there are other examples covering tokens and filtering independently, but they have some interesting interactions. I was hoping for something that showed how they could all work together cleanly.

If there's nothing, I'll muddle along on my own.

Jaypan’s picture

Here's a simple example


function my_module_some_form_submit($form, &$form_state['values'])
{
  $to = user_load_by_name($form_state['values']['to']);
  if($to)
  {
    // This is how we send a mail (requires an implementation of hook_mail
    // to be shown after this function)
    drupal_mail
    (
      // The name of your module[
      'my_module',
      // An arbitrary mail key, that you decide (used in hook_mail())
      'some_mail_key',
      // The destination mail address
      $to->mail,
      // The language that the mail should be sent in (requires translation if not English)
      $to->language,

      // Some parameters we can use in hook_mail()
      arrray
      (
        'account' => $to, // Pass the user account to hook_mail
        'some_value' => $form_state['values']['some_value'], // Pass some submitted value to hook_mail
      )
    );
  }
}

function my_module_mail($key, &$message, $params)
{
  // The key allows for a module to define multiple mails. This 
  // key needs to be the same as the first parameter in drupal_mail() above
  if($key ==  'some_mail_key')
  {
    $message['subject'] = t('Email from @site_name', array('@site_name' => variable_get('site_name', 'The site')));
    // Each element of the body array becomes a new line in the email.
    $message['body'][] = t('Hello @user', array('@user' => format_username($params['to'])));
    // Adding an empty string to the body array adds an extra line in the email
    $message['body'][] = '';
    $message['body'][] = t('A user submitted a form with this value: @value', array('@value' => $params['some_value']));
    $message['body'][] = '';
    $message['body'][] = t('Thanks, @the_site', array('@site_name' => variable_get('site_name', 'The site')));
  }
}

Checkout my Japan podcasts.
dwillcox’s picture

Jaypan -

I'd be interested in how you get the nicely colored and formatted code example here.

Thanks for the example, but the sending email is the easy part. What I'm looking for is support for both html and plain-text email, and dealing with token expansion where the tokens can expand with html. ([node:summary], for example.)

So suppose your example was in a form attached to a node, and you wanted the email sent to include tokens expanded from the node so the email could include the node summary or other node attributes. And suppose your form also had a check box labeled "Send email as html." Then you'd be doing something like what I want.

Jaypan’s picture

To get your code formatted, wrap it in PHP tags. You can click the 'php' button above the text area as well.

Unfortunately I can't help you with the HTML email (sorry I missed that in your original post).


Checkout my Japan podcasts.
dwillcox’s picture

Ah, so you can say

  // This is an example of php code
  $example = 'foo';

Nice! I'll use that in the future.

sujoyit2005’s picture

Thank you "Jaypan" for your effort and reply.

I must say to "dwillcox", this forum is not a paid service and we used to share our thoughts here politely!

dsteplight’s picture

arrray
(
'account' => $to, // Pass the user account to hook_mail
'some_value' => $form_state['values']['some_value'], // Pass some submitted value to hook_mail
)

Should say "array" and not "arrray".