我正在尝试使用本地 docker 设置从我的气流发送一封简单的电子邮件。
我的爸爸是这样的:
default_args = {
"owner": "Airflow",
"start_date": pendulum.today('UTC').add(days=-1),
"depends_on_past": False,
"email_on_failure": True,
"email_on_retry": False,
"email": "[email protected]",
"retries": 1,
"retry_delay": timedelta(minutes=5)
}
with DAG(dag_id="send_emails", schedule="@daily", default_args=default_args, catchup=False) as dag:
sending_email_notification = EmailOperator(
task_id="sending_email",
to="[email protected]",
subject=f"New email",
html_content=f"""
<h3>New Data is available</h3>
"""
)
sending_email_notification
我已经在airflow.cfg中设置了
[smtp]
部分,它与我的docker-compose.yaml
位于同一文件夹中,如下所示:
[smtp]
smtp_host = smtp-mail.outlook.com
smtp_starttls = True
smtp_ssl = False
smtp_user = [email protected]
smtp_password = mypassword
smtp_port = 587
smtp_mail_from = [email protected]
我也尝试过使用应用程序密码从 gmail 执行此操作
[smtp]
smtp_host = smtp.gmail.com
smtp_starttls = True
smtp_ssl = False
smtp_user = [email protected]
smtp_password = appssecret
smtp_port = 587
smtp_mail_from = [email protected]
smtp_timeout = 30
smtp_retry_limit = 5
但是 DAG 每次都会失败,并出现以下错误和回溯:
[2024-01-31, 21:24:42 UTC] {taskinstance.py:1400} INFO - Marking task as FAILED. dag_id=send_emails, task_id=sending_email, execution_date=20240131T211339, start_date=20240131T212442, end_date=20240131T212442
[2024-01-31, 21:24:42 UTC] {configuration.py:1050} WARNING - section/key [smtp/smtp_user] not found in config
[2024-01-31, 21:24:42 UTC] {email.py:270} INFO - Email alerting: attempt 1
[2024-01-31, 21:24:42 UTC] {configuration.py:1050} WARNING - section/key
[smtp/smtp_user] not found in config
[2024-01-31, 21:24:42 UTC] {email.py:270} INFO - Email alerting: attempt 1
[2024-01-31, 21:24:42 UTC] {taskinstance.py:2007} ERROR - Failed to send email to: [email protected]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/airflow/.local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 2005, in handle_failure
self.email_alert(error, task)
File "/home/airflow/.local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 2422, in email_alert
send_email(task.email, subject, html_content_err)
File "/home/airflow/.local/lib/python3.8/site-packages/airflow/utils/email.py", line 80, in send_email
return backend(
File "/home/airflow/.local/lib/python3.8/site-packages/airflow/utils/email.py", line 154, in send_email_smtp
send_mime_email(e_from=mail_from, e_to=recipients, mime_msg=msg, conn_id=conn_id, dryrun=dryrun)
File "/home/airflow/.local/lib/python3.8/site-packages/airflow/utils/email.py", line 272, in send_mime_email
smtp_conn = _get_smtp_connection(smtp_host, smtp_port, smtp_timeout, smtp_ssl)
File "/home/airflow/.local/lib/python3.8/site-packages/airflow/utils/email.py", line 316, in _get_smtp_connection
return smtplib.SMTP(host=host, port=port, timeout=timeout)
File "/usr/local/lib/python3.8/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python3.8/smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/lib/python3.8/smtplib.py", line 310, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/local/lib/python3.8/socket.py", line 808, in create_connection
raise err
File "/usr/local/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
OSError: [Errno 99] Cannot assign requested address
[2024-01-31, 21:24:42 UTC] {standard_task_runner.py:104} ERROR - Failed to execute job 105 for task sending_email ([Errno 99] Cannot assign requested address; 557)
[2024-01-31, 21:24:42 UTC] {local_task_job_runner.py:228} INFO - Task exited with return code 1
[2024-01-31, 21:24:42 UTC] {taskinstance.py:2778} INFO - 0 downstream tasks scheduled from follow-on schedule check
令我印象深刻的部分是警告 - 在配置中找不到部分/键 [smtp/smtp_user],但我对这里发生的情况有点迷失。如果有人既能帮助解决这个问题,又能解释为什么会发生这种情况,那将是为了未来。
AIRFLOW_SMTP_SMTP_HOST: 'smtp.freesmtpservers.com' AIRFLOW_SMTP_SMTP_PORT:'25' AIRFLOW_SMTP_SMTP_STARTTLS:“假”
电子邮件的 Dag 任务代码:
task_send_email = EmailOperator(
task_id='task_send_email',
to='[email protected]',
subject='test email',
html_content='Testing email',
)
AIRFLOW__SMTP__SMTP_HOST: 'smtp.office365.com' AIRFLOW__SMTP__SMTP_PORT:'587' AIRFLOW__SMTP__SMTP_STARTTLS:'真' AIRFLOW__SMTP__SMTP_SSL:“假” AIRFLOW__SMTP__SMTP_MAIL_FROM:'[电子邮件受保护]'
task_send_email = EmailOperator(
task_id='task_send_email',
conn_id='smtp_default',
to=['[email protected]','[email protected]'],
cc='[email protected]',
subject='testing airflow email using email connection type test',
html_content='Testing email from airflow DAG',
do_xcom_push=True,
on_success_callback =success,
on_failure_callback =failure
)
不确定 OSError: [Errno 99] 还有一些其他 stackoverflow 线程可以帮助您。