This question has been flagged
6 Replies
4782 Views

Hi,

I'm making a simple modul that show me a html form with a textfield. Now I wanted to know how I can send an email to a adress after pushing the submit button in the form?

Is there any example code for moduls?

Avatar
Discard
Author Best Answer

this is the python code:

# -*- coding: utf-8 -*-

from openerp import models, fields, api


def send_mail_example(self,cr,uid,ids,odoo_record,context=None):
ir_mail_server = self.pool.get('ir.mail_server')
title = "Your e-mail title"
message = "This is the concent from your e-mail. An example from a field inside the e-mail: " + odoo_record.odoo_dns_url + "\n\nWith kind regards,\nYenthe"
#Structure mail function: def build_email(self, email_from, email_to, subject, body, email_cc=None, email_bcc=None, reply_to=False,
msg = ir_mail_server.build_email("test23.test@gmx.ch", ["test23.test@gmx.ch"], title, message,["test23.test@gmx.chh"],[],"test23.test@gmx.ch")
ir_mail_server.send_email(cr, uid, msg)

thanks for your help 

What is wrong. When I click the button nothing happen. 

Avatar
Discard
Best Answer

Hi diezcode,

There are quite some options and methods for this but I'll give you an example.

def send_mail_example(self,cr,uid,ids,odoo_record,context=None):
ir_mail_server = self.pool.get('ir.mail_server')
title = "Your e-mail title"
message = "This is the concent from your e-mail. An example from a field inside the e-mail: " + odoo_record.odoo_dns_url + "\n\nWith kind regards,\nYenthe"
#Structure mail function: def build_email(self, email_from, email_to, subject, body, email_cc=None, email_bcc=None, reply_to=False,
msg = ir_mail_server.build_email("emailfrom@gmail.com", ["emailto@gmail.com"], title, message,["example@gmail.com"],[],"example@gmail.com")
ir_mail_server.send_email(cr, uid, msg)

With this example you could do about everything you need. Other options to use can be found under addons/mail/views/mail_mail.py in the official Odoo code, which contains a lot of e-mail functions.
You can trigger this Python function just like any other method with a button. I have an example module which explains how buttons work here: https://github.com/Yenthe666/Odoo_Samples/tree/master/button_action_demo

Update: and an example with a button inside the view (not in the topbar):

<button name="send_mail_example" type="object" string="Send E-mail"/>

Yenthe

Avatar
Discard
Author

How can I connect with a submit button from a normal form in a xml.-file?

Added a button sample that is inside the form and not in the top bar.

Author

nothing happend, I have a send_mail_example.py with this def send_mail_example code inside, what I need to do that this works?

Hard to tell from this side what is missing or what is not. Place a logging statement inside your Python function and see if that goes off. If it doesn't it means you never reach the Python function. Make sure the name of the button and the name of the Python function (the function, not the file) are exactly the same. If that all doesn't fix it then add your code in your question please. :)