Can anyone help on how to send an email with excel attachment using mandrill in python. I am using python 3.6.8 version as my project is old version and has dependencies which do not allow me to upgrade the python version. Here is the code I am using
def _send_message(to, subject, htmlbody, add_header=True, cc=None,
bcc=None, attachment=None, attachment_content=None):
if os.getenv('MGC_ENV') != 'prod':
subject = '[{0}] '.format(os.getenv('MGC_ENV')) + subject
if add_header:
subject = 'MAP {0}'.format(subject)
recipients = _generate_recipients(to, cc, bcc)
if len(recipients) == 0:
return
bundle = {
'to': recipients,
'from_email': os.getenv('FROM_EMAIL'),
'subject': subject,
'html': htmlbody,
'tags': _MANDRILL_TAGS,
'preserve_recipients': True
}
if attachment and attachment_content:
attachment = [{
"type": 'application/xlsx',
"name": 'OrderConfirmation.xlsx',
"content": attachment_content.toString('base64')
}]
bundle["attachments"] = attachment
if os.getenv('ENABLE_NOTIFICATIONS') == 'false':
return
_mandrill_client.messages.send(bundle)
The issue in the above code is the attachment is expecting the content as string encoded with base64 and when tried to set bytes array it wont work.
When we try to convert the excel into the type string it is not able to read and is breaking.
def export(licenses, plain_str=True): workbook = xlwt.Workbook()
products_sheet = _create_product_sheet(workbook)
product_row_offset = 1
cell_style = xlwt.easyxf('font: underline single; font: color
blue')
if licenses and len(licenses) > 0:
for lic in licenses:
row = products_sheet.row(product_row_offset)
_write_product_row(row, lic, cell_style=cell_style)
product_row_offset += 1
if plain_str:
content = utils.workbook_to_str(workbook)
else:
content = utils.workbook_to_bytes(workbook)
return content
def workbook_to_str(workbook):
io_buffer = io.StringIO()
workbook.save(io_buffer)
return io_buffer.read()
def workbook_to_bytes(workbook):
io_buffer = io.BytesIO()
workbook.save(io_buffer)
return io_buffer.getvalue()
Thanks in advance for your answer.
I'm no Python wizard (yet) but I've got the MailChimp integration working (pretty much the same as Mandril) using the below.
The parts that I struggled with were encoding the xlsx -
base64_encoded = base64.b64encode(df_xlsx).decode('UTF-8')and finding the correct attachment type'type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'