Email using email_template in OpenERP 6.1

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. 

 

Comments

Marie: Hello, First of all thanks for your post!!! I have an issue with a template for meeting as I would like to send the mail to all attendees and not just to the partner contact. the object I take is ${object.attendee_ids} and the result is [browse_record(calendar.attendee, 148), browse_record(calendar.attendee, 149)] I need to get the emails of the attendee but don't know how to do this as it's a many2many field. Could you help me with this? I'm using openerp 6.1 Many Thanks MN ".

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Anonymous says:

    Hi Prajul,
    First of all, very good post.
    I experiencing some issues with sending a mail using the sidebar button created from the template and with the files attached.
    I created a template for sales.order. What I want to do is have a button on the side bar to send a mail to the partner with the sales quotation before I confirm the order. To send the quotation I want either attach the pdf or create a table within the body of the message with the html table with the order lines. I couldn’t achieve any case.
    To set up the template I create the structure of the body in both, plain and html text. At the tab ‘Advanced’ in the field ‘Optional report to print and attach’ I select ‘Quotation / Order’, and I added the sidebar button.
    When I send a mail by clicking on the sidebar button from the Sale order form, the pop-up shows up and I send the mail. But if I go to Settings->Configuration->Email->Messages the mail I am gonna send is there. But taking a look inside the body is plain (according to ‘Message type’ in tab ‘Advanced’ and what I see) and there is not any document attached. All this is verified when I send and I get the mail.
    I have several questions:
    To attach the quotation pdf, it has to be already created and attached to the order?
    Where can I define the type of the mail to html?
    Thank you in advance

    1. Marie says:

      Hello,
      First of all thanks for your post!!!
      I have an issue with a template for meeting as I would like to send the mail to all attendees and not just to the partner contact.
      the object I take is ${object.attendee_ids}
      and the result is [browse_record(calendar.attendee, 148), browse_record(calendar.attendee, 149)]
      I need to get the emails of the attendee but don’t know how to do this as it’s a many2many field.
      Could you help me with this?
      I’m using openerp 6.1
      Many Thanks
      MN

      1. Prajul P T says:

        Hi Marie,

        Please try 

        <% y = [z.email for z in object.attendee_ids] %>${','.join(y)} 

        where email is the field for email in the related model of attendee_ids.

        1. Anonymous says:

          Dear Prajul,
          I tried and it worked perfectly.
          I can’t thank you enough for your quick answer and your will to help python beginners like me.
          I’m a developper myself but in a complete different area. It helps to figure out but not always to find the correct formula.
          So, there, thanks again as I greatly appreciated your quick help and should be able to deliver in time.
          Kind Regards
          Marie

          1. Anonymous says:

            Prajul,
            I could need your help once again,
            I was thinking to use the formula given before in order to list also the details for each attendee in the email body.
            ATTENDEES :
            <% y = [z.partner_address_id.name for z in object.attendee_ids] %>
            % if y <> ‘None’:
            ${(y)}
            % endif
            The result is bellow. It is normal that both entries are None as they are internal users therefore there is no partner and I don’t want to list them.
            Also, I don’t understand why there is a u before the name.
            ATTENDEES :
            [None, None, u’Rudi Karel’]
            I’m digging arround in google but don’t really know the correct terms to research (am French). It’s a bit difficult to give more personnal info on a blog…
            Your blog is really good
            Kind regards
            Marie

          2. Anonymous says:

            Hi Prajul,
            Wanted to let you know that I finaly figured out how to this.
            The last issue I’m facing is to convert time UTC to locat time.
            Thanks
            Marie

        2. Marie says:

          Dear Prajul,
          I started to implement Openerp v7 and the code I used <% y = [z.email for z in object.attendee_ids] %>${‘,’.join(y)} is not longer working.
          I get errors like openerp.addons.email_template.email_template: failed to render mako template value u”<% y = [z.partner.name for z in object.partner_ids] %>${‘,’.join(y)}”
          Traceback (most recent call last):
          File “/opt/openerp/server/openerp/addons/email_template/email_template.py”, line 99, in render_template
          result = mako_template_env.from_string(template).render(variables)
          File “/usr/lib64/python2.6/site-packages/jinja2/environment.py”, line 669, in render
          return self.environment.handle_exception(exc_info, True)
          File “

          1. Prajul P T says:

            Hi,

                Can you please check if there is any line "<?php y = [z.partner.name for z in object.partner_ids] ?>${','.join(y)}" in your template. If yes please check if it is "z.name" instead of "z.partner.name".

          2. Anonymous says:

            Hi,
            I went a bit further I my searching. In V7 the email_template module is using jinja2 and with
            <% for z in object.attendee_ids %> ${z.partner_id.name} <${z.email|safe}>,<% endfor %>
            I get the mail_to.
            With v7 using inbox internal I still have an issue with having the template sent out. So I added as explained the attendee.partner_id to the Recipients [it’s a second line in the template] but the in email_compose_message is a error because it seems to be a space or an empty value.
            Am trying to figure out this part now.
            I’m not a python specialist so it takes more time for me to track errors -)
            OH. I just noticed I sent you initially a wrong code. I was looking for z.email and not partner. Sorry about this.

          3. fraud says:

            Right now it looks like WordPress is thhe top bloggung platform out there right now.
            (from what I’ve read) Is that what you are using on your blog?

      2. get more views says:

        Hello, I enjoy reading through your article. I like to write a little comment to support you.

        1. Prajul P T says:

          You are always welcome…

    2. positionnement google says:

      Hello i'm Lashawn , i'm on your page for the first time and i was searching about this topic , zesty beanz ,since 2 or 3 hours! Many thanks you make my day!! Keep Writing, it's so informative and cool news on Reply to comment | Zesty Beanz.

      1. Prajul P T says:

        Thank you sir, thank you very much……

    3. It’s difficult to find educated people in this particular topic, but you seem like you know what you’re
      talking about! Thanks

  2. Anonymous says:

    Hello,
    Followed above steps for my module latest_base. However system displays following error
    AttributeError: ‘latest_base’ object has no attribute ‘message_post’
    Any idea to fix this issue.. Thanks

© 2020 Zesty Beanz Pvt Ltd All Rights Reserved.