Skip to Content
Menu
This question has been flagged
2 Replies
4243 Views

hello guys, i want to send an email by wizard mail.compose.message, but i want it sent by email that not in res.partner how can i achieve that, since the default odoo is using email from partner_ids

so i have condition 
1. i have customer_id field -> this will be default email that will be send by the wizard

2. i have recipient_email field -> if this field has a value (email) it will send to this email

thank you for your help :)

Avatar
Discard
Best Answer

Hi,

Python – Your Custom Wizard

from odoo import models, fields, api class CustomMailWizard (models.TransientModel): _inherit = 'mail.compose.message' customer_id = fields.Many2one( 'res.partner' , string= 'Customer' ) recipient_email = fields.Char(string= 'Recipient Email' ) @api.multi def get_mail_values ( self, res_ids ): mail_values = super (CustomMailWizard, self).get_mail_values(res_ids) for res_id in res_ids: mail_value = mail_values[res_id] # Override recipients if self.recipient_email: mail_value.update({ 'email_to' : self.recipient_email, # send to this email directly 'partner_ids' : [( 5 , 0 , 0 )], # clear partner_ids }) elif self.customer_id: mail_value.update({ 'partner_ids' : [( 4 , self.customer_id. id )], # fallback to partner }) mail_values[res_id] = mail_value return mail_values

How to Open the Wizard with Context

In your button action or code that launches the wizard, pass the fields through context:

return { 'type' : 'ir.actions.act_window' , 'view_type' : 'form' , 'view_mode' : 'form' , 'res_model' : 'mail.compose.message' , 'target' : 'new' , 'context' : { 'default_model' : 'your.model' , 'default_res_id' : self id , 'default_customer_id' : self.customer_id. id , 'default_recipient_email' : self.recipient_email, 'default_composition_mode' : 'comment' , # Optional subject/body } }

i hope it is use full

Avatar
Discard
Best Answer


if self.recipient_email: # If recipient_email is set, use it directly ctx['default_email_to'] = self.recipient_email # Clear partner_ids to avoid sending to both the partner and the custom email ctx['default_partner_ids'] = [] elif self.customer_id: # Otherwise, use the customer's partner record ctx['default_partner_ids'] = self.customer_id.ids


U can use like this 
just pass this context 
in this

return {

'name': _('Send Email to Employees'),

'type': 'ir.actions.act_window',

'res_model': 'mail.compose.message',

'view_mode': 'form',

'view_id': False,

'target': 'new',

'context': ctx,

}

And dont forget to add other context as well


Avatar
Discard
Related Posts Replies Views Activity
0
Feb 25
1166
1
Nov 24
1998
0
Oct 23
40
0
Apr 23
91
3
Dec 22
3600