Search code examples
python-2.7emailgmailodooodoo-8

How to send a mail with BCC to some recipients in Odoo 8?


I have created a module to send Blind Carbon Copy to specific users. To do this I have only inherited some models and modified a few lines:

Model email.template

I have added the next field:

email_bcc = fields.Char(string='Bcc',
                        help='Blind carbon copy message recipients')

And I have modified the following methods:

generate_email_batch, to modify the following line:

fields = ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to']

And turn it into the next one:

fields = ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'email_bcc', 'reply_to']

generate_recipients_batch, to modify the following line:

mails = tools.email_split(values.pop('email_to', '')) + tools.email_split(values.pop('email_cc', ''))

And turn it into the next one:

mails = tools.email_split(values.pop('email_to', '')) + tools.email_split(values.pop('email_cc', '')) + tools.email_split(values.pop('email_bcc', ''))

Model mail.mail

I have also added a field:

email_bcc = fields.Char(string='Bcc',
                        help='Blind carbon copy message recipients')

And modified the method send, to modify this line:

msg = ir_mail_server.build_email(
    email_from=mail.email_from,
    email_to=email.get('email_to'),
    subject=email.get('subject'),
    body=email.get('body'),
    body_alternative=email.get('body_alternative'),
    email_cc=tools.email_split(mail.email_cc),
    reply_to=mail.reply_to,
    attachments=attachments,
    message_id=mail.message_id,
    references=mail.references,
    object_id=mail.res_id and ('%s-%s' % (mail.res_id, mail.model)),
    subtype='html',
    subtype_alternative='plain',
    headers=headers
)

And turn it into the next one:

msg = ir_mail_server.build_email(
    email_from=mail.email_from,
    email_to=email.get('email_to'),
    subject=email.get('subject'),
    body=email.get('body'),
    body_alternative=email.get('body_alternative'),
    email_cc=tools.email_split(mail.email_cc),
    email_bcc=tools.email_split(mail.email_bcc),
    reply_to=mail.reply_to,
    attachments=attachments,
    message_id=mail.message_id,
    references=mail.references,
    object_id=mail.res_id and ('%s-%s' % (mail.res_id, mail.model)),
    subtype='html',
    subtype_alternative='plain',
    headers=headers
)

In both models, I am totally overwriting the methods (I was not able to find the way to modify them with a super).

The problem

I've created an email template to check email_bcc, and I call from code the method send_mail of email.template:

mails_sent &= et_pool.send_mail(
    self.env.cr, 1, template.id,
    user.id, force_send=True, context=ctx
)

First of all, when the mail is sent, I get this error in log:

openerp.addons.email_template.email_template: Failed to render template using values {'format_tz': at 0x7f170051a8c0>, 'ctx': {'lang': 'es_ES', 'tz': 'Europe/Madrid', 'search_default_my_sale_orders_filter': 1, 'params': {'action': 318}, 'uid': 1}, 'user': res.users(1,), 'object': res.users(2,)}

However, the mail is being sent rightly. But something surprising is always happening, I put an example to make you understand better:

My outgoing mail server is configured to send mails from the address [email protected]. The email template has the address [email protected] in the email_to field, and the address [email protected] in the email_bcc field.

After I sent the email, if I log in Google account [email protected], and I go to Inbox, I can see the email, but I can read that [email protected] sent the email, that it was sent to me, and that a blind carbon copy was sent to [email protected]!

How is this possible? It seems that my module has worked well and sent the email in BCC to [email protected], but why the [email protected] owner can see that fact???

Anyone can help me please?

If someone is interested in checking the module, it is here:

https://github.com/forvas/social/tree/8.0

Its name is mail_bcc.


Solution

  • I had the same problem recently and I don't think it's the module's fault, but Google's SMTP server (I assume you configured one because of the @gmail.com addresses). When I switched from GMail to our company's mail server, the BCC field was properly handled, so the mail was sent to the BCC recipient but it was not visible to the people in TO and CC.