Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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', 'myaddress@gmail.com', 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.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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.

share|improve this answer
2  
thanks, sometimes we need to wear our glasses :p –  Boussetta moez Dec 27 '13 at 8:25
    
@Boussettamoez I know that feeling, I know. You should see some of my questions ;) –  Mołot Dec 27 '13 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 '13 at 8:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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