Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How can I send email from code?

By
Dr Obx
on 4/15/16, 6:46 AM 747 views

In old API everything works, if I'm trying to adapt code to the new API .... failed, can anyone give me an example of working new API sendmail code please ?

After conversion, I've met some problems but finally solved, however, still no positive result,  not sending messages.

@api.model

def mailmessage(self):

    vals = {}

    domain=[('name','ilike','Processing_order')]

    tplate = self.env['email.template'].search(domain, limit = 1)

    for temp in tplate:

        template = temp.id

        if template:

            print 'Sending e-mail using template "'+temp.name+'" ..... ' # message is displayed

            self.env['email.template'].send_mail(temp.id)

            print '..... mail sent.' # message is displayed

            return True

        else:

            return

but inbox .... still empty :(

No sending emails :(

1

Pawan

--Pawan--
1267
| 4 3 5
Hyderabad, India
--Pawan--


Pawan
On 4/15/16, 7:49 AM

@api.multi

def mailmessage(self):

vals = {}

domain=[('name','like','Order_processing')]

template = self.env['email.template'].search(domain, limit=1) # in new api you will get list of objects instead of list of integers from search()

if not template:

return

template = template[0]

if template.email_to:

template.send_mail()

 return True

Finally, I got no errors but also got no email in my inbox :)

Dr Obx
on 4/15/16, 9:21 AM
	@api.model
	def mailmessage(self):
		vals = {}
		domain=[('name','ilike','Processing_order')]
		tplate = self.env['email.template'].search(domain, limit = 1)
		for temp in tplate:
			print 'Template', temp.name
			template = temp.id
		if template:
			print 'Sending e-mail using template "',temp.name,'" ..... '
			self.env['email.template'].send_mail(template, True)
			print '.... mail sent.'
			return True
		else:
			return
Dr Obx
on 4/15/16, 9:22 AM
0
Dr Obx
On 4/16/16, 11:23 AM

Ha!

Problem solved, thank you Pawan.

Unfortunately I can't give you more 'Up Vote'

THANK YOU

0
yopi
On 4/16/16, 7:03 AM

Hi Dr Obx,

Did you solved it?

Try this:


@api.multi

def mailmessage(self):

    template_id = self .env.ref('module_name.xml_id') # xml id of your email template

    # force_send=True: tells odoo to send the email directly

    # force_send=False: odoo will put it in queue (default)

    template_id.send_mail(self.ids, force_send=True)



Yes yopi, problem solved. Thank you anyway :)

Dr Obx
on 4/16/16, 11:23 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 4/15/16, 6:46 AM
Seen: 747 times
Last updated: 4/16/16, 11:23 AM