In Odoo, you can use the message_post method to send a message and create a mail thread. Here's an example of how you might use this method to send a message:
# Send a message on a record
record.message_post(
body='This is the message body',
subject='This is the subject',
partner_ids=[partner_id], # list of recipient partner ids
attachment_ids=[attachment_id], # list of attachment ids
message_type='comment', # type of message (comment, email, etc.)
subtype='mail.mt_comment', # subtype of message
author_id=self.env.user.partner_id.id, # id of the message's author
)
# Send a message on a model
self.env['mail.thread'].message_post(
body='This is the message body',
subject='This is the subject',
partner_ids=[partner_id], # list of recipient partner ids
attachment_ids=[attachment_id], # list of attachment ids
message_type='comment', # type of message (comment, email, etc.)
subtype='mail.mt_comment', # subtype of message
author_id=self.env.user.partner_id.id, # id of the message's author
)
The message_post method creates a new mail.message record and adds it to the mail thread associated with the record or model. The message will be sent to the specified recipients (partner_ids) and will include any specified attachments (attachment_ids).
You can also use the mail.compose.message model to create and send a message. This allows you to specify additional options, such as the message template and the email addresses of the recipients. Here's an example of how you might use this model to send a message:
compose_form = self.env.ref('mail.email_compose_message_wizard_form')
ctx = {
'default_model': 'my.model',
'default_res_id': self.id,
'default_use_template': bool(template_id),
'default_template_id': template_id,
'default_composition_mode': 'comment',
'mark_so_as_sent': True,
}
return {
'name': _('Compose Email'),
'type': 'ir.actions.act_window',
'view_mode': 'form',
'res_model': 'mail.compose.message',
'views': [(compose_form.id, 'form')],
'view_id': compose_form.id,
'target': 'new',
'context': ctx,
}
This will open a wizard that allows the user to compose and send an email message. The message will be added to the mail thread associated with the specified record or model