20

I am using Drupal 7 and focused to work with email option. I am using the Forward module. How can I add the BCC field in the drupal_mail() function.

my default function having,

drupal_mail('forward', 'forward_page', trim($to), language_default(), $params, $params['from']);

closed as too broad by kiamlaluno Jun 3 at 5:20

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

27

Everything you need is in the email message headers array.

$params['headers'] = array(
    'Bcc' => 'bcc_email@example.com',
    'Cc' => 'cc_email@example.com',
);

Here is an example implementation of drupal_mail() with bcc headers included.

$params = array(
    'body' => $body,
    'subject' => $subject,
    'headers' => array(
        'Bcc' => $header_bcc,
        'Cc' => $header_cc
    )
);

$email = drupal_mail('ModuleName', 'message_key', $to, LANGUAGE_NONE, $params, $from, true);

Using hook_mail() you need to add (thank you @clive):

/**
 * Implements hook_mail().
 */
function ModuleName_mail($key, &$message, $params) {
    switch ($key) {
        case 'message_key':
            $message['headers'] += $params['headers'];
    }
}
  • We added the code but it the cc and bcc fields are not working. Please give another solution. – sathish Aug 31 '12 at 11:15
3

You can use the hook mail alter for changing or adding altered to, cc and bcc mail ids, see the example:


/**
 * Implements hook_mail_alter().
 */
function hook_mail_alter(&$message) {
  $message['to'] = 'mail@gmail.com';
  $message['headers']['Bcc'] = 'Your mail ids goes here with comma seperation';
  $message['headers']['Cc'] = 'Your mail ids goes here with comma seperation';
}

Also you can use bcc and cc mail ids in the $params array of drupal_mail():


$params = array(
  'body' => $body,
  'subject' => 'Your Subject',
  'headers' => array(
    'Cc' => 'Your mail ids goes here with comma seperation',
    'Bcc' => 'Your mail ids goes here with comma seperation',
  ),
);
2

There's a module for that: If you want to get a bcc of all mails that drupal sends check out the BCC Module.

2

You can do this:

$message['headers']['Bcc'] = 'email@address.com';
1

In hook_mail_alter() use $message['params']['headers']['Bcc'] = 'yourmail@gmail.com';.

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