How it works...

In step 1, we created a mail template using Jinja. Jinja templates help us generate a dynamic email based on record data. The mail template is stored in the mail.template model. Let's see the list of fields you will need to pass in order to create a Jinja mail template:

  • name: The name of the template that is used to identify a specific template.
  • email_form: The value of this field will be the email address from which this email is sent.
  • email_to: The value of this field will be the email address of the recipient.
  • subject: This field contains the subject of the email.
  • model_id: This field contains the reference of the model. The mail template will be rendered with the data of this model.
  • body_html: This field will contain the body of the email template. It is a Jinja template so you can use variables, loops, conditions, and so on. If you want to learn more about Jinja templates, go to http://jinja.pocoo.org/docs/2.10/. Usually we wrap the content in the CDATA tag so the content in the body is considered as character data and not as a markup.
  • auto_delete: This is the Boolean field that deletes the email once the email is sent. The default value of this field is False.
  • lang: This field is used to translate the mail template into another language.
You can use ${} in the email_form, email_to, subject, and lang fields. This helps you to set values dynamically. Take a look at step 1 in our recipe—we used ${object.borrower_id.email} to set the email_to field dynamically.

If you look closely at the content of the body_html field, you will notice we used ${object.borrower_id.name}. Here, the object is the record set of the library.book.rent model. During the rendering, ${object.borrower_id.name} will be replaced with the borrower's name. Like object, some other helper functions and variables are passed in the rendering context. Here is the list of helpers passed to the renderer context:

  • object: This variable will contain the recordset of the model, which is set in the template by the model_id field
  • format_date: It is a reference to the method used to format date-time objects
  • format_tz: It is a reference to the method used to convert UTC date time into the time zone based date time
  • format_amount: It is a reference to the method used to convert float into string with the currency symbol
  • user: This will be the record set of the current user
  • ctx: It will contain the dictionary of the environment context

In step 2, we registered the template file in the manifest file.

In step 3, we added a button in form view to invoke the book_return_reminder() method, which will send the email to the followers.

In step 4, we added the book_return_reminder() method, which will be invoked by clicking the button. The message_post_with_template() method is used to send the email. The message_post_with_template() method is inherited in the model through mail.thread inheritance. To send the email, you just need to pass the template ID as the parameter.

If you want to see the list of templates, activate developer mode and open the Settings|Technical|Email|Templates menu. The form view of the template also provides a button to preview the rendered template.
..................Content has been hidden....................

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