Skip ke Konten
Odoo Menu
  • Login
  • Uji coba gratis
  • Aplikasi
    Keuangan
    • Akuntansi
    • Faktur
    • Pengeluaran
    • Spreadsheet (BI)
    • Dokumen
    • Tanda Tangan
    Sales
    • CRM
    • Sales
    • POS Toko
    • POS Restoran
    • Langganan
    • Rental
    Website
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Rantai Pasokan
    • Inventaris
    • Manufaktur
    • PLM
    • Purchase
    • Maintenance
    • Kualitas
    Sumber Daya Manusia
    • Karyawan
    • Rekrutmen
    • Cuti
    • Appraisal
    • Referensi
    • Armada
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Acara
    • Otomatisasi Marketing
    • Survei
    Layanan
    • Project
    • Timesheet
    • Layanan Lapangan
    • Meja Bantuan
    • Planning
    • Appointment
    Produktivitas
    • Diskusi
    • Approval
    • IoT
    • VoIP
    • Pengetahuan
    • WhatsApp
    Aplikasi pihak ketiga Odoo Studio Platform Odoo Cloud
  • Industri-Industri
    Retail
    • Toko Buku
    • Toko Baju
    • Toko Furnitur
    • Toko Kelontong
    • Toko Hardware
    • Toko Mainan
    Makanan & Hospitality
    • Bar dan Pub
    • Restoran
    • Fast Food
    • Rumah Tamu
    • Distributor Minuman
    • Hotel
    Real Estate
    • Agensi Real Estate
    • Firma Arsitektur
    • Konstruksi
    • Estate Management
    • Perkebunan
    • Asosiasi Pemilik Properti
    Konsultansi
    • Firma Akuntansi
    • Mitra Odoo
    • Agensi Marketing
    • Firma huku
    • Talent Acquisition
    • Audit & Sertifikasi
    Manufaktur
    • Tekstil
    • Logam
    • Perabotan
    • Makanan
    • Brewery
    • Corporate Gift
    Kesehatan & Fitness
    • Sports Club
    • Toko Kacamata
    • Fitness Center
    • Wellness Practitioners
    • Farmasi
    • Salon Rambut
    Perdagangan
    • Handyman
    • IT Hardware & Support
    • Sistem-Sistem Energi Surya
    • Pembuat Sepatu
    • Cleaning Service
    • Layanan HVAC
    Lainnya
    • Organisasi Nirlaba
    • Agen Lingkungan
    • Rental Billboard
    • Fotografi
    • Penyewaan Sepeda
    • Reseller Software
    Browse semua Industri
  • Komunitas
    Belajar
    • Tutorial-tutorial
    • Dokumentasi
    • Sertifikasi
    • Pelatihan
    • Blog
    • Podcast
    Empower Education
    • Program Edukasi
    • Game Bisnis 'Scale Up!'
    • Kunjungi Odoo
    Dapatkan Softwarenya
    • Download
    • Bandingkan Edisi
    • Daftar Rilis
    Kolaborasi
    • Github
    • Forum
    • Acara
    • Terjemahan
    • Menjadi Partner
    • Layanan untuk Partner
    • Daftarkan perusahaan Akuntansi Anda.
    Dapatkan Layanan
    • Temukan Mitra
    • Temukan Akuntan
    • Temui penasihat
    • Layanan Implementasi
    • Referensi Pelanggan
    • Bantuan
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Dapatkan demo
  • Harga
  • Bantuan

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

  • CRM
  • e-Commerce
  • Akuntansi
  • Inventaris
  • PoS
  • Project
  • MRP
All apps
Anda harus terdaftar untuk dapat berinteraksi di komunitas.
Semua Post Orang Lencana-Lencana
Label (Lihat semua)
odoo accounting v14 pos v15
Mengenai forum ini
Anda harus terdaftar untuk dapat berinteraksi di komunitas.
Semua Post Orang Lencana-Lencana
Label (Lihat semua)
odoo accounting v14 pos v15
Mengenai forum ini
Help

Odoo 13 Attach a file in email template

Langganan

Dapatkan notifikasi saat terdapat aktivitas pada post ini

Pertanyaan ini telah diberikan tanda
attachmentsending mail with attachmentOdoo13.0
1 Balas
13226 Tampilan
Avatar
Kev

Hello, Please help.My objective is to send an email with attachment by clicking a button. First, I generate a text file, save it in the directory that I have defined(For test only, not advisable but i'll change it later) then open and read it as binary and convert it to base64, here is the code. Please correct me if I did something wrong:

def send_email_sample(self):
"""

:return: test function
"""
self.ensure_one()
# path = '/opt/odoo/odoo/doc/'
path = "C:\\Users\myuser\\Desktop\\Odoo projects\\Odoo13-CE\\test_folder\\"
order_line_data = []
for num, rec in enumerate(self.order_line):
order_line_data.append({
'default_code': rec.product_id.default_code if rec.product_id.default_code else "None",
'price_unit': rec.price_unit,
'product_uom_qty': int(rec.product_uom_qty),
'product_uom': rec.product_uom.name,
'sequence_number': num + 1
})
json_record = {
'so_number': self.name,
'partner_id': self.partner_id.name,
'date_oder': self.date_order.strftime("%m/%d/%Y"),
'payment_term': self.payment_term_id.name if self.payment_term_id else "None",
'partner_add': "%s %s City %s" % (
self.partner_id.street, self.partner_id.city, self.partner_id.state_id.name),
'order_line': order_line_data,
}
with open(path + "SALESORDER_%s.txt" % self.name, "w") as file:
file.write("%s" % json_record)
data_value = open(path + "SALESORDER_%s.txt" % self.name, 'rb').read()
data_record = base64.encodebytes(data_value)
ir_values = {
'name': "SALESORDER_%s.txt" % self.name,
'type': 'binary',
'datas': data_record,
'store_fname': data_record,
'create_uid': self.env.uid,
'res_model':
self._name,
'res_id': self.id,
'mimetype': 'text/csv',
}
data_id = self.env['ir.attachment'].create(ir_values)

Now my problem is, how to get that attachment in ir.attachment and link it to my template. Here is my template code:

<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<data noupdate="1">
<record id="email_template_test_send" model="mail.template">
<field name="name">Test email with attachment</field>
<field name="model_id" ref="sale.model_sale_order"/>
<field name="subject">TEST SEND EMAIL ATTACHMENT (${object.name})</field>
<field name="email_from">${user.email_formatted | safe}</field>
<field name="email_to">${user.email_formatted | safe}</field>
<field name="body_html" type="html">
<div style="margin: 0px; padding: 0px;"><![CDATA[ -----Content---]]>
<p style="margin: 0px; padding: 0px; font-size: 13px;">
Testing send email file with attachment
</p>
</div>
</field>
</record>
</data>
</odoo>

I tried this code:

email_template = self.env.ref('custom_module.email_template_test_send')
email_template.attachment_ids = [(4, data_id.id)]
email_template.send_mail(data_id.id, raise_exception=False, force_send=True)
email_template.attachment_ids = [(3, data_id.id)]

but It gave me an error: odoo.exceptions.MissingError: ('Record does not exist or has been deleted.\n(Record: sale.order(705,), User: 2)', None)

The attachment is successfully registered in Technical>Database structure>attachments but I'm having a problem linking it to my template. Any help will be highly appreciated. Thank you very much.




0
Avatar
Buang
Avatar
Kev
Penulis Jawaban Terbai

Hello, I was able to find the solution. Anyways, for future reference, here is the code:

def send_email_sample(self):
"""

:return: test function
"""
self.ensure_one()
order_line_data = []
for num, rec in enumerate(self.order_line):
order_line_data.append
({
'default_code': rec.product_id.default_code if rec.product_id.default_code else "None",
'price_unit': rec.price_unit,
'product_uom_qty': int(rec.product_uom_qty),
'product_uom': rec.product_uom.name,
'sequence_number': num + 1
})
json_record = {
'so_number': self.name,
'partner_id': self.partner_id.name,
'date_oder': self.date_order.strftime("%m/%d/%Y"),
'payment_term': self.payment_term_id.name if self.payment_term_id else "None",
'partner_add': "%s %s City %s" % (
self.partner_id.street, self.partner_id.city, self.partner_id.state_id.name),
'order_line': order_line_data,
}
data_record = base64.encodebytes(("%s" % json_record).encode())
ir_values = {
'name': "SALESORDER_%s.txt" % self.name,
'type': 'binary',
'datas': data_record,
'store_fname': data_record,
'mimetype': 'text/csv',
}
data_id = self.env['ir.attachment'].create(ir_values)
template_id = self.env.ref('custom_module.email_template_test_send').id
template =
self.env['mail.template'].browse(template_id)
template.attachment_ids = [(6,0, [data_id.id])]
template.send_mail(self.id, force_send=True)
template.attachment_ids = [(3, data_id.id)]

This code is the important one for attachment, and also make sure to import base64 library:

data_record = base64.encodebytes(("%s" % json_record).encode())
ir_values = {
'name': "SALESORDER_%s.txt" % self.name,
'type': 'binary',
'datas': data_record,
'store_fname': data_record,
'mimetype': 'text/csv',
}
data_id = self.env['ir.attachment'].create(ir_values)
template_id = self.env.ref('custom_module.email_template_test_send').id
template = self.env['mail.template'].browse(template_id)
template.attachment_ids = [(6,0, [data_id.id])]
template.send_mail(self.id, force_send=True)
template.attachment_ids = [(3, data_id.id)]

First, I encode the string into bytes then pass it to a variable name data_record:

data_record = base64.encodebytes(("%s" % json_record).encode())

Then create a context for my file attachment and create it in ir.attachment table:

ir_values = {
'name': "SALESORDER_%s.txt" % self.name,
'type': 'binary',
'datas': data_record,
'store_fname': data_record,
'mimetype': 'text/csv',
}
data_id = self.env['ir.attachment'].create(ir_values)

the rest are just to query the id of the template in mail.template, add the attachment id to attachmet_ids:

template_id = self.env.ref('custom_module.email_template_test_send').id
template = self.env['mail.template'].browse(template_id)
template.attachment_ids = [(6,0, [data_id.id])]
template.send_mail(self.id, force_send=True)
template.attachment_ids = [(3, data_id.id)]

I use the [(3, data_id.id)] to unlink the attachment. You can also choose not to include it in your code.

2
Avatar
Buang
Menikmati diskusi? Jangan hanya membaca, ikuti!

Buat akun sekarang untuk menikmati fitur eksklufi dan agar terlibat dengan komunitas kami!

Daftar
Post Terkait Replies Tampilan Aktivitas
Preview on Attachement field (ir.attachment)
attachment Odoo13.0 v13
Avatar
0
Nov 20
3554
How We Can add Attachment field in exiting view in odoo
attachment file odoo Odoo13.0
Avatar
Avatar
Avatar
2
Feb 20
15643
Odoo 13. Don't close a wizard when Click a button Diselesaikan
Odoo13.0
Avatar
Avatar
Avatar
Avatar
4
Mei 24
14035
Permission error while user printing excel sheet report in odoo 13 Diselesaikan
Odoo13.0
Avatar
Avatar
1
Apr 24
4291
One2many field edited from Transient Model cannot be made empty.
Odoo13.0
Avatar
0
Nov 23
2969
Komunitas
  • Tutorial-tutorial
  • Dokumentasi
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Terjemahan
Layanan
  • Odoo.sh Hosting
  • Bantuan
  • Peningkatan
  • Custom Development
  • Pendidikan
  • Temukan Akuntan
  • Temukan Mitra
  • Menjadi Partner
Tentang Kami
  • Perusahaan kami
  • Aset Merek
  • Hubungi kami
  • Tugas
  • Acara
  • Podcast
  • Blog
  • Pelanggan
  • Hukum • Privasi
  • Keamanan
الْعَرَبيّة 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 adalah rangkaian aplikasi bisnis open source yang mencakup semua kebutuhan perusahaan Anda: CRM, eCommerce, akuntansi, inventaris, point of sale, manajemen project, dan seterusnya.

Mudah digunakan dan terintegrasi penuh pada saat yang sama adalah value proposition unik 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