
Send Mail Using Controller and With Template In Odoo 18
Overview
Odoo provides a robust and flexible email system that allows users to send notifications, alerts, and reports directly from the system. The email functionality in, ensuring seamless communication with customers, suppliers, and internal teams.Using the API method, developers can automate and customize email communication based on business requirements like Triggers emails based on specific actions or events in the system,Allows dynamic email content, templates, and attachments,Ensures that emails are sent securely, with proper authentication,Handles bulk email sending without manual intervention,Can be connected with third-party platforms via API for extended functionalities.
Working
Folder structure
Add new Controller file
Create a mail template
Create a wizard
your_module/data/controller_mail.xml
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<data >
<record id="complain_registration" model="mail.template">
<field name="name">Mail From Customer</field>
<field name="model_id" ref="your_module.model_temp_mail_data"/>
<field name="subject">{{object.subject}}</field>
<field name="email_from">noreplay@gmail.com</field>
<field name="email_to">customer.service@gmail.com</field>
<field name="body_html" type="html">
<div style="margin: 0px; padding: 0px;">
<p style="margin: 0px; padding: 0px; font-size: 13px;">
Hi,<br/>
You have a mail from <strong><t t-out="object.name" /></strong>,<br/>
<strong><t t-out="object.content" /></strong>
<br/><br/><br/>
<t t-out="object.name" /><br/>
<t t-out="object.location" /><br/>
<t t-out="object.phone" /><br/>
<t t-out="object.email" />
<br/><br/><br/><br/>
Best Regards,<br/>
Your System
</p>
</div>
</field>
</record>
</data>
</odoo>
This template will call when we send the mail
from odoo import api,models, fields
class TempMail(models.TransientModel):
_name = 'temp.mail.data'
_description = 'Temporary Details'
name = fields.Char(string="Name")
phone = fields.Char(string="Phone")
location = fields.Char(string="Location")
email = fields.Char(string="Email")
content = fields.Char(string="Content")
subject = fields.Char(string="Subject")
This is the wizardTemporarily store the data received from json parameter for mail sending.
your_module/controllers/main.py
from odoo import http
from odoo.http import request
import logging
_logger = logging.getLogger(__name__)
class webController(http.Controller):
@http.route(['/send_mail/'], type='json', auth='public', methods=['POST'], csrf=False, cors="*")
def action_send_mail(self, **kwargs):
"""API to send an email using a mail template"""
customer_name = kwargs.get('customer_name')
customer_email = kwargs.get('customer_email')
content = kwargs.get('content')
location = kwargs.get('location')
phone = kwargs.get('phone')
subject = kwargs.get('subject')
mail_values = {'name': customer_name,'phone': phone,
'location': location,'email':customer_email,
'content':content,'subject':subject,
}
mail = request.env['temp.mail.data'].sudo().create(mail_values)
template_id = request.env.ref('your_module.complain_registration').sudo()
if template_id:
template_id.send_mail(mail.id,force_send=True)
return {'success': 'Email sent successfully'}
This is the api function which is responsible for sending mail using the inputs.also if mail send it returns a success message.
Json Parameter (Postman)
By sending this params to the url with post method , return a success message if the mail successfully send
Output mail
Conclusion
Sending emails using a controller in Odoo provides a flexible, scalable, and automated approach to managing communication within the system. This method ensures seamless integration, allowing external applications, websites, or third-party services to trigger emails from Odoo without manual intervention. It is highly compatible across platforms, enabling smooth communication between Odoo and external systems like web portals, mobile apps, and CRMs. Additionally, it offers enhanced customization, allowing developers to define email templates, dynamically populate fields, and personalize messages based on business needs. With automation and scalability, businesses can send bulk notifications, automated responses, and scheduled messages, improving efficiency while reducing manual effort. This functionality supports both templated and non-templated emails, works with various SMTP providers, and allows for advanced features such as attachments, multiple recipients, CC/BCC options, and real-time tracking through Odoo’s email logs. By leveraging this approach, businesses can enhance communication, streamline operations, and integrate Odoo effortlessly with other platforms, making it a valuable feature for automated, scalable, and efficient email management.
If you are looking for an ERP implementation partner with diverse industry experience feel free to contact us. Zesty Beanz Technologies is headquartered in Trivandrum Kerala, We have proven track record of successful implementations across the world in various sectors including Odoo for Manufacturing, Odoo for Trading, Odoo for FMCG, Odoo for Oil & Gas, Odoo for Diary, Odoo for Pharma, Odoo for Cosmetic Clinic, Odoo for Contracting Companies, Odoo for HVAC, Odoo for Logistics, Odoo for Automobile, Odoo for Laundry, Odoo for Field Service, Odoo for E-Commerce & many more
ZestyBeanz offers Developer / Consultant outsourcing programs, Chat with us in Whatsapp and Hire Odoo Developers, Mobile Application Developers, Consultants.
#OdooKerala #OdooKochi #OdooTrivandrum #OdooERP #ProjectManagement #OdooVansales #HireOdooDeveloper