By starscream on
Hey there,
Does anyone know how to make a simple form-to-email snippet?
This topic doesn't work in v6
on account of what seems to be substantial changes in the form API in 6
I managed to have some luck using the snippet on the drupal_mail API as a starting point
http://api.drupal.org/api/function/drupal_mail/6
but for the life of me I cannot successfully propagate the the two form values I want over to the mail function. The email is sent, but no matter where I put the variables i can't get them into the right call in the drupal_mail function.
<?php
function test_form() {
$form['expert'] = array(
'#type' => 'textfield',
'#title' => 'I\'m an expert in',
'#size' => '20',
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => 'Email Address',
'#size' => '20',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function test_mail($key, &$message, $params) {
$language = $message['language'];
switch($key) {
case 'test':
$expert = $form_state['values']['expert'];
$email = $form_state['values']['email'];
$message['subject'] = t('New expert submission');
$message['body'] = $expert;
##$message['to'] = $email;
break;
}
}
return drupal_get_form('test_form');
function test_form_submit($form, &$form_state) {
$expert = $form_state['values']['expert'];
$email = $form_state['values']['email'];
$message['subject'] = t('New expert submission');
$message['body'] = $expert;
$message['to'] = $email;
$from = 'me@example.com';
$params = $message;
drupal_mail('test', 'test', 'me@example.com', 'en', $message, $from, $form_state);
drupal_set_message(t('Your form has been saved.'. $expert));
}
?>
Am I losing my mind? This should be really easy shouldn't it be? What am I doing wrong here?
Comments
anyone? help? please?
anyone? help? please?
params, not form_state
Sorry so late on the reply. Looks like in your test_mail function you should get the values from the $params array, and not from $form_state.
Edit - I just looked at this old post and realized my original reply wasn't useful. In case anyone is still working with D6:
D6 syntax: drupal_mail($module, $key, $to, $language, $params = array(), $from = NULL, $send = TRUE)
$language should be an object
$key should be a key defined in your module's _mail function
the last parameter should be TRUE or FALSE
Your example could be updated to:
$lang = language_default(); // or use this user's language
drupal_mail('test', 'test', 'me@example.com', $lang, $message, $from, TRUE);