Send Email with Drupal's API drupal_mail

  • Posted on: 10 January 2015
  • By: oon

Untuk mengirim email dengan drupal_mail, banyak referensi menyarankan untuk membuat fungsi callback (yang akan dipanggil oleh drupal_mail).

Misalnya untuk $module = 'xxx_api';

Maka fungsi yang akan dipanggil adalah xxx_api_mail($key, &$message, $params)

function xxx_api_mail($key, &$message, $params) {
  global $user;

  $options = array(
    'langcode' => $message['language']->language,
  );

  $message['subject'] = t('E-mail from @site-name', array('@site-name' => variable_get('site_name', 'Sindhen')), $options);
  $message['body'][] = t('@name sent you the following message:', array('@name' => $user->name), $options);
  $message['body'][] = check_plain($params['message']);
}

Kemudian kita bisa selanjutnya memanggil fungsi drupal_mail dengan terlebih dahulu melengkapi sejumlah parameternya:

  $form_values = array(
    'email' => 'mail@nospam.oo.or.id',
    'message' => $data_message,
  );
  $module = 'xxx_api';
  $key = $data['code']; // random unique code
  $to = $form_values['email'];
  $from = variable_get('site_mail', 'admin@your-drupal.com');
  $language = language_default();
  $send = TRUE;
  $result = drupal_mail($module, $key, $to, $language, $form_values, $from, $send);
  if ($result['result'] == TRUE) {
    drupal_set_message(t('Your message has been sent.'));
  }
  else {
    drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error');
  }

Untuk tau lebih detil, baca dokumentasi drupal_mail :)

Tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.