跳至内容
菜单
此问题已终结

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.




形象
丢弃
编写者 最佳答案

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.

形象
丢弃
相关帖文 回复 查看 活动
0
11月 20
2622
2
2月 20
14705
4
5月 24
12344
1
4月 24
3033
0
11月 23
1854