Skip to content

Instantly share code, notes, and snippets.

@mholtzscher
Created February 27, 2019 19:22
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mholtzscher/9648cfd27769d1df6a6ed855fdd7bd7a to your computer and use it in GitHub Desktop.
Save mholtzscher/9648cfd27769d1df6a6ed855fdd7bd7a to your computer and use it in GitHub Desktop.
Example Email Operator Usage with Attachment
from airflow.models import DAG
from airflow.operators.email_operator import EmailOperator
from airflow.operators.python_operator import PythonOperator
from datetime import datetime
from tempfile import NamedTemporaryFile
dag = DAG(
"email_example",
description="Sample Email Example with File attachments",
schedule_interval="@daily",
start_date=datetime(2018, 12, 7),
catchup=False,
)
def build_email(**context):
with NamedTemporaryFile(mode='w+', suffix=".txt") as file:
file.write("Hello World")
email_op = EmailOperator(
task_id='send_email',
to="hello@example.com",
subject="Test Email Please Ignore",
html_content=None,
files=[file.name],
)
email_op.execute(context)
email_op_python = PythonOperator(
task_id="python_send_email", python_callable=build_email, provide_context=True, dag=dag
)
@touseefzaki
Copy link

when I try to use the EmailOperator, I am getting below error
ERROR - No module named 'email_backend = airflow'

May I know What I am missing in my implementation

@Welt0n
Copy link

Welt0n commented Mar 8, 2021

@touseefzaki
You probably need to config it in airflow.cfg, it is in your airflow_home directory

@srivallivagdevi
Copy link

What are the configuration changes that are to be done? No errors and no mail

@nandhan2047
Copy link

what is the alternate for this as EmailOperator is gone now.

@mmunarriz
Copy link

PendingDeprecationWarning: Fetching SMTP credentials from configuration variables will be deprecated in a future release. Please set credentials using a connection instead.

@novadejamesng
Copy link

In airflow.cfg, use this:
email_backend = airflow.utils.email.send_email_smtp
so that you can use this:
from airflow.operators.email_operator import EmailOperator

@manishkmbl0
Copy link

can we attache file in email from GCS any bucket

@sushma-bit
Copy link

How can we attach file present in other GCS bucket ( not in composer env bucket) in the email.

@PrashanthMHiremath
Copy link

How can I attach a csv file present in a s3 bucket to Airflow EmailOperator?? Is it possible ? or Is it like we can send a file from local temp folder ? Please help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment