# Sending email via onClick button script

**URL:** <https://forum.inductiveautomation.com/t/sending-email-via-onclick-button-script/64709>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [September 30, 2022, 3:01pm UTC](https://forum.inductiveautomation.com/t/sending-email-via-onclick-button-script/64709 "2022-09-30T15:01:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard\_Said](https://avatars.discourse-cdn.com/v4/letter/r/b19c9b/32.png) [@Richard\_Said](https://forum.inductiveautomation.com/u/Richard_Said)\
**Post date:** [September 30, 2022, 3:01pm UTC](https://forum.inductiveautomation.com/t/sending-email-via-onclick-button-script/64709/1 "2022-09-30T15:01:02Z")

</div>

I am trying to make a button onClick script in perspective where it uses system.net.sendEmail() 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.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/0/1012026485e11f5f6a2f00825323eb9de91d89bd.png)

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.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/8/489184025c35907fe78e5653f2f8a6249333e8ff.png)

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

---

<div class="post-metadata">

**Author:** ![YF129701](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/yf129701/32/60498_2.png) [@YF129701](https://forum.inductiveautomation.com/u/YF129701)\
**Post date:** [September 30, 2022, 3:15pm UTC](https://forum.inductiveautomation.com/t/sending-email-via-onclick-button-script/64709/2 "2022-09-30T15:15:05Z")

</div>

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](https://docs.inductiveautomation.com/display/DOC81/Email+Settings), which you can then reference from `sendEmail`, alarm notifications, report schedules, etc.

---

<div class="post-metadata">

**Author:** ![Richard\_Said](https://avatars.discourse-cdn.com/v4/letter/r/b19c9b/32.png) [@Richard\_Said](https://forum.inductiveautomation.com/u/Richard_Said)\
**Post date:** [September 30, 2022, 3:35pm UTC](https://forum.inductiveautomation.com/t/sending-email-via-onclick-button-script/64709/3 "2022-09-30T15:35:41Z")

</div>

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?

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/3/e347b33617db014ee44721561b61b71b3567e02d.png)

---

<div class="post-metadata">

**Author:** ![craigb](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@craigb](https://forum.inductiveautomation.com/u/craigb)\
**Post date:** [September 30, 2022, 4:30pm UTC](https://forum.inductiveautomation.com/t/sending-email-via-onclick-button-script/64709/4 "2022-09-30T16:30:01Z")

</div>

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.

```auto
smtp_server = 'AdminEmail' #Name of of the SMTP server in the gateway
EmailTo = 'email@email.com'
sender = "senders Email"
subject = "Your Subject"
body = "Info in body of email"
system.net.sendEmail(smtpProfile=smtp_server, fromAddr=sender, subject=subject, body=body, html=1, to=EmailTo)

```

---

<div class="post-metadata">

**Author:** ![stra](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/stra/32/23678_2.png) [@stra](https://forum.inductiveautomation.com/u/stra)\
**Post date:** [October 1, 2022, 7:09am UTC](https://forum.inductiveautomation.com/t/sending-email-via-onclick-button-script/64709/5 "2022-10-01T07:09:26Z")

</div>

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](https://youtu.be/g_j6ILT-X0k)) after you have the code/password you need to configure the smtpprofile on the gateway

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [October 1, 2022, 7:54am UTC](https://forum.inductiveautomation.com/t/sending-email-via-onclick-button-script/64709/6 "2022-10-01T07:54:36Z")

</div>

> [@Richard\_Said](#):
>
> .. since I use Gmail I wanted to do it through gmail.

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., `ie.dublin.eng.ignition04@myCompany.com`. 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.
