5

I have these two functions, the first is to send the email via drupal_mail() function :

function example_function_name($what) {
  drupal_mail('MYMODULE', 'some_mail_key', '[email protected]', language_default());
}

And the second is the hook_mail function :

function MYMODULE_mail($key, $message, $params){
    switch ($key){
        case 'some_mail_key': 
            $message['subject'] = 'test subject';
            $message['body'] = 'test body message';
            break;
    }
}

But I recieve a blank email in spam without neither subject, or body message. Is there somthing missing here ?

Thanks in advance.

1 Answer 1

7

hook_mail is supposed to take message by reference. The way you do it, you are modifying a local copy, and your modifications cannot ever leave the function's body. It should look like this:

function MYMODULE_mail($key, &$message, $params){
    switch ($key){
        case 'some_mail_key': 
            $message['subject'] = 'test subject';
            $message['body'] = 'test body message';
            break;
    }
}

This little ampersand is what makes all the difference. You cannot omit them. If API defined hook with reference argument, keep it a reference argument, or expect problems.

2
  • @Boussettamoez I know that feeling, I know. You should see some of my questions ;)
    – Mołot
    Dec 27, 2013 at 8:26
  • I already had spent the time you posted the question and Molot answered it, I could not make to see that.
    – arpitr
    Dec 27, 2013 at 8:39

Your Answer

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

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