# Problem adding variable in email body

**URL:** <https://forum.inductiveautomation.com/t/problem-adding-variable-in-email-body/45846>\
**Category:** Ignition\
**Created:** [April 21, 2021, 6:37am UTC](https://forum.inductiveautomation.com/t/problem-adding-variable-in-email-body/45846 "2021-04-21T06:37:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aiasa21](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Aiasa21](https://forum.inductiveautomation.com/u/Aiasa21)\
**Post date:** [April 21, 2021, 6:37am UTC](https://forum.inductiveautomation.com/t/problem-adding-variable-in-email-body/45846/1 "2021-04-21T06:37:55Z")

</div>

Good morning,

I’m able to send my report by email when I have a tag change, it work perfectly.  
What I’m trying to do is to add a variable in the body, in this case the email is not sent.

```auto
value = newValue.value

if value == 1:

    body = "TagTestValue: " #If I just add this in the body, it works
    # body = "TagTestValue: " + system.tag.read("[default]TagTest").value # With this line, adding the tag.read in the body, the email is not sent and I have none error in the console

	system.report.executeAndDistribute(path="MyPath", project="MyProject", action= "email", 
	actionSettings = {"to":["myEmail"], "smtpServerName":"EmailSetting", "from":"myCompany", "subject":"MySubject", "body":body})

```

Where’s the mistake?

Thank you very much.

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [April 21, 2021, 8:23am UTC](https://forum.inductiveautomation.com/t/problem-adding-variable-in-email-body/45846/2 "2021-04-21T08:23:01Z")

</div>

```auto
value = newValue.value

if value == 1:
    variable = system.tag.read("[default]TagTest")
    variableQV = variable.value
    body = "TagTestValue: " +str(variableQV)
    

	system.report.executeAndDistribute(path="MyPath", project="MyProject", action= "email", 
	actionSettings = {"to":["myEmail"], "smtpServerName":"EmailSetting", "from":"myCompany", "subject":"MySubject", "body":body})

```

---

<div class="post-metadata">

**Author:** ![Aiasa21](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Aiasa21](https://forum.inductiveautomation.com/u/Aiasa21)\
**Post date:** [April 22, 2021, 7:25am UTC](https://forum.inductiveautomation.com/t/problem-adding-variable-in-email-body/45846/3 "2021-04-22T07:25:25Z")

</div>

Thanks for your help Matrix\_Engeneering. It works great.

One question about this: I see that if I try to send a variable DateTime and it has a correct value, the email is sent. If the variable DateTime has not a correct value (for example is “null”), the email is not sent.

Is there a way to check if the variable has a correct value, so I can manage the email body in the properly way?

Thank you very much

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [April 22, 2021, 9:40pm UTC](https://forum.inductiveautomation.com/t/problem-adding-variable-in-email-body/45846/4 "2021-04-22T21:40:02Z")

</div>

Where are you getting DateTime from, a tag?

You can try something like this maybe changing -1 to “None”

```auto
x = system.tag.read("[System]Gateway/CurrentDateTime")

print x

QV = x.value

print QV

if QV != -1:
	print "send email"
	#Put Your Send Email Code Here
	

```

---

<div class="post-metadata">

**Author:** ![Aiasa21](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Aiasa21](https://forum.inductiveautomation.com/u/Aiasa21)\
**Post date:** [April 27, 2021, 6:16am UTC](https://forum.inductiveautomation.com/t/problem-adding-variable-in-email-body/45846/5 "2021-04-27T06:16:03Z")

</div>

Hi Matrix\_Engineering and thanks for your reply.

I try to explain what I need to do, sorry for the english: I have a report that is sent when a batch is finished, and in this report I have a StartTime and an EndTime tags that I fill when this batch starts and ends.

At the end of the batch I send an email with process data and trends, and in the body I add the StartTime and EndTime with a specific format

> startBatch = system.tag.read([default]myPath).value  
> startBatch = system.date.format(startBatch , dd MMMM yyyy HH:mm)

If the DateTime have correct values, the email is sent, but I've seen that if one or both the DateTime tags have no value ("Null"), the email is not sent, so I would check if the DateTime is valid before to add it in the body, in the case is not valid I will manage it in another way.

I try this

> if QV != -1:  
> if QV != "None":  
> if QV != "Null":

but in all the cases the email is not sent.

Thanks again for your help.

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [April 27, 2021, 9:30am UTC](https://forum.inductiveautomation.com/t/problem-adding-variable-in-email-body/45846/6 "2021-04-27T09:30:02Z")

</div>

Yes because you don’t have QV in your script.

I would change so your variables are unique (for the formatted):

```auto
startBatch = system.tag.read([default]myPath).value
startBatchF = system.date.format(startBatch , dd MMMM yyyy HH:mm)

```

Then when you go to add the variable to email, make sure to typecast as string:

```auto
str(startBatchF)

```

When checking != None etc, you don’t need to add quotes around None and Null

---

<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:** [April 27, 2021, 2:22pm UTC](https://forum.inductiveautomation.com/t/problem-adding-variable-in-email-body/45846/7 "2021-04-27T14:22:20Z")

</div>

`None` is special in Python & Jython. You are supposed to test for it with the `is` keyword. Or `is not`. Like so:

```auto
if startBatch is None:
    # log that the batch interval is invalid...

```

---

<div class="post-metadata">

**Author:** ![Aiasa21](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Aiasa21](https://forum.inductiveautomation.com/u/Aiasa21)\
**Post date:** [April 28, 2021, 4:47pm UTC](https://forum.inductiveautomation.com/t/problem-adding-variable-in-email-body/45846/8 "2021-04-28T16:47:50Z")

</div>

Thanks everybody for the help.

> [@pturmel](#):
>
> `None` is special in Python & Jython. You are supposed to test for it with the `is` keyword. Or `is not`. Like so:
> 
> ```auto
> if startBatch is None:
> # log that the batch interval is invalid...
> 
> ```

I change the script as you suggest and it works perfectly!
