1

I am trying to send an email alert using the PythonOperator with email_callback function as the python_callable. I have an Xcom which is in the form :

(('FileName1', 'OrgName1'), ('FileName2', 'OrgName2'))

I want to print the values in the HTML content in the email alert like this:

Files Processed Successfully:
OrgName1 FileName1
OrgName2 FileName2

I know that I have to use a loop and to use a loop I have to use tag inside HTML content. I am able to get all the text outside the <script> tag but whenever I output something from it like document.write("Hello"), this just doesn't print anything in the email.

The code:

def email_callback(**kwargs):
    task_instance = kwargs['task_instance']
    body = """HELLO<script>document.write("Hello")</script>"""
    send_email(
        to=[
            'eamil.ids'
        ],
        subject='Batch Job Status - ' + now.strftime("%m/%d/%Y"),
        html_content=body
    )

AllTaskSuccess = PythonOperator (
    dag=dag,
    python_callable=email_callback,
    trigger_rule=TriggerRule.ALL_SUCCESS,
    provide_context=True,
    task_id="AllTaskSuccess")

Am I missing something here?

user4157124
  • 2,809
  • 13
  • 27
  • 42
Saruman
  • 11
  • 1
  • Does this answer your question? [Is JavaScript supported in an email message?](https://stackoverflow.com/questions/3054315/is-javascript-supported-in-an-email-message) –  Sep 10 '20 at 22:19
  • Yes, i kind of got the point. But if i want to use for loop, then what should i do because my XCom has a list of tuples – Saruman Sep 11 '20 at 01:01
  • The way to achieve this is to a) run code (loops, whatever) that creates the final HTML of the email b) send that HTML as email. I'm not going to look into Airflow or Xcom because it seems completely irrelevant. As long as you use whatever means to compose the complete, static HTML first, it will work just fine. –  Sep 11 '20 at 06:31

0 Answers0