Source code for airflow.providers.smtp.operators.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.abcimportSequencefrompathlibimportPathfromtypingimportTYPE_CHECKING,Anyfromairflow.exceptionsimportAirflowExceptionfromairflow.modelsimportBaseOperatorfromairflow.providers.smtp.hooks.smtpimportSmtpHookifTYPE_CHECKING:try:fromairflow.sdk.definitions.contextimportContextexceptImportError:# TODO: Remove once provider drops support for Airflow 2fromairflow.utils.contextimportContext
[docs]classEmailOperator(BaseOperator):""" Sends an email. :param to: list of emails to send the email to. (templated) :param from_email: email to send from. (templated) :param subject: subject line for the email. (templated) :param html_content: content of the email, html markup is allowed. (templated) :param files: file names to attach in email (templated) :param cc: list of recipients to be added in CC field (templated) :param bcc: list of recipients to be added in BCC field (templated) :param mime_subtype: MIME sub content type :param mime_charset: character set parameter added to the Content-Type header. :param custom_headers: additional headers to add to the MIME message. """
[docs]defexecute(self,context:Context):withSmtpHook(smtp_conn_id=self.conn_id)assmtp_hook:fields_to_re_render=[]ifself.from_emailisNone:ifsmtp_hook.from_emailisNone:raiseAirflowException("You should provide `from_email` or define it in the connection.")self.from_email=smtp_hook.from_emailfields_to_re_render.append("from_email")ifself.subjectisNone:ifsmtp_hook.subject_templateisNone:raiseAirflowException("You should provide `subject` or define `subject_template` in the connection.")self.subject=self._read_template(smtp_hook.subject_template)fields_to_re_render.append("subject")ifself.html_contentisNone:ifsmtp_hook.html_content_templateisNone:raiseAirflowException("You should provide `html_content` or define `html_content_template` in the connection.")self.html_content=self._read_template(smtp_hook.html_content_template)fields_to_re_render.append("html_content")iffields_to_re_render:self._do_render_template_fields(self,fields_to_re_render,context,self.get_template_env(),set())returnsmtp_hook.send_email_smtp(to=self.to,subject=self.subject,html_content=self.html_content,from_email=self.from_email,files=self.files,cc=self.cc,bcc=self.bcc,mime_subtype=self.mime_subtype,mime_charset=self.mime_charset,conn_id=self.conn_id,custom_headers=self.custom_headers,)