8

I am using drupal_mail to send email,

$to = 'email@email.com';
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('one@email.com', 'two@email.com', 'three@email.com',);
drupal_mail('$module_name', $key, $to, language_default(), $params = array('username' => 'Tanvir'), $from = NULL, $send = TRUE);
  • check this post, dropbucket.org/node/308 – shrish Jun 6 '14 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 Mar 29 at 20:54
24

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  = 'one@email.com,two@email.com,three@email.com';
drupal_mail('$module_name', $key, $to, language_default(), $params = array('username' => 'Tanvir'), $from = NULL, $send = TRUE);

Se also API documentation for drupal_mail

| improve this answer | |
  • i only say that when i try $to = 'one@email.com, two@email.com, three@email.com'; I receive only one email, but when I try $to = 'one@email.com,two@email.com,three@email.com'; without spaces I received all emails! – Michael Feb 28 '17 at 10:06
1

Use hook_mail to send multiple receipients by passing:

$params = array();
$params['cc'][] = 'abc@ex.org'
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'];
    //.
    //.
    //.
    //.
  }    
}
| improve this answer | |
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('one@email.com', 'two@email.com', 'three@email.com',);
foreach ($to as $email) {
  drupal_mail('$module_name', $key, $email, language_default(), $params = array('username' => 'Tanvir'), $from = NULL, $send = TRUE);
}
| improve this answer | |
  • See the solution below from @FreeRadical for a simpler way of doing this. – Brady Jul 12 '15 at 7:46
  • I updated my message to avoid confusing. Thank you – Eugene Jul 12 '15 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. user@example.com
  2. user@example.com, anotheruser@example.com
  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('one@email.com', 'two@email.com', 'three@email.com',);

//To change array('one@email.com', 'two@email.com', 'three@email.com',)  -> 'one@email.com, two@email.com, three@email.com' use implode

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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