Jika Anda mencoba mengirim surat melalui modul khusus Anda, maka Anda dapat mengikuti panduan yang disebutkan di bawah ini. Katakanlah, saya punya satu Modul yang disebut "Commerce Canvas (commerce_canvas)" 1. Pertama-tama Ubah fungsi surat Drupal untuk mendukung tautan dan menambahkan jenis penyandian lainnya
/**
* Implements hook_mail_alter()
* @param string $message
*/
function commerce_canvas_mail_alter(&$message) {
$headers = array(
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=iso-8859-1; format=flowed',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal',
);
foreach ($headers as $key => $value) {
$message['headers'][$key] = $value;
}
}
- Lalu selanjutnya, kaitkan HOOK_mail di modul Anda sebagai
/**
* Implements hook_mail()
* @param string $key
* @param string $message
* @param unknown_type $params
*/
function commerce_canvas_mail($key, &$message, $params) {
//Language Selection
$options = array(
'langcode' => $message['language']->language,
);
switch($key) {
case "user_payment_notification" :
$message['subject'] = isset($params['subject']) ? $params['subject'] : $message['subject'] = t('Payment recieved @site-name', array('@site-name' => variable_get('site_name', 'Express Canvas')), $options);
$message['body'][] = isset($params['body']) ? $params['body'] : NULL;
if (isset($params['headers']) && is_array($params['headers'])) {
$message['headers'] += $params['headers'];
}
break;
}
}
- Maka Anda perlu membuat fungsi pembungkus untuk memohon surat dari modul Anda.
function commerce_canvas_mail_send(array $values = array()) {
$module = $values['module'];
$key = $values['key'];
$to = $values['to'];
$from = $values['form'];
$language = isset($values['lang']) ? $values['lang'] : language_default();
$params = array(
'subject' => $values['subject'],
'body' => $values['body'],
);
if(array_key_exists('headers', $values)) {
$params['headers'] = $values['headers']; //Assumed as an array
}
$send = TRUE;
$mail = drupal_mail($module, $key, $to, $language, $params, $from, $send);
if($mail['result']) {
return TRUE;
} else {
$error_msg = 'Failed to send the email in commerce_canvas Module';
watchdog('canvas-email', $error_msg, array(), WATCHDOG_ALERT);
return FALSE;
}
}
Fungsi ini akan mengirim email dan juga membuat pesan debug jika terjadi kesalahan. Tapi tetap saja Anda tidak akan dapat mengirim email HTML karena Anda perlu memperpanjang DefaultMailSystem default Drupal 7 dengan Anda sendiri. Jadi perlu dilakukan sebagai berikut,
class CommerceCanvasMailSystem extends DefaultMailSystem {
/**
* Concatenate and wrap the e-mail body for plain-text mails.
*
* @param $message
* A message array, as described in hook_mail_alter().
*
* @return
* The formatted $message.
*/
public function format(array $message) {
$message['body'] = implode("\n\n", $message['body']);
return $message;
}
/**
* Send an e-mail message, using Drupal variables and default settings.
*
* @see http://php.net/manual/en/function.mail.php
* @see drupal_mail()
*
* @param $message
* A message array, as described in hook_mail_alter().
* @return
* TRUE if the mail was successfully accepted, otherwise FALSE.
*/
public function mail(array $message) {
$mimeheaders = array();
foreach ($message['headers'] as $name => $value) {
$mimeheaders[] = $name . ': ' . mime_header_encode($value);
}
$line_endings = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
return mail(
$message['to'],
mime_header_encode($message['subject']),
// Note: e-mail uses CRLF for line-endings. PHP's API requires LF
// on Unix and CRLF on Windows. Drupal automatically guesses the
// line-ending format appropriate for your system. If you need to
// override this, adjust $conf['mail_line_endings'] in settings.php.
preg_replace('@\r?\n@', $line_endings, $message['body']),
// For headers, PHP's API suggests that we use CRLF normally,
// but some MTAs incorrectly replace LF with CRLF. See #234403.
join("\n", $mimeheaders)
);
}
}
Selanjutnya Anda perlu mendaftarkan fungsi tema baru yang akan memanggil file templat kustom Anda di hook_theme Anda ().
/**
* Implements hook_theme();
*/
function commerce_canvas_theme($existing, $type, $theme, $path) {
if($type == 'module') {
return array(
'tracking_code_email' => array(
'variables' => array(
'image_path' => NULL,
'user' => NULL,
'page' => NULL,
),
'template' => 'commerce-canvas-tracking-code',
'path' => drupal_get_path('module', 'commerce_canvas').'/theme',
),
);
}
}
Terakhir, Anda perlu memanggil fungsi tema ini saat mengirim email sebagai,
function a_custom_function($params) {
$email_text_user = theme('tracking_code_email', array(
'image_path' => $base_url . '/' . drupal_get_path('module', 'commerce_canvas') . '/images',
'user' => null,
'page' => array(
'greet_text' => t('Thank you for your order at Express Canvas. Your order is ready and has shipped. You may track your order using FedEx tracking with your tracking number: '.$order->field_tracking_number['und']['0']['value'].''),
),
));
$sent_to_user = commerce_canvas_mail_send($email_values_user);
}
Ini adalah cuplikan lengkap untuk menangani email HTML dengan benar di Drupal 7 sehingga di hook_theme Anda dapat memberikan templat khusus Anda.