Sending an email with Drupal is easy!

Published on
Sep 8, 2010

Sending a freaking email with Drupal 6 or 7 consumes time mostly on learning, but also on implementing. Instead of simply calling a function, Drupal provides extensive specification of the steps to follow.

Since Drupal is module-oriented, other modules can alter different parts of an email, and different languages can be used if the email is sent with drupal_mail().

But if you want to create a simple webform, that will send an email to the users without too much smartness, then don't waste your time on learning how drupal_mail() works, just because you don't have another day for learning, and more important, you probably won't really use all that functionality after. Well, unless you're a Core developer :-p

So here's what's needed to send the freaking HTML:

$message = array( 'to' => '"'. addslashes(mime_header_encode('Winnie Pooh')) .'" '; 'subject' => t('Example subject'), 'body' => "Hi Winnie! I'm already on Twitter! So please please join me there and not only from TV!
Thanks,
Your friend, N.
", 'headers' => array( 'From' => 'n@webdesignpatterns.org', 'MIME-Version' => '1.0', 'Content-Type' => 'text/html;charset=utf-8',), ); drupal_mail_send($message);

A quick explanation.

We're sending an email using drupal_mail_send() function. The user will receive the email in the pretty form - with his name (Winnie Pooh) followed by the email address (winnie.pooh@example.com). 'MIME-Version' and 'Content-Type' fields just make it possible to send a Unicode HTML, and the 'body' is for the HTML itself. Yeah, you can go wild here, and add styles etc.

This should do the job. And then, on your free time, you can go and check the drupal_mail() functionality. ;-)