sender.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. from copy import copy
  2. from email.message import EmailMessage
  3. from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union
  4. import jinja2
  5. from redmail.email.attachment import Attachments
  6. from redmail.email.body import HTMLBody, TextBody
  7. from redmail.models import EmailAddress, Error
  8. from .envs import get_span, is_last_group_row
  9. import smtplib
  10. from pathlib import Path
  11. from platform import node
  12. from getpass import getuser
  13. import datetime
  14. import os
  15. if TYPE_CHECKING:
  16. # These are never imported but just for linters
  17. import pandas as pd
  18. from PIL.Image import Image
  19. import matplotlib.pyplot as plt
  20. class EmailSender:
  21. """Red Mail Email Sender
  22. Parameters
  23. ----------
  24. host : str
  25. SMTP host address.
  26. port : int
  27. Port to the SMTP server.
  28. user_name : str, optional
  29. User name to authenticate on the server.
  30. password : str, optional
  31. User password to authenticate on the server.
  32. cls_smtp : smtplib.SMTP
  33. SMTP class to use for connection. See options
  34. from :stdlib:`Python smtplib docs <smtplib.html>`.
  35. use_starttls : bool
  36. Whether to use `STARTTLS <https://en.wikipedia.org/wiki/Opportunistic_TLS>`_
  37. when connecting to the SMTP server.
  38. **kwargs : dict
  39. Additional keyword arguments are passed to initiation in ``cls_smtp``.
  40. These are stored as attribute ``kws_smtp``
  41. Attributes
  42. ----------
  43. sender : str
  44. Address for sending emails if it is not specified
  45. in the send method.
  46. receivers : list of str
  47. Addresses to send emails if not specified
  48. in the send method.
  49. cc : list of str
  50. Carbon copies of emails if not specified
  51. in the send method.
  52. bcc : list of str
  53. Blind carbon copies of emails if not specified
  54. in the send method.
  55. subject : str
  56. Subject of emails if not specified
  57. in the send method.
  58. text : str
  59. Text body of emails if not specified
  60. in the send method.
  61. html : str
  62. HTML body of emails if not specified
  63. in the send method.
  64. text_template : str
  65. Name of the template to use as the text body of emails
  66. if not specified in the send method.
  67. html_template : str
  68. Name of the template to use as the HTML body of emails
  69. if not specified in the send method.
  70. use_jinja : bool
  71. Use Jinja to render text/HTML. If Jinja is disabled,
  72. images cannot be embedded to HTML, templates have no
  73. effect and body_params are not used. Defaults True
  74. templates_html : jinja2.Environment
  75. Jinja environment used for loading HTML templates
  76. if ``html_template`` is specified in send.
  77. templates_text : jinja2.Environment
  78. Jinja environment used for loading text templates
  79. if ``text_template`` is specified in send.
  80. default_html_theme : str
  81. Jinja template from ``templates_html_table``
  82. used for styling tables for HTML body.
  83. default_text_theme : str
  84. Jinja template from ``templates_text_table``
  85. used for styling tables for text body.
  86. templates_html_table : jinja2.Environment
  87. Jinja environment used for loading templates
  88. for table styling for HTML bodies.
  89. templates_text_table : jinja2.Environment
  90. Jinja environment used for loading templates
  91. for table styling for text bodies.
  92. kws_smtp : dict
  93. Keyword arguments passed to ``cls_smtp``
  94. when connecting to the SMTP server.
  95. connection : smtplib.SMTP, None
  96. Connection to the SMTP server. Created and closed
  97. before and after sending each email unless there
  98. is an existing connection.
  99. Examples
  100. --------
  101. .. code-block:: python
  102. email = EmailSender(server="smtp.mymail.com", port=123)
  103. email.send(
  104. subject="Example Email",
  105. sender="me@example.com",
  106. receivers=["you@example.com"],
  107. )
  108. """
  109. default_html_theme = "modest.html"
  110. default_text_theme = "pandas.txt"
  111. templates_html = jinja2.Environment(loader=jinja2.FileSystemLoader(str(Path(__file__).parent / "templates/html")))
  112. templates_html_table = jinja2.Environment(loader=jinja2.FileSystemLoader(str(Path(__file__).parent / "templates/html/table")))
  113. templates_text = jinja2.Environment(loader=jinja2.FileSystemLoader(str(Path(__file__).parent / "templates/text")))
  114. templates_text_table = jinja2.Environment(loader=jinja2.FileSystemLoader(str(Path(__file__).parent / "templates/text/table")))
  115. # Set globals
  116. templates_html_table.globals["get_span"] = get_span
  117. templates_text_table.globals["get_span"] = get_span
  118. templates_html_table.globals["is_last_group_row"] = is_last_group_row
  119. templates_text_table.globals["is_last_group_row"] = is_last_group_row
  120. attachment_encoding = 'UTF-8'
  121. def __init__(self, host:str, port:int, user_name:str=None, password:str=None, cls_smtp:smtplib.SMTP=smtplib.SMTP, use_starttls:bool=True, **kwargs):
  122. self.host = host
  123. self.port = port
  124. self.user_name = user_name
  125. self.password = password
  126. # Defaults
  127. self.sender = None
  128. self.receivers = None
  129. self.cc = None
  130. self.bcc = None
  131. self.subject = None
  132. self.text = None
  133. self.html = None
  134. self.html_template = None
  135. self.text_template = None
  136. self.use_jinja = True
  137. self.use_starttls = use_starttls
  138. self.cls_smtp = cls_smtp
  139. self.kws_smtp = kwargs
  140. self.connection = None
  141. def send(self,
  142. subject:Optional[str]=None,
  143. sender:Optional[str]=None,
  144. receivers:Union[List[str], str, None]=None,
  145. cc:Union[List[str], str, None]=None,
  146. bcc:Union[List[str], str, None]=None,
  147. html:Optional[str]=None,
  148. text:Optional[str]=None,
  149. html_template:Optional[str]=None,
  150. text_template:Optional[str]=None,
  151. body_images:Optional[Dict[str, Union[str, bytes, 'plt.Figure', 'Image']]]=None,
  152. body_tables:Optional[Dict[str, 'pd.DataFrame']]=None,
  153. body_params:Optional[Dict[str, Any]]=None,
  154. attachments:Optional[Dict[str, Union[str, os.PathLike, 'pd.DataFrame', bytes]]]=None) -> EmailMessage:
  155. """Send an email.
  156. Parameters
  157. ----------
  158. subject : str
  159. Subject of the email.
  160. sender : str, optional
  161. Email address the email is sent from.
  162. Note that some email services might not
  163. respect changing sender address
  164. (for example Gmail).
  165. receivers : list, optional
  166. Receivers of the email.
  167. cc : list, optional
  168. Cc or Carbon Copy of the email.
  169. Additional recipients of the email.
  170. bcc : list, optional
  171. Blind Carbon Copy of the email.
  172. Additional recipients of the email that
  173. don't see who else got the email.
  174. html : str, optional
  175. HTML body of the email. This is processed
  176. by Jinja and may contain loops, parametrization
  177. etc. See `Jinja documentation <https://jinja.palletsprojects.com>`_.
  178. text : str, optional
  179. Text body of the email. This is processed
  180. by Jinja and may contain loops, parametrization
  181. etc. See `Jinja documentation <https://jinja.palletsprojects.com>`_.
  182. html_template : str, optional
  183. Name of the HTML template loaded using Jinja environment specified
  184. in ``templates_html`` attribute. Specify either ``html`` or ``html_template``.
  185. text_template : str, optional
  186. Name of the text template loaded using Jinja environment specified
  187. in ``templates_text`` attribute. Specify either ``text`` or ``text_template``.
  188. body_images : dict of bytes, dict of path-like, dict of plt Figure, dict of PIL Image, optional
  189. HTML images to embed with the html. The key should be
  190. as Jinja variables in the html and the values represent
  191. images (path to an image, bytes of an image or image object).
  192. body_tables : dict of Pandas dataframes, optional
  193. HTML tables to embed with the html. The key should be
  194. as Jinja variables in the html and the values are Pandas
  195. DataFrames.
  196. body_params : dict, optional
  197. Extra Jinja parameters passed to the HTML and text bodies.
  198. use_jinja : bool
  199. Use Jinja to render text/HTML. If Jinja is disabled, body content cannot be
  200. embedded, templates have no effect and body parameters do nothing.
  201. attachments : dict, optional
  202. Attachments of the email. If dict value is string, the attachment content
  203. is the string itself. If path, the attachment is the content of the path's file.
  204. If dataframe, the dataframe is turned to bytes or text according to the
  205. file extension in dict key.
  206. Examples
  207. --------
  208. Simple example:
  209. .. code-block:: python
  210. from redmail import EmailSender
  211. email = EmailSender(
  212. host='localhost',
  213. port=0,
  214. user_name='me@example.com',
  215. password='<PASSWORD>'
  216. )
  217. email.send(
  218. subject="An email",
  219. sender="me@example.com",
  220. receivers=['you@example.com'],
  221. text="Hi, this is an email.",
  222. html="<h1>Hi, </h1><p>this is an email.</p>"
  223. )
  224. See more examples from :ref:`docs <examples>`
  225. Returns
  226. -------
  227. EmailMessage
  228. Email message.
  229. Notes
  230. -----
  231. See also `Jinja documentation <https://jinja.palletsprojects.com>`_
  232. for utilizing Jinja in ``html`` and ``text`` arguments or for using
  233. Jinja templates with ``html_template`` and ``text_template`` arguments.
  234. """
  235. msg = self.get_message(
  236. subject=subject,
  237. sender=sender,
  238. receivers=receivers,
  239. cc=cc,
  240. bcc=bcc,
  241. html=html,
  242. text=text,
  243. html_template=html_template,
  244. text_template=text_template,
  245. body_images=body_images,
  246. body_tables=body_tables,
  247. body_params=body_params,
  248. attachments=attachments,
  249. )
  250. self.send_message(msg)
  251. return msg
  252. def get_message(self,
  253. subject:Optional[str]=None,
  254. sender:Optional[str]=None,
  255. receivers:Union[List[str], str, None]=None,
  256. cc:Union[List[str], str, None]=None,
  257. bcc:Union[List[str], str, None]=None,
  258. html:Optional[str]=None,
  259. text:Optional[str]=None,
  260. html_template:Optional[str]=None,
  261. text_template:Optional[str]=None,
  262. body_images:Optional[Dict[str, Union[str, bytes, 'plt.Figure', 'Image']]]=None,
  263. body_tables:Optional[Dict[str, 'pd.DataFrame']]=None,
  264. body_params:Optional[Dict[str, Any]]=None,
  265. attachments:Optional[Dict[str, Union[str, os.PathLike, 'pd.DataFrame', bytes]]]=None,
  266. use_jinja=None) -> EmailMessage:
  267. """Get the email message"""
  268. subject = subject or self.subject
  269. sender = self.get_sender(sender)
  270. receivers = self.get_receivers(receivers)
  271. cc = self.get_cc(cc)
  272. bcc = self.get_bcc(bcc)
  273. html = html or self.html
  274. text = text or self.text
  275. html_template = html_template or self.html_template
  276. text_template = text_template or self.text_template
  277. use_jinja = self.use_jinja if use_jinja is None else use_jinja
  278. if subject is None:
  279. raise ValueError("Email must have a subject")
  280. msg = self._create_body(
  281. subject=subject,
  282. sender=sender,
  283. receivers=receivers,
  284. cc=cc,
  285. bcc=bcc,
  286. )
  287. if text is not None or text_template is not None:
  288. body = TextBody(
  289. template=self.get_text_template(text_template),
  290. table_template=self.get_text_table_template(),
  291. use_jinja=use_jinja
  292. )
  293. body.attach(
  294. msg,
  295. text,
  296. tables=body_tables,
  297. jinja_params=self.get_text_params(extra=body_params, sender=sender),
  298. )
  299. if html is not None or html_template is not None:
  300. body = HTMLBody(
  301. template=self.get_html_template(html_template),
  302. table_template=self.get_html_table_template(),
  303. use_jinja=use_jinja
  304. )
  305. body.attach(
  306. msg,
  307. html=html,
  308. images=body_images,
  309. tables=body_tables,
  310. jinja_params=self.get_html_params(extra=body_params, sender=sender)
  311. )
  312. if attachments:
  313. att = Attachments(attachments, encoding=self.attachment_encoding)
  314. att.attach(msg)
  315. return msg
  316. def get_receivers(self, receivers:Union[list, str, None]) -> Union[List[str], None]:
  317. """Get receivers of the email"""
  318. return receivers or self.receivers
  319. def get_cc(self, cc:Union[list, str, None]) -> Union[List[str], None]:
  320. """Get carbon copy (cc) of the email"""
  321. return cc or self.cc
  322. def get_bcc(self, bcc:Union[list, str, None]) -> Union[List[str], None]:
  323. """Get blind carbon copy (bcc) of the email"""
  324. return bcc or self.bcc
  325. def get_sender(self, sender:Union[str, None]) -> str:
  326. """Get sender of the email"""
  327. return sender or self.sender or self.user_name
  328. def _create_body(self, subject, sender, receivers=None, cc=None, bcc=None) -> EmailMessage:
  329. msg = EmailMessage()
  330. msg["from"] = sender
  331. msg["subject"] = subject
  332. # To whoom the email goes
  333. if receivers:
  334. msg["to"] = receivers
  335. if cc:
  336. msg['cc'] = cc
  337. if bcc:
  338. msg['bcc'] = bcc
  339. return msg
  340. def send_message(self, msg:EmailMessage):
  341. "Send the created message"
  342. if self.is_alive:
  343. self.connection.send_message(msg)
  344. else:
  345. # The connection was opened for this message
  346. # thus it is also closed with this message
  347. with self:
  348. self.connection.send_message(msg)
  349. def __enter__(self):
  350. self.connect()
  351. def __exit__(self, *args):
  352. self.close()
  353. def connect(self):
  354. "Connect to the SMTP Server"
  355. self.connection = self.get_server()
  356. def close(self):
  357. "Close (quit) the connection"
  358. self.connection.quit()
  359. self.connection = None
  360. def get_server(self) -> smtplib.SMTP:
  361. "Connect and get the SMTP Server"
  362. user = self.user_name
  363. password = self.password
  364. server = self.cls_smtp(self.host, self.port, **self.kws_smtp)
  365. if self.use_starttls:
  366. server.starttls()
  367. if user is not None or password is not None:
  368. server.login(user, password)
  369. return server
  370. @property
  371. def is_alive(self):
  372. "bool: Check if there is a connection to the SMTP server"
  373. return self.connection is not None
  374. def get_params(self, sender:str) -> Dict[str, Any]:
  375. "Get Jinja parametes passed to both text and html bodies"
  376. # TODO: Add receivers to params
  377. return {
  378. "node": node(),
  379. "user": getuser(),
  380. "now": datetime.datetime.now(),
  381. "sender": EmailAddress(sender),
  382. }
  383. def get_html_params(self, extra:Optional[dict]=None, **kwargs) -> Dict[str, Any]:
  384. "Get Jinja parameters passed to HTML body"
  385. params = self.get_params(**kwargs)
  386. params.update({
  387. "error": Error(content_type='html-inline')
  388. })
  389. if extra:
  390. params.update(extra)
  391. return params
  392. def get_text_params(self, extra:Optional[dict]=None, **kwargs) -> Dict[str, Any]:
  393. "Get Jinja parameters passed to text body"
  394. params = self.get_params(**kwargs)
  395. params.update({
  396. "error": Error(content_type='text')
  397. })
  398. if extra:
  399. params.update(extra)
  400. return params
  401. def get_html_table_template(self, layout:Optional[str]=None) -> Union[jinja2.Template, None]:
  402. "Get Jinja template for tables in HTML body"
  403. layout = self.default_html_theme if layout is None else layout
  404. if layout is None:
  405. return None
  406. return self.templates_html_table.get_template(layout)
  407. def get_html_template(self, layout:Optional[str]=None) -> Union[jinja2.Template, None]:
  408. "Get pre-made Jinja template for HTML body"
  409. if layout is None:
  410. return None
  411. return self.templates_html.get_template(layout)
  412. def get_text_table_template(self, layout:Optional[str]=None) -> jinja2.Template:
  413. "Get Jinja template for tables in text body"
  414. layout = self.default_text_theme if layout is None else layout
  415. if layout is None:
  416. return None
  417. return self.templates_text_table.get_template(layout)
  418. def get_text_template(self, layout:Optional[str]=None) -> jinja2.Template:
  419. "Get pre-made Jinja template for text body"
  420. if layout is None:
  421. return None
  422. return self.templates_text.get_template(layout)
  423. def set_template_paths(self,
  424. html:Union[str, os.PathLike, None]=None,
  425. text:Union[str, os.PathLike, None]=None,
  426. html_table:Union[str, os.PathLike, None]=None,
  427. text_table:Union[str, os.PathLike, None]=None):
  428. """Create Jinja envs for body templates using given paths
  429. This is a shortcut for manually setting them:
  430. .. code-block:: python
  431. sender.templates_html = jinja2.Environment(loader=jinja2.FileSystemLoader(...))
  432. sender.templates_text = jinja2.Environment(loader=jinja2.FileSystemLoader(...))
  433. sender.templates_html_table = jinja2.Environment(loader=jinja2.FileSystemLoader(...))
  434. sender.templates_text_table = jinja2.Environment(loader=jinja2.FileSystemLoader(...))
  435. """
  436. if html is not None:
  437. self.templates_html = jinja2.Environment(loader=jinja2.FileSystemLoader(html))
  438. if text is not None:
  439. self.templates_text = jinja2.Environment(loader=jinja2.FileSystemLoader(text))
  440. if html_table is not None:
  441. self.templates_html_table = jinja2.Environment(loader=jinja2.FileSystemLoader(html_table))
  442. if text_table is not None:
  443. self.templates_text_table = jinja2.Environment(loader=jinja2.FileSystemLoader(text_table))
  444. def copy(self) -> 'EmailSender':
  445. "Shallow copy EmailSender"
  446. return copy(self)