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.