跳至内容
Odoo 菜单
  • 登录
  • 免费试用
  • 应用程序
    财务
    • 会计
    • 发票
    • 费用
    • 电子表格 (BI)
    • 文档
    • 电子签名
    销售
    • 客户关系管理
    • 销售
    • POS 销售点管理-零售
    • POS 销售点管理 - 餐厅
    • 订阅
    • 租赁
    网站
    • 网站设计
    • 电子商务
    • 博客
    • 论坛
    • 在线客服
    • 在线学习
    供应链
    • 库存
    • 制造
    • 产品生命周期
    • 采购
    • 维护保养
    • 品控
    人力资源
    • 员工
    • 招聘
    • 休假
    • 评价
    • 内部推荐
    • 车队
    营销
    • 社媒营销
    • 电邮营销
    • 短信营销
    • 近期活动
    • 营销自动化
    • 网上调查
    服务
    • 项目管理
    • 工时单
    • 现场服务
    • 服务台
    • 排期
    • 预约
    生产力
    • 讨论
    • 批核
    • IoT物联网
    • VoIP
    • 知识库
    • WhatsApp
    第三方应用软件 Odoo 定制 Odoo云端平台
  • 行业
    零售
    • 书店
    • 服装店
    • 家具店
    • 食品杂货店
    • 五金店
    • 玩具店
    餐饮与酒店服务
    • 酒吧及酒馆
    • 餐厅
    • 快餐
    • 民宿
    • 饮品分销商
    • 酒店
    房地产
    • 房地产代理
    • 建筑师事务所
    • 建造业
    • 地产管理
    • 园艺
    • 业主协会
    咨询
    • 会计师事务所
    • Odoo合作伙伴
    • 市场推广公司
    • 律师事务所
    • 人才招聘
    • 审核 & 认证
    制造
    • 纺织
    • 金属
    • 家具
    • 食品
    • 啤酒厂
    • 企业礼品
    保健与健身
    • 体育俱乐部
    • 眼镜店
    • 健身中心
    • 健康从业者
    • 药房
    • 发型屋
    Trades
    • 维修人员
    • IT 硬件及支持
    • 太阳能系统
    • 鞋匠
    • 清洁服务
    • 暖通空调服务
    其他
    • 非营利组织
    • 环境机构
    • 广告牌租赁
    • 摄影服务
    • 自行车租赁
    • 软件经销商
    浏览所有行业
  • 社区
    学习
    • 教学视频
    • 文档
    • 认证
    • 培训
    • 博客
    • 播客
    赋能教育
    • 教育计划
    • Scale Up! 商业游戏
    • 参观Odoo
    获取软件
    • 下载
    • 版本对比
    • 发布
    合作
    • Github
    • 论坛
    • 近期活动
    • 翻译
    • 成为合作伙伴
    • 合作伙伴服务
    • 注册您的会计事务所
    获取服务
    • 寻找合作伙伴
    • 查找会计服务
    • 预约顾问咨询
    • 安装及推行服务
    • 客户参考
    • 支持
    • 升级
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    获取演示
  • 定价
  • 技术支持

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • 客户关系管理
  • e-Commerce
  • 会计
  • 库存
  • PoS
  • 项目
  • MRP
All apps
只限注册用戶才可与社群互动。
所有帖文 人 徽章
标签 (查看所有)
odoo accounting v14 pos v15
关于此论坛区
只限注册用戶才可与社群互动。
所有帖文 人 徽章
标签 (查看所有)
odoo accounting v14 pos v15
关于此论坛区
帮助

Binary field as attachment in mail

订阅

此帖文有活动时,接收通知

此问题已终结
mailinvoiceattachmentbinary
5 回复
17878 查看
形象
Hugo M.

Hello,

I'm developing a module to extend Invoice (Odoo 11). I have added a binary field into the Invoice model, and I want to be able to send this binary via mail.

Sending an e-mail directly and attaching a binary is simple, but I want to open the composer wizard with the binary as an attachment. Currently, the "Send by e-mail" button opens the composer with Odoo's default PDF report; I want to add another file in that view.

This is my current code to try to attach the files. This piece of code itself runs and doesn't generate any errors, but the composer wizard just ignores the added info.

@api.multi
def action_invoice_sent(self):
self.ensure_one()

result = super(AccountInvoice,self).action_invoice_sent()

pdf_attachment_id = self.env['ir.attachment'].create({
'name': ("%s" %self.pdf),
'datas': self.pdfname,
'datas_fname': self.pdf,
'res_model': 'mail.compose.message',
'res_id': 0,
'type': 'binary'
})

result['context'].update({
'attachment_ids': [(6,0,[pdf_attachment_id.id])]
})

return result

Thanks a lot!
1
形象
丢弃
形象
Hugo M.
编写者 最佳答案

Thanks but that's not my question. I don't want to attach a PDF report, the default invoice's e-mail template already does that. I want to add *another* file in the e-mail

I already solved it by extending the Composer wizard to retrieve the added files in context, but thanks anyway.

0
形象
丢弃
形象
Megha Patel
最佳答案

Hugo M.

Firstly you have to check that the attachment you are sending is in pdf format or not, if its not then convert it using get_pdf method.

here is the example of attached report,

@api.multi
def action_rfq_send(self):
    self.ensure_one()
    ir_model_data = self.env['ir.model.data']
    try:
        if self.env.context.get('send_rfq', False):
            temp_id = self.env.ref('purchase.email_template_edi_purchase')
        else:
            temp_id = self.env.ref(
                               'purchase.email_template_edi_purchase_done')
    except ValueError:
        temp_id = False
    try:
        compose_form_id = ir_model_data.get_object_reference('mail',
                         'email_compose_message_wizard_form')[1]
    except ValueError:
        compose_form_id = False

    attach_obj = self.env['ir.attachment']

    pdf_rfq_quote = self.env['report'].sudo().get_pdf([self.id],
                                             'report name')
    result_rfq_quote = base64.b64encode(pdf_rfq_quote)

    attachment_ids = []
    if result:
        attach_data = {
            'name': 'name.pdf',
            'datas': result_rfq_quote,
            'datas_fname': 'name.pdf',
            'res_model': 'ir.ui.view',
            }
        attach_id = attach_obj.create(attach_data)
        attachment_ids.append(attach_id.id)
    if attachment_ids:
        temp_id.write({'attachment_ids': [(6, 0, attachment_ids)]})

    ctx = dict(self.env.context or {})
        ctx.update({
            'default_model': 'purchase.order',
            'default_res_id': self.ids[0],
            'default_use_template': bool(temp_id.id),
            'default_template_id': temp_id.id,
            'default_composition_mode': 'comment',
        })
        return {
            'name': _('Compose Email'),
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'mail.compose.message',
            'views': [(compose_form_id, 'form')],
            'view_id': compose_form_id,
            'target': 'new',
            'context': ctx,
        }
       

Thank You

1
形象
丢弃
形象
Mohamed Amine Kaabi
最佳答案

it works with pdf file ?


0
形象
丢弃
形象
Gabriel Hernandez
最佳答案
Thanks but that's not my question. I don't want to attach a PDF report, the default invoice's e-mail template already does that. I want to add *another* file in the e-mail
I already solved it by extending the Composer wizard to retrieve the added files in context, but thanks anyway.


I want to do the same, how to solve this?

Thank you.
0
形象
丢弃
形象
Carlos Alberto García Brizuela
最佳答案

Hello, this is my implementation using odoo email wizard:

def send_mail(self):
self.ensure_one()
template = self.env.ref('my_module.email_template_payroll', False)
compose_form = self.env.ref('mail.email_compose_message_wizard_form', False)

# attach xml from binary field
if self.xml_data:
xml_name = '%s.xml' % self.name
attachment_ids = []
encoded_data = base64.b64encode(self.xml_data)
decoded_data = base64.b64decode(encoded_data)

if decoded_data:
attach_data = {
'name': xml_name,
'res_name': xml_name,
'datas': decoded_data,
'res_model': 'hr.payslip',
'res_id': self.id,
}
attach_id = self.env['ir.attachment'].create(attach_data)
attachment_ids.append(attach_id.id)

if attachment_ids:
template.write({'attachment_ids': [(6, 0, attachment_ids)]})

ctx = dict()
ctx.update({
'default_model': 'hr.payslip',
'default_res_id': self.id,
'default_use_template': bool(template),
'default_template_id': template.id,
'default_composition_mode': 'comment',
})


return {
'name': _('Compose Email'),
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'mail.compose.message',
'views': [(compose_form.id, 'form')],
'view_id': compose_form.id,
'target': 'new',
'context': ctx,
}

Be careful with the indentation of the code, it got lost when paste text.

Hope it helps


0
形象
丢弃
Mohamed Amine Kaabi

xml_data a existing field ?

Carlos Alberto García Brizuela

"xml_data" field is a binary stored field in the same model of your method "send_mail". not remember if its a native odoo field or a custom one.

Mohamed Amine Kaabi

it works with pdf file ?

Carlos Alberto García Brizuela

I think should work too, pdf files are stored by odoo in binary fields as well.

Mohamed Amine Kaabi

i have this error

File "/home/ibs/Bureau/workspace/odoo13/odoo/api.py", line 390, in call_kw
result = _call_kw_multi(method, model, args, kwargs)
File "/home/ibs/Bureau/workspace/odoo13/odoo/api.py", line 377, in _call_kw_multi
result = method(recs, *args, **kwargs)
File "/home/ibs/Bureau/workspace/odoo13/test_erp/spc_intranet_sfr/models/hs_commande.py", line 278, in action_commande_send
encoded_data = base64.b64encode(self.attachment_access_adsl)
File "/usr/lib/python3.8/base64.py", line 58, in b64encode
encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'ir.attachment'

Mohamed Amine Kaabi

This is the function
def action_commande_send(self):
self.ensure_one()
template = self.env.ref('spc_intranet_sfr.email_template_hs_commande', False)
compose_form = self.env.ref('mail.email_compose_message_wizard_form', False)

if self.attachment_access_adsl:
pdf_name = '%s.pdf' % self.name
attachment_ids = []
encoded_data = base64.b64encode(self.attachment_access_adsl)
decoded_data = base64.b64decode(encoded_data)

if decoded_data:
attach_data = {
'name': pdf_name,
'res_name': pdf_name,
'datas': decoded_data,
'res_model': 'hs.commande',
'res_id': self.id,
}
attach_id = self.env['ir.attachment'].create(attach_data)
attachment_ids.append(attach_id.id)

if attachment_ids:
template.write({'attachment_ids': [(6, 0, attachment_ids)]})

ctx = dict()
ctx.update({
'default_model': 'hs.commande',
'default_res_id': self.id,
'default_use_template': bool(template),
'default_template_id': template.id,
'default_composition_mode': 'comment',
})
return {
'name': _('Compose Email'),
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'mail.compose.message',
'views': [(compose_form.id, 'form')],
'view_id': compose_form.id,
'target': 'new',
'context': ctx,
}

Carlos Alberto García Brizuela

The error is in this line:
encoded_data = base64.b64encode(self.attachment_access_adsl)

I think you are passing the full model or class object, but you need to pass the binary field instead, for example:

encoded_data = base64.b64encode(self.attachment_access_adsl.bin_field_name)

Mohamed Amine Kaabi

bin_field_name ?!!!!!!

Carlos Alberto García Brizuela

base64.b64encode(*)

This method expects binary data as a parameter, you are passing an instance object of the class "ir.attachment".

what you need to pass is the binary field of the class "ir.attachment". In odoo 15 the binary field name is "datas"

喜欢讨论吗?不要只阅读,加入进来!

立即创建账户,享受专属功能,与我们的精彩社区互动!

注册
相关帖文 回复 查看 活动
Send a mail with fields.binary attached
mail attachment send binary
形象
形象
4
6月 17
11116
send email with multiple attachment
mail attachment
形象
1
8月 24
2695
Send a mail with automatic attachment ofa binary field from a model
wizard mail attachment template binary
形象
1
3月 15
7716
Attachments in openerp 7.0
invoice attachment
形象
形象
形象
3
3月 15
9781
Send invoice by email: attachments creation
invoice attachment
形象
形象
1
3月 15
7109
社区
  • 教学视频
  • 文档
  • 论坛
开源
  • 下载
  • Github
  • Runbot
  • 翻译
服务
  • Odoo.sh 托管
  • 支持
  • 升级
  • 自定义开发服务
  • 教育
  • 查找会计服务
  • 寻找合作伙伴
  • 成为合作伙伴
关于我们
  • 我们的公司
  • 品牌资产
  • 联系我们
  • 招聘
  • 近期活动
  • 播客
  • 博客
  • 客户
  • 法律 • 隐私
  • 安全
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo致力于为企业管理提供高效智能的开源解决方案,是全球业内高速成长的软件服务商之一,逾七百五十万用户选择Odoo进行数字化升级。通过一系列全业务链覆盖、高度集成、简单易用的商业应用,助力企业实现信息化改革、降本增效并释放公司增长潜力。

Odoo独特的价值在于是一款非常容易使用又完全集成的应用。

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now