johnbocook
9/9/2015 - 3:04 PM

Use drupal_mail to send an email

Use drupal_mail to send an email

<?php

/**
 * Implements hook_menu()
 * Call a custom form as menu callback
 * @return
 *   $items array of menu items created programmatically
 */
function test_menu() {
  $items['test'] = array(
    'title' => t('Send Email Using Drupal mail'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('test_custom_form', 1),
    'access arguments' => array('access content'),
    //'file' => 'test.inc',
    //'file path' => drupal_get_path('module', 'test') . '/includes',
  );
  return $items;
}

/**
 * fucntion to return custom form on menu callback
 * @param 
 *   type $form an array consists of form fields
 * @param 
 *   type $form_state an array consists of form fields values input a user
 * @return 
 *   $form returns array of dorm fields.
 */
function test_custom_form($form, $form_state) {
  $form = array();
  $form['email'] = array(
    '#type' => 'textfield',
    '#title' => 'To Email',
    '#prefix' => '<div id="email-field-wrapper">',
    '#suffix' => '</div>',
    '#required' => TRUE,
  );
  $form['from_email'] = array(
    '#type' => 'textfield',
    '#title' => 'From Email',
    '#prefix' => '<div id="from-email-address">',
    '#suffix' => '</div>',
    '#required' => TRUE,
  );
  $form['email_body'] = array(
    '#type' => 'textarea',
    '#title' => 'Email Body',
    '#prefix' => '<div id="email-body">',
    '#suffix' => '</div>',
    '#required' => TRUE,
  );
  $form['submit_form'] = array(
    '#type' => 'submit',
    '#value' => t('Send Email'),
    '#submit' => array('test_custom_form_submit'),
  );
  $form['#validate'] = array('test_custom_form_validation');
  return $form;
}

/**
 * Email Validation function for custom form
 * @param type $form
 * @param type $form_state 
 */
function test_custom_form_validation($form, &$form_state) {
  $mail = $form_state['values']['email'];
  $from_email = $form_state['values']['from_email'];
  if (!valid_email_address($mail)) {
    form_set_error('email', t('Please Enter a valid to email address.'));
  }

  if (!valid_email_address($from_email)) {
    form_set_error('from_email', t('Please Enter a valid from email address.'));
  }
}

function test_custom_form_submit($form, &$form_state) {
  drupal_set_message("form is submitted, thanks");
  $email_content = get_mail_content($form_state);
  $params = array('body' => $email_content);
  $key = 'test_email';
  $to = $form_state['values']['email'];
  $from = $form_state['values']['from_email'];
  $mail = drupal_mail('test', $key, $to, language_default(), $params, $from);

 // dpm($mail);
}

/**
 * Implements hook_mail()
 * @param 
 *   type $key to decide which email body to sent on basis of key parameter inacese of multiple email content
 * @param 
 *   type $message the email content to be sent.Message array contains 'subject and body ' for the email.
 * @param 
 *   type $params using to get the cusotm email content from a function.This can be used in my other ways aslo as per need.
 */
function test_mail($key, &$message, $params) {
  $language = $message['language'];
  switch ($key) {
    //switching on $key lets you create variations of the email based on the $key parameter
    case 'test_email':
      $message['subject'] = t('Test Email');
      //the email body is here, inside the $message array
      $message['body'][] = $params['body'];
      break;
  }
}

/**
 * function to get the html formatted email content
 * @param 
 *   type $form_state to get values from form fields.
 * @return 
 *   $body type string return the html email content 
 */
function get_mail_content($form_state) {
  $email_to = $form_state['values']['email'];
  $pos = strpos($email_to, '@');
  $user_name = substr($email_to, 0, $pos);
  $body = '';
  $body .= 'Hi ' . $user_name . '<br>';
  $body .= 'Please find my test email. <br>';
  $body .= $form_state['values']['email_body'] . '<br>';
  $body .= 'Thanks<br>';
  $body .= 'TestTeam';
  return $body;
}