Unable to open attachments from email sent using system.net.sendEmail

I am working on a Perspective app where I am using the system.net.sendEmail function to send attachments to an email address. I am able to get the email with the attachments but when i open them i get the following error msg. Please help.

image

What’s your script look like? It looks like maybe the data is not being encoded correctly.

  • Here is the script on the “File Upload” Component -
	filename = event.file.name
	filedata = event.file.getBytes()
#	system.perspective.print(filedata)
	upload_path = 'webserver/webapps/main/UploadedFiles/'
	system.util.getLogger('FILEUPLOAD').info('Uploading: \'{0}\' to directory \'{1}\''.format(event.file.name, upload_path))
	event.file.copyTo(upload_path + event.file.name)
  • Here is my script from the “Submit” button that sends the email -
	smtp ="mail.mycompany.com" 
	sender = "myemail@mycompany.com"
	subject = "MES HELP DESK REQUEST"
	if emailAdd ==None:
		emailAdd = " No Email ID on File "
	body =  " Employee Name : "+ fullname + " Email Address : "+ emailAdd + " Severity : "+ priority + " Subject :  " + subject + " Job Number " + jobNum + " Workstation Number" + ws + " Comments : " + comments
	recipients = ["gramesh@imminet.com"]
	filePath ='C:/Program Files/Inductive Automation/Ignition/webserver/webapps/main/UploadedFiles'
	uploadFiles = os.listdir(filePath)
	 
	for i in range(len(uploadFiles)):
		finalPath = filePath+"/"+uploadFiles[i]
		fileData = system.file.readFileAsBytes(finalPath)
		system.net.sendEmail(smtp, sender, subject, body, 0, recipients, [uploadFiles[i]], [fileData[i]])

Do you actually need to persist the files to disk? If you try sending a test email directly in the file upload script, does the attachment come through correctly? If you look at the files saved on disk, are they valid files?

  1. I have other information i take from the user along with the file attachments which i combine together as the body of the email and send it out using another button component after the file upload. For this purpose i am sending the file to a disk and then using that to attach in the email.

  2. Test email - yes i tried to send a test email directly from the file upload component and that works. i was able to open and view the attachment from the email without any issues. but i would like to be able to send the email from another button on the page since there can be cases where there are no attachments as well for the email.

  3. files saved on the disk - Yes they look valid and i am able to open and view the files as well.

Thank you for your help.