-
Notifications
You must be signed in to change notification settings - Fork 1
/
email-local.py
46 lines (32 loc) · 1.34 KB
/
email-local.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from datetime import datetime
from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.python_operator import PythonOperator
from smtplib import SMTP
def print_hello():
return 'Hello world!'
def my_email_func():
smtp = SMTP()
smtp.set_debuglevel(10)
smtp.connect('smtp.example.com', 587)
smtp.login('smtp-username', 'smtp-password')
from_addr = "Sender Name <[email protected]>"
to_addr = "[email protected]"
subj = "hello"
date = datetime.now().strftime( "%d/%m/%Y %H:%M" )
message_text = "Hello\nThis is a mail from your server\n\nBye\n"
msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( from_addr, to_addr, subj, date, message_text )
smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()
return 'Email sent!'
default_args = {
'owner': 'abe',
'start_date':datetime(2024,5,26)
}
dag = DAG('send_email_test', description='SMTP Function DAG',
schedule_interval='* * * * *',
default_args = default_args, catchup=False)
dummy_operator = DummyOperator(task_id='dummy_task', retries=3, dag=dag)
hello_operator = PythonOperator(task_id='hello_task', python_callable=print_hello, dag=dag)
email = PythonOperator(task_id='email_task', python_callable=my_email_func, dag=dag)
email >> dummy_operator >> hello_operator