email.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. import collections.abc
  20. import logging
  21. import os
  22. import smtplib
  23. import ssl
  24. import warnings
  25. from email.mime.application import MIMEApplication
  26. from email.mime.multipart import MIMEMultipart
  27. from email.mime.text import MIMEText
  28. from email.utils import formatdate
  29. from typing import Any, Iterable
  30. import re2
  31. from airflow.configuration import conf
  32. from airflow.exceptions import AirflowConfigException, AirflowException, RemovedInAirflow3Warning
  33. log = logging.getLogger(__name__)
  34. def send_email(
  35. to: list[str] | Iterable[str],
  36. subject: str,
  37. html_content: str,
  38. files: list[str] | None = None,
  39. dryrun: bool = False,
  40. cc: str | Iterable[str] | None = None,
  41. bcc: str | Iterable[str] | None = None,
  42. mime_subtype: str = "mixed",
  43. mime_charset: str = "utf-8",
  44. conn_id: str | None = None,
  45. custom_headers: dict[str, Any] | None = None,
  46. **kwargs,
  47. ) -> None:
  48. """
  49. Send an email using the backend specified in the *EMAIL_BACKEND* configuration option.
  50. :param to: A list or iterable of email addresses to send the email to.
  51. :param subject: The subject of the email.
  52. :param html_content: The content of the email in HTML format.
  53. :param files: A list of paths to files to attach to the email.
  54. :param dryrun: If *True*, the email will not actually be sent. Default: *False*.
  55. :param cc: A string or iterable of strings containing email addresses to send a copy of the email to.
  56. :param bcc: A string or iterable of strings containing email addresses to send a
  57. blind carbon copy of the email to.
  58. :param mime_subtype: The subtype of the MIME message. Default: "mixed".
  59. :param mime_charset: The charset of the email. Default: "utf-8".
  60. :param conn_id: The connection ID to use for the backend. If not provided, the default connection
  61. specified in the *EMAIL_CONN_ID* configuration option will be used.
  62. :param custom_headers: A dictionary of additional headers to add to the MIME message.
  63. No validations are run on these values, and they should be able to be encoded.
  64. :param kwargs: Additional keyword arguments to pass to the backend.
  65. """
  66. backend = conf.getimport("email", "EMAIL_BACKEND")
  67. backend_conn_id = conn_id or conf.get("email", "EMAIL_CONN_ID")
  68. from_email = conf.get("email", "from_email", fallback=None)
  69. to_list = get_email_address_list(to)
  70. to_comma_separated = ", ".join(to_list)
  71. return backend(
  72. to_comma_separated,
  73. subject,
  74. html_content,
  75. files=files,
  76. dryrun=dryrun,
  77. cc=cc,
  78. bcc=bcc,
  79. mime_subtype=mime_subtype,
  80. mime_charset=mime_charset,
  81. conn_id=backend_conn_id,
  82. from_email=from_email,
  83. custom_headers=custom_headers,
  84. **kwargs,
  85. )
  86. def send_email_smtp(
  87. to: str | Iterable[str],
  88. subject: str,
  89. html_content: str,
  90. files: list[str] | None = None,
  91. dryrun: bool = False,
  92. cc: str | Iterable[str] | None = None,
  93. bcc: str | Iterable[str] | None = None,
  94. mime_subtype: str = "mixed",
  95. mime_charset: str = "utf-8",
  96. conn_id: str = "smtp_default",
  97. from_email: str | None = None,
  98. custom_headers: dict[str, Any] | None = None,
  99. **kwargs,
  100. ) -> None:
  101. """
  102. Send an email with html content.
  103. :param to: Recipient email address or list of addresses.
  104. :param subject: Email subject.
  105. :param html_content: Email body in HTML format.
  106. :param files: List of file paths to attach to the email.
  107. :param dryrun: If True, the email will not be sent, but all other actions will be performed.
  108. :param cc: Carbon copy recipient email address or list of addresses.
  109. :param bcc: Blind carbon copy recipient email address or list of addresses.
  110. :param mime_subtype: MIME subtype of the email.
  111. :param mime_charset: MIME charset of the email.
  112. :param conn_id: Connection ID of the SMTP server.
  113. :param from_email: Sender email address.
  114. :param custom_headers: Dictionary of custom headers to include in the email.
  115. :param kwargs: Additional keyword arguments.
  116. >>> send_email("test@example.com", "foo", "<b>Foo</b> bar", ["/dev/null"], dryrun=True)
  117. """
  118. smtp_mail_from = conf.get("smtp", "SMTP_MAIL_FROM")
  119. if smtp_mail_from is not None:
  120. mail_from = smtp_mail_from
  121. else:
  122. if from_email is None:
  123. raise ValueError(
  124. "You should set from email - either by smtp/smtp_mail_from config or `from_email` parameter"
  125. )
  126. mail_from = from_email
  127. msg, recipients = build_mime_message(
  128. mail_from=mail_from,
  129. to=to,
  130. subject=subject,
  131. html_content=html_content,
  132. files=files,
  133. cc=cc,
  134. bcc=bcc,
  135. mime_subtype=mime_subtype,
  136. mime_charset=mime_charset,
  137. custom_headers=custom_headers,
  138. )
  139. send_mime_email(e_from=mail_from, e_to=recipients, mime_msg=msg, conn_id=conn_id, dryrun=dryrun)
  140. def build_mime_message(
  141. mail_from: str | None,
  142. to: str | Iterable[str],
  143. subject: str,
  144. html_content: str,
  145. files: list[str] | None = None,
  146. cc: str | Iterable[str] | None = None,
  147. bcc: str | Iterable[str] | None = None,
  148. mime_subtype: str = "mixed",
  149. mime_charset: str = "utf-8",
  150. custom_headers: dict[str, Any] | None = None,
  151. ) -> tuple[MIMEMultipart, list[str]]:
  152. """
  153. Build a MIME message that can be used to send an email and returns a full list of recipients.
  154. :param mail_from: Email address to set as the email's "From" field.
  155. :param to: A string or iterable of strings containing email addresses to set as the email's "To" field.
  156. :param subject: The subject of the email.
  157. :param html_content: The content of the email in HTML format.
  158. :param files: A list of paths to files to be attached to the email.
  159. :param cc: A string or iterable of strings containing email addresses to set as the email's "CC" field.
  160. :param bcc: A string or iterable of strings containing email addresses to set as the email's "BCC" field.
  161. :param mime_subtype: The subtype of the MIME message. Default: "mixed".
  162. :param mime_charset: The charset of the email. Default: "utf-8".
  163. :param custom_headers: Additional headers to add to the MIME message. No validations are run on these
  164. values, and they should be able to be encoded.
  165. :return: A tuple containing the email as a MIMEMultipart object and a list of recipient email addresses.
  166. """
  167. to = get_email_address_list(to)
  168. msg = MIMEMultipart(mime_subtype)
  169. msg["Subject"] = subject
  170. if mail_from:
  171. msg["From"] = mail_from
  172. msg["To"] = ", ".join(to)
  173. recipients = to
  174. if cc:
  175. cc = get_email_address_list(cc)
  176. msg["CC"] = ", ".join(cc)
  177. recipients += cc
  178. if bcc:
  179. # don't add bcc in header
  180. bcc = get_email_address_list(bcc)
  181. recipients += bcc
  182. msg["Date"] = formatdate(localtime=True)
  183. mime_text = MIMEText(html_content, "html", mime_charset)
  184. msg.attach(mime_text)
  185. for fname in files or []:
  186. basename = os.path.basename(fname)
  187. with open(fname, "rb") as file:
  188. part = MIMEApplication(file.read(), Name=basename)
  189. part["Content-Disposition"] = f'attachment; filename="{basename}"'
  190. part["Content-ID"] = f"<{basename}>"
  191. msg.attach(part)
  192. if custom_headers:
  193. for header_key, header_value in custom_headers.items():
  194. msg[header_key] = header_value
  195. return msg, recipients
  196. def send_mime_email(
  197. e_from: str,
  198. e_to: str | list[str],
  199. mime_msg: MIMEMultipart,
  200. conn_id: str = "smtp_default",
  201. dryrun: bool = False,
  202. ) -> None:
  203. """
  204. Send a MIME email.
  205. :param e_from: The email address of the sender.
  206. :param e_to: The email address or a list of email addresses of the recipient(s).
  207. :param mime_msg: The MIME message to send.
  208. :param conn_id: The ID of the SMTP connection to use.
  209. :param dryrun: If True, the email will not be sent, but a log message will be generated.
  210. """
  211. smtp_host = conf.get_mandatory_value("smtp", "SMTP_HOST")
  212. smtp_port = conf.getint("smtp", "SMTP_PORT")
  213. smtp_starttls = conf.getboolean("smtp", "SMTP_STARTTLS")
  214. smtp_ssl = conf.getboolean("smtp", "SMTP_SSL")
  215. smtp_retry_limit = conf.getint("smtp", "SMTP_RETRY_LIMIT")
  216. smtp_timeout = conf.getint("smtp", "SMTP_TIMEOUT")
  217. smtp_user = None
  218. smtp_password = None
  219. if conn_id is not None:
  220. try:
  221. from airflow.hooks.base import BaseHook
  222. airflow_conn = BaseHook.get_connection(conn_id)
  223. smtp_user = airflow_conn.login
  224. smtp_password = airflow_conn.password
  225. except AirflowException:
  226. pass
  227. if smtp_user is None or smtp_password is None:
  228. warnings.warn(
  229. "Fetching SMTP credentials from configuration variables will be deprecated in a future "
  230. "release. Please set credentials using a connection instead.",
  231. RemovedInAirflow3Warning,
  232. stacklevel=2,
  233. )
  234. try:
  235. smtp_user = conf.get("smtp", "SMTP_USER")
  236. smtp_password = conf.get("smtp", "SMTP_PASSWORD")
  237. except AirflowConfigException:
  238. log.debug("No user/password found for SMTP, so logging in with no authentication.")
  239. if not dryrun:
  240. for attempt in range(1, smtp_retry_limit + 1):
  241. log.info("Email alerting: attempt %s", str(attempt))
  242. try:
  243. smtp_conn = _get_smtp_connection(smtp_host, smtp_port, smtp_timeout, smtp_ssl)
  244. except smtplib.SMTPServerDisconnected:
  245. if attempt == smtp_retry_limit:
  246. raise
  247. else:
  248. if smtp_starttls:
  249. smtp_conn.starttls()
  250. if smtp_user and smtp_password:
  251. smtp_conn.login(smtp_user, smtp_password)
  252. log.info("Sent an alert email to %s", e_to)
  253. smtp_conn.sendmail(e_from, e_to, mime_msg.as_string())
  254. smtp_conn.quit()
  255. break
  256. def get_email_address_list(addresses: str | Iterable[str]) -> list[str]:
  257. """
  258. Return a list of email addresses from the provided input.
  259. :param addresses: A string or iterable of strings containing email addresses.
  260. :return: A list of email addresses.
  261. :raises TypeError: If the input is not a string or iterable of strings.
  262. """
  263. if isinstance(addresses, str):
  264. return _get_email_list_from_str(addresses)
  265. elif isinstance(addresses, collections.abc.Iterable):
  266. if not all(isinstance(item, str) for item in addresses):
  267. raise TypeError("The items in your iterable must be strings.")
  268. return list(addresses)
  269. else:
  270. raise TypeError(f"Unexpected argument type: Received '{type(addresses).__name__}'.")
  271. def _get_smtp_connection(host: str, port: int, timeout: int, with_ssl: bool) -> smtplib.SMTP:
  272. """
  273. Return an SMTP connection to the specified host and port, with optional SSL encryption.
  274. :param host: The hostname or IP address of the SMTP server.
  275. :param port: The port number to connect to on the SMTP server.
  276. :param timeout: The timeout in seconds for the connection.
  277. :param with_ssl: Whether to use SSL encryption for the connection.
  278. :return: An SMTP connection to the specified host and port.
  279. """
  280. if not with_ssl:
  281. return smtplib.SMTP(host=host, port=port, timeout=timeout)
  282. else:
  283. ssl_context_string = conf.get("email", "SSL_CONTEXT")
  284. if ssl_context_string == "default":
  285. ssl_context = ssl.create_default_context()
  286. elif ssl_context_string == "none":
  287. ssl_context = None
  288. else:
  289. raise RuntimeError(
  290. f"The email.ssl_context configuration variable must "
  291. f"be set to 'default' or 'none' and is '{ssl_context_string}."
  292. )
  293. return smtplib.SMTP_SSL(host=host, port=port, timeout=timeout, context=ssl_context)
  294. def _get_email_list_from_str(addresses: str) -> list[str]:
  295. """
  296. Extract a list of email addresses from a string.
  297. The string can contain multiple email addresses separated
  298. by any of the following delimiters: ',' or ';'.
  299. :param addresses: A string containing one or more email addresses.
  300. :return: A list of email addresses.
  301. """
  302. pattern = r"\s*[,;]\s*"
  303. return re2.split(pattern, addresses)