How to use a drupal_mail function.
Let's say you want to send mail using drupal_mail function in your "test" custom module (test is an example name of module used below) after the form is submitted. You have to build two functions. First is a test_form_submit function, the second one is the test_mail function.
In the submit function you have to put the code below:
<?php
function test_form_submit($form, &$form_state) {
$to = "test@example.com"; // to e-mail address
$from = "test@example.com"; // from e-mail address
$subject = "text to display in e-mail subject"; // subject of e-mail
$body = "text to display in e-mail body"; //it might be any variable from the form eg. $form_state['values']['your_field']
//params is the array passed to hook_mail function
$params = array(
'subject' => $subject,
'body' => $body,
);
drupal_mail('test', 'information', $to, language_default(), $params, $from);
}
?>
The second function is a test_mail function:
<?php
function test_mail($key, &$message, $params) {
switch ($key) {
case 'information':
$message['subject'] = $params['subject'];
$message['body'] = $params['body'];
break;
}
}
?>
And that's it! Hope you'll find it useful :)
Post new comment