SMTP server email is not working in the script

I have successfully configured the SMTP Gmail server in ignition. I have tested it too, able to send email from the ignition test option. When I try to use system.net.sendEmail in my script, not able to send email. Here is my code,

smtp = "smtp.gmail.com:587"
		myFrom = "srinisekar9899@gmail.com"
		subject = "Here is the email!"
		body = "New Ticket has been created"
		html = 0
		recipients = ["seenudhanum283@gmail.com"]
		system.net.sendEmail(smtp,myFrom, subject, body, html, recipients)

Attached the image of SMTP server setting.

Kindly help me out. I am using ignition in AWS server .

Adding the error message!

" Error running action 'dom.onClick' on Templates/Ticketing/createTicketPopUp@PaddNewTicket/root/DownloadFlex/Button: Traceback (most recent call last): File "function:runAction", line 23, in runAction 530-5.7.0 About SMTP error messages - Google Workspace Admin Help and review RFC 3207 530 5.7.0 specifications. d2e1a72fcca58-7042eba77e2sm6137676b3a.104 - gsmtp at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2358) at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1823) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1300) at javax.mail.Transport.send0(Transport.java:255) at javax.mail.Transport.send(Transport.java:124) at com.inductiveautomation.ignition.gateway.script.GatewayNetUtilities.sendEmail(GatewayNetUtilities.java:262) at com.inductiveautomation.ignition.gateway.script.GatewayNetUtilities.sendEmail(GatewayNetUtilities.java:133) at com.inductiveautomation.ignition.gateway.script.GatewayNetUtilities._sendEmail(GatewayNetUtilities.java:59) at com.inductiveautomation.ignition.common.script.builtin.AbstractNetUtilities.sendEmail(AbstractNetUtilities.java:161) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) com.sun.mail.smtp.SMTPSendFailedException: com.sun.mail.smtp.SMTPSendFailedException: 530-5.7.0 Must issue a STARTTLS command first. For more information, go to 530-5.7.0 About SMTP error messages - Google Workspace Admin Help and review RFC 3207 530 5.7.0 specifications. d2e1a72fcca58-7042eba77e2sm6137676b3a.104 - gsmtp"

Use the smtpProfile argument to system.net.sendEmail() instead of the smtp argument. When you use smtp, the function bypasses the profile you tested.

1 Like

Thank you for reply. I tried the same , but same issue, not able to send the mail.

#		smtpProfile  = "smtp.gmail.com:587"
#		myFrom = "srinisekar9899@gmail.com"
#		subject = "Here is the email!"
#		body = "New Ticket has been created"
#		html = 0
#		recipients = ["srinisekar9899@gmail.com"]
#		system.net.sendEmail(smtpProfile,myFrom, subject, body, html, recipients)
##		# Closes a docked view with the given dock id.

You've commented out all your code. What are you actually running?

Tip: set the "from" to something like scriptname.projectname@gatewayname.mycompany.com so that if it starts to go mad you or your IT department will be able to find the source of the problem.

Are you posting your real email address on a public forum?

1 Like

All you have done here is rename your input variable from smtp to smtpProfile. You are still passing in an smtp parameter to system.net.sendEmail().

What is the name of the SMTP profile you have configured in your gateway? You need to pass this name into the function into the smtpProfile parameter

1 Like

thank you for your reply. I have uncommented the code only then i have run the code. But facing same issue.
tip.... :smile:

Thank you for your reply. I will try now and get back to you

Hi ! I tried the below code, still not able to send email. Kindly help me with your input.

smtp = "smtp.gmail.com:587"
		smtpProf = "TicketEmailServer"
		myFrom = "srinisekar9899@gmail.com"
		subject = "Here is the email!"
		body = "New Ticket has been created"
		html = 0
		myuser = "srinisekar9899@gmail.com"
		mypass = "zgeb unjz orow hseh"
		recipients = ["srinisekar9899@gmail.com"]
		system.net.sendEmail(smtp,myFrom, subject, body, html, recipients,username=myuser, password=mypass, smtpProfile=smtpProf)

As per my last screenshot, if you are supplying an SMTP profile argument the function will ignore smtp, username and password inputs so you don't need to supply these (nor should you be sharing your password on a public forum...)

You have supplied the optional html parameter with a 0, when it is a Boolean, so it should be False.

1 Like

thank you. Will check.
All these email, password are fake ones. :blush:

I tired the say, but same not working,

            myFrom = "srinisekar9899@gmail.com"
			subject = "Here is the email!"
			body = "New Ticket has been created"
			html = False
#		myuser = "srinisekar9899@gmail.com"
#		mypass = "zgeb unjz orow hseh"
			recipients = ["srinisekar9899@gmail.com"]
			system.net.sendEmail(myFrom, subject, body, html, recipients,smtpProfile=smtpProf)

Can you post your entire code block? I think you are still missing something.

Also, have you tested the SMTP Profile to verify that it will send e-mails?

2 Likes

You might try supplying keyword arguments to the whole function instead of just the smtp profile argument. There are a lot of arguments this function can take:

system.net. sendEmail( smtp, fromAddr, [ subject ] , [ body ] , [ html ] , to, [ attachmentNames ] , [ attachmentData ] , [ timeout ] , [ username ] , [ password ] , [ priority ] , [ smtpProfile ] , [ cc ] , [ bcc ] , [ retries ], [replyTo] )

So instead of:
system.net.sendEmail(myFrom, subject, body, html, recipients,smtpProfile=smtpProf)

Try:
system.net.sendEmail(fromAddr=myFrom,subject=subject,body=body,html=html,to=recipients,smtpProfile=smtpProf)

Maybe even strip it down to just the required arguments and build it back up from there:
system.net.sendEmail(fromAddr=myFrom,to=recipients,smtpProfile=smtpProf)

This is assuming your smtpProf variable is a string with the same name as the one you defined in your gateway configuration (screenshot below). It looks like you are missing a line of code where you define this variable, unless you just didn't copy it here. If that is indeed your whole code, I think you are supplying that argument incorrectly.

image

3 Likes

Thank you for your reply. Here is my revised code. Still not able to receive the mail. Kindly check .

        smtpProf = "TicketEmailServer"
		myFrom = "srinisekar9899@gmail.com"
		subject = "Here is the email!"
		body = "New Ticket has been created"
		html = False
#		myuser = "srinisekar9899@gmail.com"
#		mypass = "zgeb unjz orow hseh"
		recipients = ["srinisekar9899@gmail.com"]
		system.net.sendEmail(fromAddr=myFrom,subject=subject,body=body,html=html,to=recipients,smtpProfile=smtpProf)

Thank you for your reply. Actually just I am trying to collect the data in the text field areas in my form to update in the DB.

#	self.parent.parent.getChild("testing").props.text = "deviceName"
	ticketID = system.db.runNamedQuery("Tickets/get_LastTicketID").getValueAt(0, 0)
	deviceName = self.parent.parent.getChild("Site_SysFlexRepeater").props.instances[0].value
	siteID = self.parent.parent.getChild("Site_SysFlexRepeater").props.instances[1].value
	ticketCategory = self.parent.parent.getChild("Reason_UserFlexRepeater_0").props.instances[0].options[0].value
	ticketDescription = self.parent.parent.getChild("FlexContainer").getChild("TextArea").props.text
	ticketStatus = 	"OPEN"
	userid = self.parent.parent.getChild("Reason_UserFlexRepeater_0").props.instances[1].options[0].value
#	system.db.runNamedQuery("Tickets/insert_TicketMaster", {"inVarTicketID": ticketID , "inVarTicketStatus": ticketStatus})
#	system.db.runNamedQuery("Tickets/insert_Ticket", {"inVarTicketID":ticketID, "inVarDeviceName": deviceName, "inVarSiteID": siteID, "inVarTicketCategory": ticketCategory, "inVarTicketDescription": ticketDescription, "inVarTicketStatus": ticketStatus, "inVarUserID": userid})

	if deviceName != '' and siteID != '' and ticketCategory != '' and ticketDescription != '':
		system.db.runNamedQuery("Tickets/insert_TicketMaster", {"inVarTicketID": ticketID , "inVarTicketStatus": ticketStatus})
		system.db.runNamedQuery("Tickets/insert_Ticket", {"inVarTicketID":ticketID, "inVarDeviceName": deviceName, "inVarSiteID": siteID, "inVarTicketCategory": ticketCategory, "inVarTicketDescription": ticketDescription, "inVarTicketStatus": ticketStatus, "inVarUserID": userid})
		system.perspective.sendMessage('refresh',scope = 'page')
#		smtp = "smtp.gmail.com:587"
		smtpProf = "TicketEmailServer"
		myFrom = "srinisekar9899@gmail.com"
		subject = "Here is the email!"
		body = "New Ticket has been created"
		html = False
#		myuser = "srinisekar9899@gmail.com"
#		mypass = "zgeb unjz orow hseh"
		recipients = ["srinisekar9899@gmail.com"]
		system.net.sendEmail(fromAddr=myFrom,subject=subject,body=body,html=html,to=recipients,smtpProfile=smtpProf)
##		# Closes a docked view with the given dock id.
		system.perspective.closePopup('addNewTicket')
		try:
#			smtpProf = "TicketEmailServer"
			myFrom = "srinisekar9899@gmail.com"
			subject = "Here is the email!"
			body = "New Ticket has been created"
			html = False
#		myuser = "srinisekar9899@gmail.com"
#		mypass = "zgeb unjz orow hseh"
			recipients = ["srinisekar9899@gmail.com"]
			system.net.sendEmail(myFrom, subject, body, html, recipients,smtpProfile=smtpProf)
		except:
			pass
		
	else:
		system.perspective.openPopup('errorMessage','Templates/General/messageBox',params = {'Text':'Enter all the fields!'},modal=True,showCloseIcon=False,viewportBound=True)

Do you know that the e-mail profile works?

You can test it via the web page at Config->Networking->EMail Settings, and then under "More" on the e-mail profile click test.
Once you have that working, then you take the next step to getting the code working.

2 Likes

Thank you bschroder, that has successful message, but still not working

Hmmm... Are you sure you are getting to the line to send the email?

Maybe stick some logging in your logic after the if, and right before the send function just to verify that you are getting to that point.

And absolutely no errors at all when you run this code? Make sure to check both the client and designer.