Email with Excel file Attached without prompting

Hello,

I’m sending emails from Ignition with a xls file attached using the system.dataset.exportExcel function.

Here’s my script :

table=event.source.parent.parent.getComponent('Table')
data=table.data
filePath=system.dataset.exportExcel("File.xls", 1, data)
if filePath != None:
   fileName = filePath.split("\\")[-1]
   fileData = system.file.readFileAsBytes(filePath)
   system.net.sendEmail("smtp.example","test@test.com","Subject","Body",0,["i.mathias@courtoisenergies.fr"],[fileName],[fileData])

I’d like to do the same thing but without prompting the user on where to save the file, just as I’m doing with the printToImage function which automatically saves the file in a temp folder and let me access it through its name rather than its path.

Example :

window = system.gui.getWindow("WindowPath") name_window = "Test.png" system.print.printToImage(window,name_window) fileName = "Test.png" fileData = fpmi.file.readFileAsBytes(fileName) system.net.sendEmail("smtp.example","test@test.com","Email de test","Contenu de l'email",0,["i.mathias@courtoisenergies.fr"],[fileName],[fileData])

Maybe this can be done using the system.dataset.dataSetToExcel function? But then I have to use file.writeFile to use the return of this function and specify a path, not just a name… Any idea?

1 Like

When using Windows VISTA/7/8, you can write to the public directory:

C:\Users\Public\

Yes, you should use the system.dataset.dataSetToExcel function.

You can use the system.file.getTempFile function to get a path to a temporary file that you write to.

Actually, you do not need to write the excel string to a file. All you need to do is convert the string into bytes and email the bytes.

Here is a code example:

table = event.source.parent.getComponent("Table")
excelstr = system.dataset.dataSetToExcel(True,[table.data])
excelbytes = excelstr.encode("UTF8")
filename = "myexcelfile.xls"
system.net.sendEmail("smtp.example","test@test.com","Email de test","Contenu de l'email",0,["i.mathias@courtoisenergies.fr"],[filename],[excelbytes])
2 Likes

Thanks a lot !! :prayer:

For anyone like myself stumbling into this thread a decade late, this action is even easier now. At least in my test I was able to do this without encoding to UTF8.

I used spreadsheet = system.dataset.toExcel(True, [dataset]), skipped the next line for encoding and plugged [spreadsheet] into the file data field on the email command.

I'm only commenting because I almost gave up after not being able to use the encode function.