email.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. from __future__ import annotations
  19. from typing import TYPE_CHECKING, Any, Sequence
  20. from airflow.models.baseoperator import BaseOperator
  21. from airflow.utils.email import send_email
  22. if TYPE_CHECKING:
  23. from airflow.utils.context import Context
  24. class EmailOperator(BaseOperator):
  25. """
  26. Sends an email.
  27. :param to: list of emails to send the email to. (templated)
  28. :param subject: subject line for the email. (templated)
  29. :param html_content: content of the email, html markup
  30. is allowed. (templated)
  31. :param files: file names to attach in email (templated)
  32. :param cc: list of recipients to be added in CC field
  33. :param bcc: list of recipients to be added in BCC field
  34. :param mime_subtype: MIME sub content type
  35. :param mime_charset: character set parameter added to the Content-Type
  36. header.
  37. :param custom_headers: additional headers to add to the MIME message.
  38. """
  39. template_fields: Sequence[str] = ("to", "subject", "html_content", "files")
  40. template_fields_renderers = {"html_content": "html"}
  41. template_ext: Sequence[str] = (".html",)
  42. ui_color = "#e6faf9"
  43. def __init__(
  44. self,
  45. *,
  46. to: list[str] | str,
  47. subject: str,
  48. html_content: str,
  49. files: list | None = None,
  50. cc: list[str] | str | None = None,
  51. bcc: list[str] | str | None = None,
  52. mime_subtype: str = "mixed",
  53. mime_charset: str = "utf-8",
  54. conn_id: str | None = None,
  55. custom_headers: dict[str, Any] | None = None,
  56. **kwargs,
  57. ) -> None:
  58. super().__init__(**kwargs)
  59. self.to = to
  60. self.subject = subject
  61. self.html_content = html_content
  62. self.files = files or []
  63. self.cc = cc
  64. self.bcc = bcc
  65. self.mime_subtype = mime_subtype
  66. self.mime_charset = mime_charset
  67. self.conn_id = conn_id
  68. self.custom_headers = custom_headers
  69. def execute(self, context: Context):
  70. send_email(
  71. self.to,
  72. self.subject,
  73. self.html_content,
  74. files=self.files,
  75. cc=self.cc,
  76. bcc=self.bcc,
  77. mime_subtype=self.mime_subtype,
  78. mime_charset=self.mime_charset,
  79. conn_id=self.conn_id,
  80. custom_headers=self.custom_headers,
  81. )