3

I am developing a Drupal 6 module that should send an email after a form is submitted. So far, I wrote the following code.

function mailform_mail($key, &$message, $params) {
  $headers = array(
    'MIME-Version' => '1.0',
    'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
    'Content-Transfer-Encoding' => '8Bit',
    'X-Mailer' => 'Drupal'
  );

  foreach ($headers as $key => $value) {
    $message['headers'][$key] = $value;
  }
  $message['subject'] = $params['subject'];
  $message['body'] = $params['body'];
}

/**
 * Form submission handler.
 */
function mailform_form_submit($form, &$form_state) {
  $valid_email = $form_state['values']['email'];
  $subject = $form_state['values']['Subject'];
  $body = $form_state['values']['body'];
  $params = array(
    'email'=> $valid_email,
    'body' => $body,
    'subject' => $subject,
  );

 // The first argument of drupal_mail() function must match the first part of the hook_mail function.
 // Therefore the first argument is  'mailform_form' and the the mail hook is 'mailform_form_mail'
 // For more infoormation see:
 // - http://api.drupal.org/api/function/drupal_mail/6
 // - http://api.drupal.org/api/function/hook_mail/6
 if (drupal_mail('mailform','reply', $valid_email, language_default(), $params, $from, TRUE) {
    drupal_set_message('An email has been sent to ' . $valid_email);
  }
  else {
    drupal_set_message('There was an error sending your email');
  }
}

Using this code, I can get the values after a form is submitted, but the mail is not sent out.

Is there anything wrong with the code? The module machine name is mailform.

1
  • 1
    You need to clear the cache after renaming the hook function
    – Clive
    Nov 12 '13 at 14:00
0

The answer's in your comments

//Therefore the first argument is  'mailform_form'

But you're using the string mailform as the first argument.

If your module is called mailform_form as your comments suggest, use this code:

drupal_mail('mailform_form','some_mail_key', $valid_email, language_default(), $params, $from, TRUE)

If it's named mailform, use your original call to drupal_mail, but change the name of your hook function to the correct one:

function mailform_mail($key, &$message, $params) {
3
  • @Clive.I have changed my code and executed after form submission its shows blank page.
    – Rahman
    Nov 12 '13 at 14:00
  • Check your server logs for PHP errors
    – Clive
    Nov 12 '13 at 14:01
  • @Rahman And clear the cache, since Drupal caches the list of hooks implemented by a module. If you edit the code of an enabled module you need to clear the cache to let Drupal notice the code change.
    – apaderno
    Jan 15 '16 at 7:54
0

The first argument of drupal_mail() is the module machine name, not the first part of the name for the function calling drupal_mail().

Since in the shown code there is a mailform_mail() function (which a hook_mail() implementation) and a mailform_form_submit() function (which is a form submission handler), I take the module machine name is mailform. Therefore, those function names are correct, and the email isn't sent because other issues.
For example, mailform_form_submit() is called as form submission handler if the form has a submission module and the form builder is mailform_form(). Otherwise, the submission handler won't be called and the email won't be sent.

To verify which is the module machine name, look at the name of the file that has .module as extension.
If that file is mailform.module, the code is correct.
If that file is mailform_form.module, the code needs to be changed, since:

  • All the hook implementations for that module needs to have a name starting with mailform_form_. Instead of mailform_mail(), it must be mailform_form_mail().

  • mailform_form() as form builder should be avoided, as the function name would only contain the machine name. For example, I would call the form builder mailform_send_form() and the form submission handler mailform_send_form_submit().

  • The code sending the email should use drupal_mail('mailform_form','reply', $valid_email, language_default(), $params). I omitted the last two parameters, as:

    • The default value of the last parameter is TRUE; it's useless to use that value
    • $from hasn't been initialized; it's like passing NULL for the sixth parameter, which uses that value as default one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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