How to Send Email From Code in Odoo 18

Hello Odoo Developers & Learners! 

Email is one of the most important tools for communication in any business. Whether it's sending updates to customers, sharing documents with colleagues, or notifying suppliers, email plays a key role in daily operations. In Odoo 18, sending emails through code is simple once you know the steps. The system includes email features that work across different modules, allowing businesses to customize and automate email communication with clients, employees, and partners. This makes it easy to stay connected and maintain clear records of all important conversations within the platform.

In this guide, we’ll take you step-by-step through:

  • How to create an email template
  • How to send the email when a button is clicked
  • What you need to set up to make sure your email is actually delivered

Let’s get started!

Why Use Emails in Odoo?

Emails help you:

  • Notify customers automatically
  • Send internal updates to users
  • Track communication in a written form
  • Trigger actions (like confirmations, receipts, alerts)

By using templates and automation, we can send emails with just one click or even trigger them automatically when an action happens.

Step 1: Create an Email Template

First, create an XML file to define your email template. This template tells Odoo how your email will look and who it's for.

 Example: Email Template XML

XML

<?xml version="1.0" encoding="utf-8"?>

<odoo>

  <data>

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

     <field name="name">Ticket Details</field>

     <field name="model_id" ref="dev_helpdesk.model_dev_helpdesk_ticket"/>

     <field name="email_from">{{(object.user_id.email_formatted or user.email_formatted)}}</field>

     <field name="partner_to">{{object.partner_id.id}}</field>

     <field name="subject">Details of {{object.name}}</field>

     <field name="body_html"><![CDATA[

         <div style="background-color:#F3F5F6;color:#515166;font-family:Arial,Helvetica,sans-serif;font-size:14px;padding-top:5px;padding-left:30px;padding-bottom:5px;padding-right:30px;">

             Dear <span t-esc="object.sudo().partner_id.name or 'Madam/Sir'"/>,

             <br /><br />

             Your ticket <b><span t-esc="object.ticket_sequnce"/></b> request has been received successfully.

             <br/>

             Your issue is <b><span t-esc="object.name"/></b>

             <br/>

             <br/>

             Our team will resolve your issue as soon as possible

             <br/>

             <br/>

                Do not hesitate to contact us if you have any questions.

          </div>

     ]]></field>

</record>

  </data>

</odoo>

Make sure to change your_module and model_dev_helpdesk_ticket to match your actual model name.

email_from

  • What it does: Sets the sender's email address for the email.
  • Explanation: It first tries to use the ticket's assigned user's email (object.user_id). If not available, it falls back to the current user's email (user.email_formatted).

partner_to

  • What it does: Sets the recipient's partner ID (linked contact) for the email.
  • Explanation: It sends the email to the partner (customer) related to the ticket.

subject

  • What it does: Defines the subject line of the email.
  • Explanation: It shows something like “Details of [Ticket Title]” – using the name (title) of the ticket.

 Step 2: Add a Button to Your Form View

Let’s add a button called "Send Email" to your model's form view.

 XML Code for Button

XML

<odoo>

  <data>

    <record id="dev_helpdesk_ticket_form_view" model="ir.ui.view">

      <field name="name">dev.helpdesk.ticket.form</field>

      <field name="model">dev.helpdesk.ticket</field>

      <field name="arch" type="xml">

        <form string="Helpdesk Ticket">

          <header>

            <button name="action_send_email"

                    string="Send By Mail"

                    type="object"

                    class="btn-primary"/>

          </header>

          <sheet>

<!--ADD YOUR FIEDLS-->        

          </sheet>

          <chatter/>

        </form>

      </field>

    </record>

  </data>

</odoo>


On the form view screen, you can see the "Send by Mail" button clearly highlighted. When you click this button, an email containing the ticket details will be sent to the customer.

How to Send Email From Code in Odoo 18

When you click this button, the ticket details are sent by email and as you can see in the chatter, the mail was successfully sent.

Step 3: Add Python Code to Send the Email

Now let's define the action_send_email method in your model Python file:

Python Code

.py

from odoo import models

class HelpdeskTicket(models.Model):

    _name = 'dev.helpdesk.ticket'

    _inherit = ['mail.thread', 'mail.activity.mixin']  # Enables chatter and tracking

    def action_send_email(self):

        template_id = self.env.ref('dev_helpdesk.email_template_helpdesk_ticket_send)

        for record in self:

            if template:

                email_values = {

                    'email_from': self.env.user.email,  # Sender email

                }

                template.send_mail(record.id, force_send=True, email_values=email_values)

Make sure to replace your_module.email_template_helpdesk_ticket_send with your real XML ID.

Don't Forget: Set Up the Outgoing Email Server!

Before any email can actually be sent, Odoo needs to know how to send it.

To configure the Outgoing Mail Server:

Go to Settings → Technical → Email → Outgoing Mail Servers


Fill in the SMTP Server, SMTP Port, your email, and password as shown. Then, click the "Test Connection" button (highlighted in red) to verify the email server setup.

Great! As shown in your screenshot, the "Connection Test Successful!" message confirms that your outgoing mail server setup in Odoo is working correctly.

SMTP configuration is now complete, and you can send emails from Odoo using this Gmail account.

Let me know if you’d like help sending a test email or configuring incoming mail as well.

The ticket confirmation email has been successfully sent with clear ticket details and customer personalization. Minor formatting improvements, like line breaks or a clearer subject line, could enhance readability.


 Tips for Beginners

  • Always test on a test record first.
  • Use force_send=True to send immediately.
  • You can preview your email from the Email Templates menu under Technical.

Conclusion:

In this blog, you've learned step-by-step how to send emails using code in Odoo 18, designed for beginners and those new to Odoo development. We walked through creating a reusable email template, adding a custom button in the form view, and writing a simple Python method to send the email when the button is clicked.

We also explained how to configure the outgoing mail server so your Odoo database can actually send the emails. For more hands-on learning and skill-building, explore Odoo training programs offered by Devintellecs.


Odoo DEV May 28, 2025
Share this post
Archive
Sign in to leave a comment
How to Use ValidationError and UserError in Odoo