# Is there a way to automatically attach a document to an email?

**URL:** <https://forum.inductiveautomation.com/t/is-there-a-way-to-automatically-attach-a-document-to-an-email/23043>\
**Category:** Ignition\
**Created:** [March 4, 2019, 8:14pm UTC](https://forum.inductiveautomation.com/t/is-there-a-way-to-automatically-attach-a-document-to-an-email/23043 "2019-03-04T20:14:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nelpers](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nelpers](https://forum.inductiveautomation.com/u/nelpers)\
**Post date:** [March 4, 2019, 8:14pm UTC](https://forum.inductiveautomation.com/t/is-there-a-way-to-automatically-attach-a-document-to-an-email/23043/1 "2019-03-04T20:14:55Z")

</div>

I am looking for a way to automatically attach a word document to an email that will be sent when triggered. Right now I have it set up on a button trigger for testing purposes. I have used the format that is in the inductive automation documents. I have looked into attachmentData and attachmentNames, but I am still learning so if those are used I am not sure how to use them effectively.

```auto
filePath = system.net.openFile('docx','C:\Test_doc.docx')
if filePath != None:
   fileName = filePath.split("\\")[-1]
   fileData = fpmi.file.readFileAsBytes(filePath)
   smtp = "server.company.local"
   sender = "sender@companyname.com"
   subject = "Test Email From Ignition"
   body = "This is a test email from ignition."
   recipients = ["myname@companyname.com"]
   system.net.sendEmail(smtp, sender, subject, body, 0, recipients, [fileName], [fileData])

```

If I need to use the other methods mentioned in the documents I can do that as well, as I also have other buttons setup with those as well.`Preformatted text`

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 4, 2019, 9:03pm UTC](https://forum.inductiveautomation.com/t/is-there-a-way-to-automatically-attach-a-document-to-an-email/23043/2 "2019-03-04T21:03:30Z")

</div>

In your script, you have a spurious `attachmentData` stuck between subject and body. Try taking that out.

---

<div class="post-metadata">

**Author:** ![nelpers](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nelpers](https://forum.inductiveautomation.com/u/nelpers)\
**Post date:** [March 4, 2019, 9:09pm UTC](https://forum.inductiveautomation.com/t/is-there-a-way-to-automatically-attach-a-document-to-an-email/23043/3 "2019-03-04T21:09:27Z")

</div>

Ah, yes, thank you, I missed that editing that out before posting. When trying different things.

---

<div class="post-metadata">

**Author:** ![ajdewhirst1](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@ajdewhirst1](https://forum.inductiveautomation.com/u/ajdewhirst1)\
**Post date:** [March 4, 2019, 9:34pm UTC](https://forum.inductiveautomation.com/t/is-there-a-way-to-automatically-attach-a-document-to-an-email/23043/4 "2019-03-04T21:34:31Z")

</div>

In the first line, “system.net.openFile” should be “system.file.openFile” ?

---

<div class="post-metadata">

**Author:** ![GreggCavazos](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@GreggCavazos](https://forum.inductiveautomation.com/u/GreggCavazos)\
**Post date:** [August 26, 2019, 2:45pm UTC](https://forum.inductiveautomation.com/t/is-there-a-way-to-automatically-attach-a-document-to-an-email/23043/5 "2019-08-26T14:45:15Z")

</div>

It worked for me like below

I don’t use the system.net.openFile because I already have the filepath.  
To read the file I use system.file.readFileAsBytes(filepath), not sure what library is needed to use the “fpmi.file.readFileAsBytes(filePath)”

```auto
if filePath != None:
   fileName = filePath.split("\\")[-1]
   fileData = system.file.readFileAsBytes(filePath)
   smtp = "server.company.local"
   sender = "sender@companyname.com"
   subject = "Test Email From Ignition"
   body = "This is a test email from ignition."
   recipients = ["myname@companyname.com"]
   system.net.sendEmail(smtp, sender, subject, body, 0, recipients, [fileName], [fileData])

```
