How it works...

In step 1, we created the mail.alias record. This alias will handle the [email protected] email address. When you send the mail to this address, Odoo will create a new record in the library.book.rent model. If you want to see the list of active aliases in the system, open Setting|Technical|Email|Aliases. Here is the list of fields available to configure the alias:

  • alias_name: This field holds the local part of the email address, for example, the rent in the [email protected] is the local part of the email address.
  • alias_model_id: The model reference on which the record should be created on incoming mail.
  • alias_user_id: When incoming mail is received, records are created with the environment of the user in this field.
  • alias_contact: This field holds the security preferences for the alias. Possible options are everyone, partners, followers, and employees.
  • alias_defaults: When incoming mail is received, its record is created in the model specified on the alias. If you want to set default values in the record, give the values in the form of a dictionary in this field.

In step 2, we added the necessary imports.

In step 3, we overrode the message_new() method. This method is invoked automatically when a new email is received on the alias email address. This method will take two parameters:

  • msg_dict: This parameter will be the dictionary that contains information about the received email. It contains email information such as the sender's email address, the receiver's email address, email subject, and email body.
  • custom_values: This is a custom value used to create a new record. This is the same value you set on the alias record using the alias_defaults field.

In our recipe, we overrode the message_new() method and fetched the book's title from the mail subject through a regular expression. Then we fetched the email address of the sender with the help of the tools we imported in step 2. We used the sender email address to find the borrower's record. Then we updated custom_values with these two values: books_id and borrower_id. We pass this updated custom_values data to the super() method, which will create a new library.book.rent record with the given books_id and borrower_id. This is how the record is created when you send an email to the alias.

Note that this recipe generates an error when you don't send the proper email subject, such as [book name] remaining subject. You can update the program according to your business logic to avoid errors.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset