İçereği Atla
Odoo Menü
  • Portal
  • Ücretsiz deneyin
  • Uygulamalar
    Finans
    • Muhasebe
    • Faturalama
    • Masraf Yönetimi
    • Elektronik Çizelge (BI)
    • Belgeler
    • İmza
    Satış
    • Müşteri İlişkileri Yönetimi (CRM)
    • Satış
    • Satış Noktası Mağaza
    • Satış Noktası Restoran
    • Abonelikler
    • Kiralama
    Web Sitesi
    • Web Sitesi Oluşturucu
    • eTicaret
    • Blog
    • Forum
    • Canlı Sohbet
    • eÖğrenme
    Tedarik Zinciri
    • Envanter
    • Üretim
    • Ürün Yaşam Döngüsü Yönetimi
    • Satın Alma
    • Bakım
    • Kalite
    İnsan Kaynakları
    • Çalışanlar
    • İşe Alım
    • İzin
    • Değerlendirme
    • Referans
    • Filo Yönetimi
    Pazarlama
    • Sosyal Medyada Pazarlama
    • E-posta ile Pazarlama
    • SMS ile Pazarlama
    • Etkinlikler
    • Pazarlama Otomasyonu
    • Anket
    Hizmetler
    • Proje Yönetimi
    • Çalışma Çizelgeleri
    • Saha Hizmeti
    • Yardım Masası
    • Planlama
    • Randevular
    Verimlilik
    • Sohbet
    • Onay
    • Nesnelerin İnterneti
    • VoIP
    • Bilgi Bankası
    • WhatsApp
    Üçüncü taraf uygulamalar Odoo Stüdyo Odoo Bulut Platformu
  • Sektörler
    Perakende satış
    • Kitapçı
    • Giyim Mağazası
    • Mobilya Mağazası
    • Gıda Marketi
    • Hırdavat Dükkanı
    • Oyuncak Dükkanı
    Gıda ve Konaklama
    • Bar ve Pub
    • Restoran
    • Fast Food Restoranı
    • Konuk Evi
    • İçecek Distribütörü
    • Otel
    Gayrimenkul
    • Emlak Acentesi
    • Mimarlık Firması
    • İnşaat
    • Emlak Yönetimi
    • Bahçe Tasarımı
    • Mülk Sahipleri Derneği
    Uzmanlık
    • Muhasebe Firması
    • Odoo Partner
    • Pazarlama Ajansı
    • Hukuk Firması
    • Yetenek Kazanımı
    • Denetim ve Belgelendirme
    Üretim
    • Tekstil
    • Metal
    • Mobilyalar
    • Gıda
    • Bira fabrikası
    • Kurumsal Hediye
    Sağlık ve Spor
    • Spor Kulübü
    • Optik Mağazası
    • Fitness Merkezi
    • Sağlıklı Yaşam Merkezi
    • Eczane
    • Kuaför Salonu
    Ticaret
    • Tamirci
    • BT Donanım & Destek
    • Güneş Enerjisi Sistemleri
    • Ayakkabı İmalatçısı
    • Temizlik Hizmetleri
    • HVAC Hizmetleri
    Diğerleri
    • Kar Amacı Gütmeyen Kuruluş
    • Çevre Ajansı
    • Reklam Panosu Kiralama
    • Fotoğrafçılık
    • Bisiklet Kiralama
    • Yazılım Bayisi
    Tüm Sektörlere Göz Atın
  • Topluluk
    Öğrenim
    • Eğitim Araçları
    • Dokümantasyon
    • Sertifikasyonlar
    • Eğitim Etkinlikleri
    • Blog
    • Podcast
    Eğitim ve Gelişim
    • Eğitim Programı
    • Scale Up! İşletme Oyunu
    • Odoo'yu Ziyaret Edin
    Yazılım
    • İndirin
    • Sürümleri Kıyaslayın
    • Sürümler
    İş Birliği
    • Github
    • Forum
    • Etkinlikler
    • Çeviriler
    • Partner Olun
    • Partnerler için Hizmetler
    • Muhasebe Firmanızı Kaydettirin
    Hizmetler
    • Partner Bulun
    • Muhasebeci Bulun
    • Bir danışmanla görüşün
    • Kurulum Hizmetleri
    • Müşteri Referansları
    • Destek
    • Sürüm Yükseltme
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Demo randevusu alın
  • Fiyatlandırma
  • Yardım

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

  • Müşteri İlişkileri Yönetimi
  • e-Commerce
  • Muhasebe
  • Envanter
  • PoS
  • Proje Yönetimi
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Etiketler (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Etiketler (View all)
odoo accounting v14 pos v15
About this forum
Yardım

Odoo 13 Attach a file in email template

Abone Ol

Get notified when there's activity on this post

Bu soru işaretlendi
attachmentsending mail with attachmentOdoo13.0
1 Cevapla
13208 Görünümler
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
Vazgeç
Avatar
Kev
Üretici En İyi Yanıt

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
Vazgeç
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Üye Ol
İlgili Gönderiler Cevaplar Görünümler Aktivite
Preview on Attachement field (ir.attachment)
attachment Odoo13.0 v13
Avatar
0
Kas 20
3546
How We Can add Attachment field in exiting view in odoo
attachment file odoo Odoo13.0
Avatar
Avatar
Avatar
2
Şub 20
15630
Odoo 13. Don't close a wizard when Click a button Çözüldü
Odoo13.0
Avatar
Avatar
Avatar
Avatar
4
May 24
13992
Permission error while user printing excel sheet report in odoo 13 Çözüldü
Odoo13.0
Avatar
Avatar
1
Nis 24
4275
One2many field edited from Transient Model cannot be made empty.
Odoo13.0
Avatar
0
Kas 23
2958
Topluluk
  • Eğitim Araçları
  • Dokümantasyon
  • Forum
Açık Kaynak
  • İndirin
  • Github
  • Runbot
  • Çeviriler
Hizmetler
  • Odoo.sh Hosting
  • Destek
  • Sürüm Yükseltme
  • Özel Geliştirmeler
  • Eğitim
  • Muhasebeci Bulun
  • Partner Bulun
  • Partner Olun
Hakkında
  • Şirketimiz
  • Pazarlama Gereçleri
  • İletişim
  • Kariyer
  • Etkinlikler
  • Podcast
  • Blog
  • Müşteriler
  • Hukuki • Gizlilik
  • Güvenlik
الْعَرَبيّة 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, müşteri ilişkileri yönetimi, eTicaret, muhasebe, envanter, satış noktası, proje yönetimi gibi şirketinizin tüm ihtiyaçlarını karşılayan bir açık kaynak işletme uygulamaları paketidir.

Odoo’nun eşsiz değer önermesi, aynı anda hem kullanımının çok kolay olup hem de tamamen entegre olmasıdır.

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