9

I am using drupal_mail to send email,

$to = '[email protected]';
drupal_mail('$module_name', $key, $to, language_default(), $params = array('username' => 'Tanvir'), $from = NULL, $send = TRUE);

I want to send email to multiple email addresses at once. Should I be doing this?

$to  = array('[email protected]', '[email protected]', '[email protected]',);
drupal_mail('$module_name', $key, $to, language_default(), $params = array('username' => 'Tanvir'), $from = NULL, $send = TRUE);
2
  • check this post, dropbucket.org/node/308
    – shrish
    Commented Jun 6, 2014 at 12:21
  • Hey Tanvir, maybe consider accepting a different answer. The currently accepted answer entails the risk of running out of memory or time limit when processing many thousand emails like that.
    – leymannx
    Commented Mar 29, 2020 at 20:54

4 Answers 4

26

The to-parameter to drupal_mail is a string, not an array. But you can have as many receipent as you like in the the e-mails "to" string, provided they're separated by commas.

So to send the same mail to multiple recipients, do the following:

$to  = '[email protected],[email protected],[email protected]';
drupal_mail('$module_name', $key, $to, language_default(), $params = array('username' => 'Tanvir'), $from = NULL, $send = TRUE);

Se also API documentation for drupal_mail

1
1

Use hook_mail to send multiple receipients by passing:

$params = array();
$params['cc'][] = '[email protected]'
drupal_mail('$module_name', 'custom_key', $to, language_default(), $params = array('username' => 'Tanvir'), $from = NULL, $send = TRUE);


//hook will be the module name

function hook_mail($key,&$message,$params) {

  if ($key == 'custom_key') {
    $message['headers']['cc'] = $params['cc'];
    //.
    //.
    //.
    //.
  }    
}
1

According to PHP doc you can send only one letter via one call of function drupal_mail. So, you should use cycle. Example

$to  = array('[email protected]', '[email protected]', '[email protected]',);
foreach ($to as $email) {
  drupal_mail('$module_name', $key, $email, language_default(), $params = array('username' => 'Tanvir'), $from = NULL, $send = TRUE);
}
2
  • See the solution below from @FreeRadical for a simpler way of doing this.
    – Brady
    Commented Jul 12, 2015 at 7:46
  • I updated my message to avoid confusing. Thank you
    – Eugene
    Commented Jul 12, 2015 at 15:40
0

If you read drupal_mail function documentation then you'd see '$to' param accepts string not an array in certain formats like:

  1. [email protected]
  2. [email protected], [email protected]
  3. User
  4. User , Another User

So, we can easily convert your $to array into required format using implode function and hence you'll be to send email to multiple recipients in one go. Here's the code:

$to  = array('[email protected]', '[email protected]', '[email protected]',);

//To change array('[email protected]', '[email protected]', '[email protected]',)  -> '[email protected], [email protected], [email protected]' use implode

$to_str = implode(",", $to);

drupal_mail('$module_name', $key, $to_str, language_default(), $params = array('username' => 'Tanvir'), $from = NULL, $send = TRUE);
0

Your Answer

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

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