I created a scheduled script to run a query and attach the table to a email but when I run it, I don't have the attachment. Am I supposed to create the file on the hard drive temporally then attach it?
type or paste code h
# Run named query to get data
results = system.db.runNamedQuery("Alarms", {})
spreadsheet = system.dataset.toExcel(True, [results])
fileName = "Alarms.xlsx"
fileData = system.dataset.toExcel(True, [results])
body = "<HTML><BODY><H1>This is a big header</H1>"
body += "TEST1 <font color='red'>red</font></BODY></HTML>"
recipients = ["email@email.com"]
smtpProfile = "Email"
attachments = [(fileName, spreadsheet)] # Pass the filename and data as a tuple in a list
system.net.sendEmail(
smtpProfile=smtpProfile,
fromAddr="email@email.com",
subject="Here is the email!",
body=body,
html=1,
to=recipients,
attachments=attachments
)ere
Thank you, I finally got it to work. For anyone else that has difficulties with smtp profile and excel attachment in the future, here is my code for reference:
#runs named queries
result1 = system.db.runNamedQuery("Alarms",{})
result2 = system.db.runNamedQuery("ID",{})
#takes results then places them on separate sheets in excel
spreadsheet = system.dataset.toExcel(True, [result1,result2], [], ["Alarm1","ID"])
filePath = "Alarm Combined.xlsx"
#Send email via smtp profile
body = "<HTML><BODY>This report is automatically generated."
#body += "And this text is <font color='red'>red</font></BODY></HTML>"
recipients = ["example@gmail.com"]
smtp_server = "your smtp profile"
system.net.sendEmail(smtpProfile=smtp_server,
fromAddr="noreply@gmail.com", subject="Alarms Excel File",
body=body, html=1, to=recipients, attachmentNames=[filePath],
attachmentData=[spreadsheet])