Source code for airflow.providers.smtp.notifications.smtp
# Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.from__future__importannotationsfromcollections.abcimportIterablefromfunctoolsimportcached_propertyfrompathlibimportPathfromtypingimportAnyfromairflow.providers.common.compat.notifierimportBaseNotifierfromairflow.providers.smtp.hooks.smtpimportSmtpHook
[docs]classSmtpNotifier(BaseNotifier):""" SMTP Notifier. Accepts keyword arguments. The only required arguments are `from_email` and `to`. Examples: .. code-block:: python EmptyOperator(task_id="task", on_failure_callback=SmtpNotifier(from_email=None, to="my@mail.com")) EmptyOperator( task_id="task", on_failure_callback=SmtpNotifier( from_email="myemail@myemail.com", to="myemail@myemail.com", subject="Task {{ ti.task_id }} failed", ), ) You can define a default template for subject and html_content in the SMTP connection configuration. :param smtp_conn_id: The :ref:`smtp connection id <howto/connection:smtp>` that contains the information used to authenticate the client. """
[docs]defnotify(self,context):"""Send a email via smtp server."""fields_to_re_render=[]ifself.from_emailisNone:ifself.hook.from_emailisnotNone:self.from_email=self.hook.from_emailelse:raiseValueError("You should provide `from_email` or define it in the connection")fields_to_re_render.append("from_email")ifself.subjectisNone:smtp_default_templated_subject_path:strifself.hook.subject_template:smtp_default_templated_subject_path=self.hook.subject_templateelse:smtp_default_templated_subject_path=(Path(__file__).parent/"templates"/"email_subject.jinja2").as_posix()self.subject=self._read_template(smtp_default_templated_subject_path)fields_to_re_render.append("subject")ifself.html_contentisNone:smtp_default_templated_html_content_path:strifself.hook.html_content_template:smtp_default_templated_html_content_path=self.hook.html_content_templateelse:smtp_default_templated_html_content_path=(Path(__file__).parent/"templates"/"email.html").as_posix()self.html_content=self._read_template(smtp_default_templated_html_content_path)fields_to_re_render.append("html_content")iffields_to_re_render:jinja_env=self.get_template_env(dag=context["dag"])self._do_render_template_fields(self,fields_to_re_render,context,jinja_env,set())withself.hookassmtp:smtp.send_email_smtp(smtp_conn_id=self.smtp_conn_id,from_email=self.from_email,to=self.to,subject=self.subject,html_content=self.html_content,files=self.files,cc=self.cc,bcc=self.bcc,mime_subtype=self.mime_subtype,mime_charset=self.mime_charset,custom_headers=self.custom_headers,)