Friday 8 June 2012

Email using email_template in OpenERP 6.1

Courtesy :- Prajul P T


Email_template is a default module of OpenERP which is used to send emails from OpenERP. It is much easier than smtp_client and the template of the mail can be easily customized. We can configure outgoing email server and template of email_template from Setting > Configuration > Email. The outgoing mail server of email_template can be configured from "Outgoing Mail Servers" in  Setting > Configuration > Email. The following is a screenshot of configuring outgoing mail server in email_template.





In outgoing mail server menu, there are some mandatory fields which has to be configured by the user. These parameters are used to configure the mail server. 
  • The SMTP Server : IP or Host name of SMTP server
  • SMTP Port : SMTP Port
  • Debugging : If enabled, full output of the SMTP session will be written to server log at debug level
  • Connection Security : Connection security schema
  • Username : Username of the email
  • Password : Password of the email
Another important feature of email_template is the Template which can be found in Setting > Configuration > Email > Template. This is used to specify the format in which the email has to be send. A screenshot of the Template in email_template is shown below: 




In template, we can set 
  • Name : Name of the template
  • Related document model : Model to which the template is related
In Email Detail Tab, we can set:
  • From : Sender address
  • To : Destination mail address
  • CC : CC Address if required
  • BCC : BCC Address if required
  • Reply To : Preferred Reply address
  • Subject : Subject of the mail
  • Body : Body of the email
Body of the email can be either plain text or an HTML format. In plain text the body can be created using the plain text format and in HTML we can create the mail body in HTML. We also have the freedom to select the language in which the mail is to be send. This is specified in Language selection field in Email Detail Tab.

Another main feature of email_template is the freedom to dynamically specify the field values like From, To, etc. The mail template can be customized any time without modifying the code. Even a person without technical expertise in OpenERP coding can  set the fields using Dynamic Value Builder.

Dynamic Value Builder consist of following fields:
  • Field : Select field from Related Document Model
  • Sub-Model : When a relation field like many2one is selected, this field specify the Related model the relation goes to.
  • Sub-field : When a relation field is selected, this field let us select the target field inside the Sub-Model
  • Null Value : Optional value to use when the target field is empty or Null
  • Expression : This is the resulting expression generated when the fields are selected in Dynamic Value Builder. This can be copy pasted in fields in email template.
In Advanced tab, We can select
  • Outgoing Mail Server : Outgoing Mail Server configured before
  • Resource Tracking : Enables us to track the mail and to map the replies to the corresponding resource record
  • Auto Delete : Permanently delete the email after sending it to save space
In this tab, we have an option to attach files to email. We can see an button Add Sidebar Button. This creates a button in sidebar in the related document model form view which helps in sending email any time from the form view. For example I have created a template for the sale.order and a sidebar button is created by clicking "Add Sidebar Button" button in template, a new button will be created as shown below:





When this button is clicked, a window pops up using which we can send email as shown below:





There can be situation in which client want to send email when a particular button is clicked. For example if a client want to send a mail to the hr manager when a leave application by an employee has been confirmed. For this create a template for hr_holidays model, inherit the button action to confirm the leave from which the mail has to be send, Search and find the id of the template using which the mail has to be send, call the function send_mail in email_template and pass the arguments like template id, resource id to it. Mail will be send every time the button is clicked. 




No comments:

Post a Comment