Perspective File Upload and Email

I am trying to do two tasks and I’m not sure where the failure point is.
First, use the “File Upload” component to select files, second, send those files in an email as an attachment (system.net.sendEmail). I can’t seem to get either to work separately nor in tandem.

Does the filepath prop appear when a file is selected with the file upload? How does one reference that in the script to get it to send along with the email?

Can you post the script you’re working from (maybe clear out any email server/SMTP settings)?

Sure thing. This one works:

date = system.date.now()
	location = self.parent.parent.getChild("InfoContainer").getChild("PlantSelection").props.text
	name = self.parent.parent.getChild("InfoContainer").getChild("NameField").props.text
	email = self.parent.parent.getChild("InfoContainer").getChild("EmailField").props.text
	phone = self.parent.parent.getChild("InfoContainer").getChild("PhoneField").props.value
	ext = self.parent.parent.getChild("InfoContainer").getChild("ExtField").props.value
	severity = self.parent.parent.getChild("SevContainer").getChild("RadioGroup").props.value
	teamviewer = self.parent.parent.getChild("SevContainer").getChild("TextField").props.text
	ticketType = self.parent.parent.getChild("TicketType").getChild("RadioGroup").props.value
	user = self.session.props.auth.user.userName
	userEmail = self.session.props.auth.user.email
	problem = self.session.custom.Description

	body = "Test Ticket \n"
	body += "Posted: {} \n".format(date)
	body += "Busines Unit {} \n".format(location)
	body += "Submitted by {} \n".format(name)
	body += "Phone Contact: {} , ext: {} \n".format(phone,ext)
	body += "TeamViewer Number: {} \n".format(teamviewer)
	body += "Ticket Type: {} \n".format(ticketType)
	body += "Severity: {} \n".format(severity)
	body += "User: {} \n".format(user)
	body += "Description of Problem: {} \n".format(problem)
	#body += "Attachments: {}".format(fileName)
	
	recipients = email
	cc_recipients = ["jcaudle@atlasroofing.com"]
	smtp_server = 'AtlasIT'
	system.net.sendEmail(smtpProfile=smtp_server, fromAddr="AtlasIT@atlasroofing.com", subject="Test Ticket", body=body, html=0, to=recipients, cc=cc_recipients)
	system.perspective.navigate('/ticketsubmitted')

But when I try to tie in attachments it fails (I apologize for the lack of elegance):

filePath = system.file.openFile()
	if filePath != None:
		fileName = filePath.split("\\")[-1]
		fileData = system.file.readFileAsBytes(filePath)
	
		body = "Test Ticket \n"
		body += "Posted: {} \n".format(date)
		body += "Busines Unit {} \n".format(location)
		body += "Submitted by {} \n".format(name)
		body += "Phone Contact: {} , ext: {} \n".format(phone,ext)
		body += "TeamViewer Number: {} \n".format(teamviewer)
		body += "Ticket Type: {} \n".format(ticketType)
		body += "Severity: {} \n".format(severity)
		body += "User: {} \n".format(user)
		body += "Description of Problem: {} \n".format(problem)
		body += "Attachments: {}".format(fileName)
		
		recipients = email
		cc_recipients = ["jcaudle@atlasroofing.com"]
		smtp_server = 'AtlasIT'
		system.net.sendEmail(smtpProfile=smtp_server, fromAddr="AtlasIT@atlasroofing.com", subject="Test Ticket", body=body, html=0, to=recipients, cc=cc_recipients, [fileName], [fileData])
		system.perspective.navigate('/ticketsubmitted')
		
	else:
		body = "Test Ticket \n"
		body += "Posted: {} \n".format(date)
		body += "Busines Unit {} \n".format(location)
		body += "Submitted by {} \n".format(name)
		body += "Phone Contact: {} , ext: {} \n".format(phone,ext)
		body += "TeamViewer Number: {} \n".format(teamviewer)
		body += "Ticket Type: {} \n".format(ticketType)
		body += "Severity: {} \n".format(severity)
		body += "User: {} \n".format(user)
		body += "Description of Problem: {} \n".format(problem)
		#body += "Attachments: {}".format(fileName)
		
		recipients = email
		cc_recipients = ["jcaudle@atlasroofing.com"]
		smtp_server = 'AtlasIT'
		system.net.sendEmail(smtpProfile=smtp_server, fromAddr="AtlasIT@atlasroofing.com", subject="Test Ticket", body=body, html=0, to=recipients, cc=cc_recipients)
		system.perspective.navigate('/ticketsubmitted')

It looks like you’re only assigning a value to fileName if filePath != None, so if the filePath IS None, then you’d encounter an unresolved reference.

Edit: nevermind, the formatting of your script was unclear.

Which block fails? the part WITH the attachments included, or the part without?

The following changes would also remove a lot of duplication and make it a bit easier to troubleshoot going forward.

    date = system.date.now()
	location = self.parent.parent.getChild("InfoContainer").getChild("PlantSelection").props.text
	name = self.parent.parent.getChild("InfoContainer").getChild("NameField").props.text
	email = self.parent.parent.getChild("InfoContainer").getChild("EmailField").props.text
	phone = self.parent.parent.getChild("InfoContainer").getChild("PhoneField").props.value
	ext = self.parent.parent.getChild("InfoContainer").getChild("ExtField").props.value
	severity = self.parent.parent.getChild("SevContainer").getChild("RadioGroup").props.value
	teamviewer = self.parent.parent.getChild("SevContainer").getChild("TextField").props.text
	ticketType = self.parent.parent.getChild("TicketType").getChild("RadioGroup").props.value
	user = self.session.props.auth.user.userName
	userEmail = self.session.props.auth.user.email
	problem = self.session.custom.Description
    
	body = "Test Ticket \n"
	body += "Posted: {} \n".format(date)
	body += "Busines Unit {} \n".format(location)
	body += "Submitted by {} \n".format(name)
	body += "Phone Contact: {} , ext: {} \n".format(phone,ext)
	body += "TeamViewer Number: {} \n".format(teamviewer)
	body += "Ticket Type: {} \n".format(ticketType)
	body += "Severity: {} \n".format(severity)
	body += "User: {} \n".format(user)
	body += "Description of Problem: {} \n".format(problem)

    recipients = email
	cc_recipients = ["jcaudle@atlasroofing.com"]
	smtp_server = 'AtlasIT'

    filePath = system.file.openFile()
	if filePath is not None:
		fileName = filePath.split("\\")[-1]
		fileData = system.file.readFileAsBytes(filePath)
	    body += "Attachments: {}".format(fileName)
        system.net.sendEmail(smtpProfile=smtp_server, fromAddr="AtlasIT@atlasroofing.com", subject="Test Ticket", body=body, html=0, to=recipients, cc=cc_recipients, [fileName], [fileData])
    else:
    	system.net.sendEmail(smtpProfile=smtp_server, fromAddr="AtlasIT@atlasroofing.com", subject="Test Ticket", body=body, html=0, to=recipients, cc=cc_recipients)
	system.perspective.navigate('/ticketsubmitted')

Thanks for the truncation. I’m not succeeding with your code execute either. If I disable all the attachment related things then it works.

date = system.date.now()
location = self.parent.parent.getChild("InfoContainer").getChild("PlantSelection").props.text
name = self.parent.parent.getChild("InfoContainer").getChild("NameField").props.text
email = self.parent.parent.getChild("InfoContainer").getChild("EmailField").props.text
phone = self.parent.parent.getChild("InfoContainer").getChild("PhoneField").props.value
ext = self.parent.parent.getChild("InfoContainer").getChild("ExtField").props.value
severity = self.parent.parent.getChild("SevContainer").getChild("RadioGroup").props.value
teamviewer = self.parent.parent.getChild("SevContainer").getChild("TextField").props.text
ticketType = self.parent.parent.getChild("TicketType").getChild("RadioGroup").props.value
user = self.session.props.auth.user.userName
userEmail = self.session.props.auth.user.email
problem = self.session.custom.Description

body = "Test Ticket \n"
body += "Posted: {} \n".format(date)
body += "Busines Unit {} \n".format(location)
body += "Submitted by {} \n".format(name)
body += "Phone Contact: {} , ext: {} \n".format(phone,ext)
body += "TeamViewer Number: {} \n".format(teamviewer)
body += "Ticket Type: {} \n".format(ticketType)
body += "Severity: {} \n".format(severity)
body += "User: {} \n".format(user)
body += "Description of Problem: {} \n".format(problem)
recipients = email
cc_recipients = ["jcaudle@atlasroofing.com"]
smtp_server = 'AtlasGmail'
filePath = system.file.openFile()
if filePath is not None:
	fileName = filePath.split("\\")[-1]
	fileData = system.file.readFileAsBytes(filePath)
    body += "Attachments: {}".format(fileName)
    system.net.sendEmail(smtpProfile=smtp_server, fromAddr="AtlasIT@atlasroofing.com", subject="Test Ticket", body=body, html=0, to=recipients, cc=cc_recipients, [fileName], [fileData])
else:
	system.net.sendEmail(smtpProfile=smtp_server, fromAddr="AtlasIT@atlasroofing.com", subject="Test Ticket", body=body, html=0, to=recipients, cc=cc_recipients)
system.perspective.navigate('/ticketsubmitted')

Right, but you haven’t answered one of my previous questions: Is there actually an attachment? I understand your code is failing, but without a stacktrace of the failure or a clarification of WHERE it’s failing, I won’t be able to help very much.

If you do NOT have an attachment, does the email get sent?

Please place some logging into the attachment block:

systemutil.getLogger('IAQA').info('Type: {0} | Value {1}'.format(str(type(fileName)), filename))

I apologize for that. I tried it with and without an attachment. Is there a noob guide to using the logger and getting a stack trace?

So, there’s three things we’re going to try in order to figure out where the failure is:

  1. First, we need to fix the actual method call because you’re passing named arguments and then positional arguments, which is a big no-no in python. Jython doesn’t provide any warnings when you do it, but it can mask failures and so you shouldn’t do it. *
  2. Then we’re going to turn on Debug mode for your SMTP Profile. Go to the Gateway configuration page and select Email Settings. Then select your SMTP Profile “AtlasIT”. Toward the bottom, select the checkbox to enable advanced properties, and then select the checkbox to Enable Debug Mode, and save your changes.
  3. Finally, consider placing some temporary logging into your code so that you can see what steps have successfully completed and what your true values are. Make sure you’re also looking at the type of the value, as Qualified Values can cause problems in scripts because they’re an object which is sometimes coerced into returning its internal value attribute instead of itself, and so it looks like you’re dealing with a simple value until you attempt to operate on it.

Once these steps are done, try running it again and let us know what new results you see.

(*)

system.net.sendEmail(smtpProfile=smtp_server, fromAddr="AtlasIT@atlasroofing.com", subject="Test Ticket", body=body, html=0, to=recipients, cc=cc_recipients, attachments=[fileName], attachmentData=[fileData])
2 Likes