Skip to Content
Menu
This question has been flagged
3 Replies
497 Views

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>

Avatar
Discard
Author Best Answer

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>

Avatar
Discard
Best Answer

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



Avatar
Discard
Best Answer

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

Avatar
Discard
Related Posts Replies Views Activity
0
May 25
513
1
Mar 15
5863
1
Aug 25
173
4
Jul 25
1536
1
Jul 25
847