social icon indicating copy to clipboard operation
social copied to clipboard

[17.0] mail_composer_cc_bcc Error on send mail with BCC

Open FlorianGenicq opened this issue 6 months ago • 3 comments

When i try to send email with mail.compose.message like that for example : ctx = { 'default_model': 'training.session', 'default_use_template': bool(template.id), 'default_template_id': template.id, 'default_composition_mode': 'comment', 'default_partner_ids': [(6, 0, self.company_id.partner_id.ids)], 'default_partner_bcc_ids': [(6, 0, bcc_partner_ids.ids)], 'default_freeze_partner_ids': True, } return { 'type': 'ir.actions.act_window', 'view_mode': 'form', 'res_model': 'mail.compose.message', 'views': [(False, 'form')], 'view_id': False, 'target': 'new', 'context': ctx, }

The outgoing server mail return this error : There may be at most 1 Bcc headers in a message

I have tried differents ways to resolve this issue but nothing work.

I think there are a problem with this addon.

FlorianGenicq avatar May 30 '25 10:05 FlorianGenicq

@trisdoan Do you have any idea for this issue ?

FlorianGenicq avatar Jun 02 '25 12:06 FlorianGenicq

The error provide from this : class IrMailServer(models.Model): _inherit = "ir.mail_server"

def _prepare_email_message(self, message, smtp_session):
    """
    Define smtp_to based on context instead of To+Cc+Bcc
    """
    x_odoo_bcc_value = next(
        (value for key, value in message._headers if key == "X-Odoo-Bcc"), None
    )
    # Add Bcc field inside message to pass validation
    if x_odoo_bcc_value:
        message["Bcc"] = x_odoo_bcc_value

    smtp_from, smtp_to_list, message = super()._prepare_email_message(
        message, smtp_session
    )

    is_from_composer = self.env.context.get("is_from_composer", False)
    if is_from_composer and self.env.context.get("recipients", False):
        smtp_to = self.env.context["recipients"].pop(0)
        _logger.debug("smtp_to: %s", smtp_to)
        smtp_to_list = [smtp_to]

    return smtp_from, smtp_to_list, message

the header already exist, so when it pass on these lines it raise the error : if x_odoo_bcc_value: message["Bcc"] = x_odoo_bcc_value

i resolve the error like this : if "Bcc" not in message: message["Bcc"] = x_odoo_bcc_value

But i'm not sure it really consistent and the best way

FlorianGenicq avatar Jun 02 '25 14:06 FlorianGenicq

Hello, I am still able to send mail w/ cc/bcc via Mail Composer. Could you elaborate on the flow you did? Or any add-on you install?

trisdoan avatar Jun 20 '25 07:06 trisdoan