Sending email via onClick button script

I am trying to make a button onClick script in perspective where it uses function to send an email with a body to someone. This is the template I followed and since I use Gmail I wanted to do it through gmail.

I know that there is a potential it can be sent to spam consistently for the receiver and your account can be terminated however I'm willing to run the risk and this is purely experimental at the moment. When I put my email and click on the button the following error message pop's up after like 6 seconds of waiting.

All of these are 'Socket Timed Out: read Timed Out' exceptions when I hit "Details" on the error. I just don't know what could be the fix for it. I only been using ignition for a couple months so bear with me

Hi Richard, I think the likeliest reason it's timing out is because you didn't supply authentication into the function sendEmail function using the username and password named parameters. You could test that out, but I wouldn't just leave your login credentials in a script - what would be better is to add the SMTP details to the gateway, which you can then reference from sendEmail, alarm notifications, report schedules, etc.

1 Like

I see, currently SSL/TLS is not setup in the gateway so I suppose I should do that first. How would I reference that information using sendEmail?

I stripped this out of my code and erased any private data, hopefully I didn't break anything doing that. My email had attachments from a table so I can put that code back in if you need help with that.

smtp_server = 'AdminEmail' #Name of of the SMTP server in the gateway
EmailTo = ''
sender = "senders Email"
subject = "Your Subject"
body = "Info in body of email", fromAddr=sender, subject=subject, body=body, html=1, to=EmailTo)

I spend 2-3 hours for understood how to send an email via Gmail.... it is a little bit complicated but works; you need to enable/create an "application" in a google account and give you a secret code. exist many tutorials on youtube about that (like that: How to Automate Emails with Python [New Method 2022] - YouTube) after you have the code/password you need to configure the smtpprofile on the gateway

There shouldn't be a need to use Gmail's mail servers for outbound if there is a mail server on the company network. If there is then it may allow emails from within the network without credentials and that would eliminate the need for storing credentials in your script.

I also suggest that the "from" email address makes it clear what the source of the email is. I'm working for an international company and if one of my scripts started to generate vast amounts of email it would be important for the IT guys to be able to identify the culprit. As a result I use the form <country>.<location>.<department>.<machineID>@<domain>. e.g., It's long but unambiguous and is instantly clear that the mail is generated internally and by what. We do this on any devices (UPS, backup scripts, etc.) that are capable of sending out emails. You can then sort messages by sender, etc.

Another point is that internal emails never have to leave the company domain.

1 Like