0

I have made a custom form from a custom module. I need to send an email to the admin when user clicks submit. How can I achieve this. Can I use entities here? Say like send mail to [site:mail] or something like that. Also, its pretty odd to see that Rules module does provide an event for this.

Here's my .module file:

<?php

global $user;

function my_module_help($path, $arg) {
  switch ($path) {
    case "admin/help#formtest":
      return '<p>' . t("Test form for accept challenge") . '</p>';
      break;
  }
}

function my_module_menu() {
  $items = array();
  $items['my_module/form'] = array(
    'title' => t('My form'),
    'page callback' => 'my_module_form',
    'access arguments' => array('access content'),
    'description' => t('My form'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function my_module_form() {

 return drupal_get_form('my_module_my_form');

}


function my_module_my_form($form_state) {

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Accept Challenge',
    '#submit' => array('my_module_action'),
    );

  return $form;
}

function my_module_action($form, &$form_state) {
  drupal_set_message(t('Challenge Accepted'));
  $query = db_insert('challenges')
    ->fields(array(
    'cid' => '$node->nid',
    'uid' => '$user->uid'
    ))
    ->execute();
}

As you may have noticed there's no text field, well that is by purpose. So any suggestions would be really appreciated. One more thing, drupal_mail, or any other, say for example, mail chimp, notify etc would be of interest. What I need is just a mail to be send to admin after submit from the code itself.

Thanks

4

You surely can send an email on the submit.

See the code below.

function my_module_action($form, &$form_state) {
  drupal_set_message(t('Challenge Accepted'));
  $query = db_insert('challenges')
    ->fields(array(
    'cid' => '$node->nid',
    'uid' => '$user->uid'
    ))
    ->execute();
  $message = array('A new entry has been submitted');
  $to = 'example@example.com';
  $params = array(
    'body' => $body,
     'subject' => 'Website Information Request',
  );
  if (drupal_mail('my_module', 'some_mail_key', $to, language_default(), $params)) {
        drupal_set_message('Thanks, email sent.');    
    } else {
        drupal_set_message('Email not sent.');
    }
}

You also need this function.

function my_module_mail($key, &$message, $params) {
  $message['subject'] = $params['subject'];
  $message['body'] = $params['body'];
}

in drupal_mail(), we pass $params array which contains parameters to build the email. If your module sends multiple emails, you can differentiate them using the $key variable. But in this example, we simple assign message's variables from the direct $params variables. Note that $message (in your submit handler) needs to be an array of text that you need to send. For example,

$body = array();
$body[] = 'Hello,';
$body[] = 'There is a new submission...';

This is because when the email gets sent, body content will be imploded by a new line character. Each value in the $body array will appear in a new line.

Also, as a fine tuning, you won't need my_module_form function if you format your hook_menu function like this:

function my_module_menu() {
  $items = array();
  $items['my_module/form'] = array(
    'title' => 'My form', // We don't t() menu titles.
    'page callback' => 'drupal_get_form', // Directly call drupal_get_form...
    'page arguments' => array('my_module_my_form'), // ...with this argument.
    'access arguments' => array('access content'),
    'description' => t('My form'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}
  • hi..thanks..will try these...just one follow up...is there any way to use entity in such custom codes – why Feb 7 '13 at 18:45
  • You can pass values to the submit function by adding them in $form_state or $form itself (see #type => value in form API docs). To load an entity, use entity_load or appropriate load functions (node_load(), user_load(), etc). You can access the current user object at $GLOBALS['user'] - $GLOBALS['user']->mail = user's email address, for example. – AyeshK Feb 7 '13 at 19:04
0

@ayesh's answer is accurate and is the recommended process for production sites (ie, use drupal_mail()), but if you only want a quick email for debugging, just use php's mail() function:

function my_module_action($form, &$form_state) {

  drupal_set_message(t('Challenge Accepted'));

  $query = db_insert('challenges')
    ->fields(array(
      'cid' => '$node->nid',
      'uid' => '$user->uid'
    ))
    ->execute();

  mail('you@email.com', 'form was submitted', 'someone submitted the form!');

}

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