Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
572 Lượt xem

Hello Guys,

Im running Odoo.sh on version 18.

I createt a record of a template with following entrys. It gets called from a other module an through a comand with context. Some how the mail_to field doesnt get updatet. 

Can some help me wtih this?

If I use the comment field everything works.

email_adress = "test@dummy.com"

template = self.env.ref("reminder_activity_cron.email_template_remindermail_aktivity")

template = template.with_context(user=f"{email_adress}", displayname = displayname, resName = resName, link= link).send_mail(self.id, force_send=True)

<odoo>

    <data noupdate="0">

        <record id="email_template_remindermail_aktivity" model="mail.template">

            <field name="name">Remindermail aktivity</field>

            <field name="model_id" ref="mail.model_mail_activity"/>

            <field name="subject">Reminder Task due Today</field>

            <field name="email_from">reminder@reminder.com</field>

            <field name="description">Rindermail for Task due Today</field>

        <!--<field name="email_to">bot@bot.com</field>-->

            <field name="email_to">{{user}}</field>

            <field name="body_html" type="html">

<!-- Body is a littel bit longer but is working-->

​</field>

        </record>

    </data>

</odoo>

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

Hi,

 the solution from  Desk Enterprise worked for me. 

After a bit of testing the ctx.get() funktion ist the way to go.

Thank you for the help.

<field name="email_to">{{ ctx.get('user', '') }}</field>

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hii,

{{user}} Jinja variable doesn't automatically pick up the context value unless you reference it explicitly as ctx['user'] in the template.

Because when Odoo renders email templates, it uses ctx to refer to the rendering context passed from with_context().

so please try below code 
<record id="email_template_remindermail_aktivity" model="mail.template">

    <field name="name">Remindermail aktivity</field>

    <field name="model_id" ref="mail.model_mail_activity"/>

    <field name="subject">Reminder Task due Today</field>

    <field name="email_from">reminder@reminder.com</field>

    <field name="description">Reminder mail for Task due Today</field>

    <field name="email_to">{{ ctx.get('user', '') }}</field>

    <field name="body_html" type="html">

        <!-- Body is a littel bit longer but is working-->

    </field>

</record>


i hope it is usefull



Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,


The problem is related to how the context variables are passed and rendered in the mail.template. The email_to field in the template expects a valid Jinja expression, and you're passing {{ user }} which is correct syntax only if the context is set up correctly.


In your code, you're using user=f"{email_adress}", but this should be passed as user in the rendering context, not just the environment context.


Try with the following code.


email_address = "test@dummy.com"


template = self.env.ref("reminder_activity_cron.email_template_remindermail_aktivity")


template = template.with_context(

    email_to=email_address,

    displayname=displayname,

    resName=resName,

    link=link

).send_mail(self.id, force_send=True)


Then in your XML template, update email_to field like:


<field name="email_to">{{ email_to }}</field>


Use context keys that match your Jinja variable names. So if you write {{ email_to }} in the template, then pass email_to in context. Always ensure that force_send=True is used if you want the email sent immediately.


Hope it helps

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 5 25
549
0
thg 8 25
43
1
thg 3 15
5971
1
thg 8 25
305
1
thg 8 25
336