0

I've looking for in google then finally I got some example working for me but when I start to manualy code making my cutom module send email it doesnt work ? then I try to copy paste in the new module just modif the name of module still doesnt work it says

Unable to send e-mail. Contact the site administrator if the problem persists.

Here's the original code that working

<?php

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'),
  );
  return $items;
}
function test_custom_form($form, $form_state) {
  $form = array();
  $form['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#required' => TRUE,
    );

  $form['email'] = array(
    '#type' => 'textfield',
    '#title' => 'To Email',
    '#prefix' => '<div id="email-field-wrapper">',
    '#suffix' => '</div>',
    '#required' => TRUE,
  );
  $form['from_email'] = array(
    '#type' => 'hidden',
    '#title' => 'From Email',
    '#value' => '[email protected]',
    '#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;
}
/**
 * validation function for custom form
 * check for vadlidation for email address.
 * @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,
    'subject' => $form_state['values']['subject']);
  $key = 'test_email';
  $to = $form_state['values']['email'];
  $from = $form_state['values']['from_email'];
  $mail = drupal_mail('test', $key, $to, language_default(), $params, $from);
  // for better understanding,you can uncomment the following
 // 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'] = $params['subject'];
//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'];
  $body = '';
  $body .= $form_state['values']['email_body'];
  return $body;
}

this my code doesnt working

    <?php 
    function email_permission() {
      return array(
        'administer my module' => array(
          'title' => t('Administer my module'),
          'description' => t('Perform administration tasks for my module.'),
        ),
      );
    }

    function email_menu() {
      $items['email'] = array(
        'title' => t('Kirim Email'),
        'page callback' => 'email_output',
        'access arguments' => array('administer my module'),
        );
      return $items;
    }

    function email_output() {
      $output = drupal_get_form('email_form');
      return $output;
    }

    function email_form($form, &$form_state) {
      $form = array();
      $form['subject'] = array(
        '#type' => 'textfield',
        '#title' => t('Subject'),
        '#required' => TRUE,
        );
      $form['email'] = array(
        '#type' => 'textfield',
        '#title' => t('Ke Email'),
        '#required' => TRUE,
        );
      $form['dari_email'] = array(
        '#type' => 'hidden',
        '#value' => '[email protected]',
        );
      $form['pesan'] = array(
        '#type' => 'textarea',
        '#title' => t('Pesan'),
        '#required' => TRUE,
        );
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Kirim email'),
        );
      return $form;
    }

    function email_form_validate($form, &$form_state) {
      $mail = $form_state['values']['email'];
      $dari_email = $form_state['values']['dari_email'];
      if (!valid_email_address($mail)) {
        form_set_error('email', t('Silahkan Masukan format email yang benar'));
      }
      if (!valid_email_address($dari_email)) {
        form_set_error('dari_email', t('Silahkan Masukna Format email yang benar.'));
      }
    }

    function email_form_submit($form, &$form_state) {
      drupal_set_message("Pesan telah terkirim");
      $email_pesan = email_ambil_pesan($form_state);
$subject1 = $form_state['values']['subject'];
      $params = array('body' => $email_pesan
'subject' => $subject1);
      $key = 'email_email';
      $to = $form_state['values']['email'];
      $from = $form_state['values']['dari_email'];
      $mail = drupal_mail('test', $key, $to, language_default(), $params, $from);
    }

    function email_mail($key, &$message, $params) {
      $language = $message['language'];
      switch ($key) {
        case 'email_email':
          $message['subject'] = $params['subject'];
          $message['body'] = $params['body'];
          break;
      }
    }

    function email_ambil_pesan($form_state) {
      $ke_email = $form_state['values']['email'];
      $body = '';
      $body .= $form_state['values']['pesan'];
      return $body;
    }
    ?>

What really mean Unable send email contact administrator ? please help..

1
  • If you're not using SMTP module, the e-mail address in /admin/config/system/site-information must be [email protected] of your site.
    – No Sssweat
    Commented Jun 20, 2017 at 1:12

1 Answer 1

1

If you are seeing that message, it means that your code to send the email is properly going through all the way to the sending of the mail. It's either Drupal or your server that is unable to send the mail after that point. Do you have a mail server on your server? If not, then try downloading the SMTP module, and using a remote mail server (gmail for example).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.