The Altair Community is migrating to a new platform to provide a better experience for you. In preparation for the migration, the Altair Community is on read-only mode from October 28 - November 6, 2024. Technical support via cases will continue to work as is. For any urgent requests from Students/Faculty members, please submit the form linked here
SendMail Operator with Attachment
Contributor II
We often start usecases with the first interface to the business user being a mail with results.
This is a really quick way to get results to him without a lot of invest in interface development.
It would be quite handsome to be able to attach one or multiple files to that Mail because it is inconvenient to put data into the body of the mail.
Tagged:
12
Comments
<?xml version="1.0" encoding="UTF-8"?><process version="8.2.000"> <context> <input/> <output/> <macros/> </context> <operator activated="true" class="process" compatibility="8.2.000" expanded="true" name="Process"> <process expanded="true"> <operator activated="true" class="generate_data" compatibility="8.2.000" expanded="true" height="68" name="Generate Data" width="90" x="179" y="34"/> <operator activated="true" class="python_scripting:execute_python" compatibility="7.4.000" expanded="true" height="82" name="Send_Mail" width="90" x="380" y="34"> <parameter key="script" value="import datetime import os import zipfile import pandas import smtplib from xlsxwriter.utility import xl_rowcol_to_cell from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase from email import encoders writer = pandas.ExcelWriter('file.xlsx', engine='xlsxwriter') now = datetime.datetime.now() def rm_main(data): 	 	#Passing data to an excel file 	data.to_excel(writer, sheet_name='Name',index=False) 	writer.save() 	path = "file.xlsx" 	 	 	z = zipfile.ZipFile('zipped_file.zip', 'w') 	z.write(path) # has to exist 	 	z.close() 	#Define who is going to recieve the file 	fromaddr = "mail@mail.com" 	toaddr = ["customer@mail.com"] 	msg = MIMEMultipart() 	msg['From'] = fromaddr 	msg['To'] = ", ".join(toaddr) 	msg['Subject'] = "Subject" 	 	body = "I'm sending a mail with a ziped file" 	part = MIMEBase('application', "octet-stream") 	part.set_payload(open(path2, "rb").read()) 	encoders.encode_base64(part) 	part.add_header('Content-Disposition', 'attachment; filename="zipped_file.zip"') 	msg.attach(part) 	msg.attach(MIMEText(body, 'html')) 	 	#Configure you SMTP options 	server = smtplib.SMTP('SMTP', 587) 	server.starttls() 	server.login(fromaddr,"YOUR_PASSWORD") 	#End configuration of email 	text = msg 	 	server.sendmail(fromaddr, toaddr, msg.as_string()) 	server.quit() 	return 0"/> </operator> <connect from_op="Generate Data" from_port="output" to_op="Send_Mail" to_port="input 1"/> <connect from_op="Send_Mail" from_port="output 1" to_port="result 1"/> <portSpacing port="source_input 1" spacing="0"/> <portSpacing port="sink_result 1" spacing="0"/> <portSpacing port="sink_result 2" spacing="0"/> </process> </operator> </process>