How to use self.env 'mail.mail on odoo 10
In Odoo 10, self.env
is a way to access the current environment of the application, which provides a way to interact with the Odoo system.
mail.mail
is a model in Odoo that allows you to send emails. To use it, you can follow these steps:
-
First, you need to import the
mail.mail
model in your Python code:from odoo import models from odoo.mail import mail
-
Then, you can use the
mail.mail
model to send an email. Here is an example:class MyModel(models.Model): _name = 'my.model' def send_email(self): mail_server = self.env['ir.mail_server'].search([], limit=1) mail_message = mail.mail( subject='Test email', body='This is a test email', email_from='[email protected]', email_to='[email protected]', mail_server_id=mail_server.id ) mail_message.send()
In this example, we are sending an email using the
mail.mail
model. We first search for a mail server using their.mail_server
model, and then we create a newmail.mail
object with the subject, body, sender, and recipient. Finally, we send the email using thesend()
method.
You can also use the mail.mail
model to send emails in a more complex way, such as sending emails with attachments or using a specific mail server.
Here is an example of sending an email with an attachment:
class MyModel(models.Model):
_name = 'my.model'
def send_email_with_attachment(self):
mail_server = self.env['ir.mail_server'].search([], limit=1)
mail_message = mail.mail(
subject='Test email with attachment',
body='This is a test email with attachment',
email_from='[email protected]',
email_to='[email protected]',
mail_server_id=mail_server.id,
attachment_ids=[(4, attachment.id) for attachment in self.env['ir.attachment'].search([])]
)
mail_message.send()
In this example, we are sending an email with an attachment. We first search for attachments using the ir.attachment
model, and then we add them to the mail.mail
object using the attachment_ids
parameter.
You can also use the mail.mail
model to send emails in a more complex way, such as sending emails with multiple recipients or using a specific mail server.