# Sending custom emails from cylc workflow

**URL:** <https://cylc.discourse.group/t/sending-custom-emails-from-cylc-workflow/665>\
**Category:** Cylc Support\
**Created:** [May 31, 2023, 9:57am UTC](https://cylc.discourse.group/t/sending-custom-emails-from-cylc-workflow/665 "2023-05-31T09:57:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfrost-mo](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/jfrost-mo/32/209_2.png) [@jfrost-mo](https://cylc.discourse.group/u/jfrost-mo)\
**Post date:** [May 31, 2023, 9:57am UTC](https://cylc.discourse.group/t/sending-custom-emails-from-cylc-workflow/665/1 "2023-05-31T09:57:10Z")

</div>

Currently I have a task near the end of my workflow that sends an email with a link to the output of the workflow (which is a webpage). However I’ve found that the way I’m doing it (a python script) is not very portable between sites.

Cylc already has the infrastricture to send emails for its task failure notifications. Is it possible to tap into this infrastructure to send custom content in an email?

I’ve looked at [message triggers](https://cylc.github.io/cylc-doc/latest/html/tutorial/furthertopics/message-triggers.html) and the [email event handler](https://cylc.github.io/cylc-doc/latest/html/user-guide/writing-workflows/runtime.html#built-in-email-event-handler), and while they provide a good way to send emails, they don’t seem to provide any way to put custom content in the message body.

---

<div class="post-metadata">

**Author:** ![wxtim](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/wxtim/32/151_2.png) [@wxtim](https://cylc.discourse.group/u/wxtim)\
**Post date:** [May 31, 2023, 11:49am UTC](https://cylc.discourse.group/t/sending-custom-emails-from-cylc-workflow/665/2 "2023-05-31T11:49:00Z")

</div>

After a bit of a look at what is possible I have to conclude that you can’t easily do this for a specific task.

What you can do is set it in `[scheduler][mail][footer]` and then turn on `[runtime][<namespace>]mail events = succeeded`.

---

<div class="post-metadata">

**Author:** ![hilary.j.oliver](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/hilary.j.oliver/32/4_2.png) [@hilary.j.oliver](https://cylc.discourse.group/u/hilary.j.oliver)\
**Post date:** [June 2, 2023, 1:34am UTC](https://cylc.discourse.group/t/sending-custom-emails-from-cylc-workflow/665/3 "2023-06-02T01:34:17Z")

</div>

I actually thought Cylc event emails were supposed to include the associated task message, if there is one. But that appears not to be the case. Some investigation required … not sure if we deliberately or accidentally dropped that somewhere along the way.

[Update] Task messages are available to event handlers in general, but they are ignored by the built-in mail event handler. We should be able to fix that …

> <https://github.com/cylc/cylc-flow/issues/5566>
>
> REF: https://cylc.discourse.group/t/sending-custom-emails-from-cylc-workflow/665…
> 
> \`\`\`ini
> \[scheduling\]
> \[\[graph\]\]
> R1 = "foo"
> \[runtime\]
> \[\[foo\]\]
> script = """
> ON\_THE\_FLY=$(date)
> cylc message WARNING:"My birthday is ${ON\_THE\_FLY}."
> """
> \[\[\[events\]\]\]
> mail events = warning
> \`\`\`
> 
> Running this results in an email to the user, for the warning event, but it does not include the associated task message.
> 
> The only way to get custom content into the email seems to be \`\[scheduler\]\[mail\]footer\` which allows templating of workflow-level variables (so not task-level \`%(message)s\` etc.
> 
> Fixing this is probably complicated by event-batching for emails.

---

<div class="post-metadata">

**Author:** ![jfrost-mo](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/jfrost-mo/32/209_2.png) [@jfrost-mo](https://cylc.discourse.group/u/jfrost-mo)\
**Post date:** [June 2, 2023, 7:30am UTC](https://cylc.discourse.group/t/sending-custom-emails-from-cylc-workflow/665/4 "2023-06-02T07:30:46Z")

</div>

That sounds ideal. For now I am relying on directly using the `mail` command, which I think cylc itself uses. It would be nicer to hook more directly into the capabilities of cylc however.

---

<div class="post-metadata">

**Author:** ![hilary.j.oliver](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/hilary.j.oliver/32/4_2.png) [@hilary.j.oliver](https://cylc.discourse.group/u/hilary.j.oliver)\
**Post date:** [June 2, 2023, 8:23am UTC](https://cylc.discourse.group/t/sending-custom-emails-from-cylc-workflow/665/5 "2023-06-02T08:23:50Z")

</div>

Yep, in the meantime you can roll your own event handler script, which takes the task message and sends the email itself (which it sounds as if you have done already!).
