arpakitlib 1.8.157__py3-none-any.whl → 1.8.158__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@ import asyncio
2
2
  import datetime as dt
3
3
  import logging
4
4
  import smtplib
5
+ import ssl
5
6
  from email.message import EmailMessage
6
7
 
7
8
  import aiosmtplib
@@ -12,79 +13,112 @@ from project.core.util import setup_logging
12
13
  _logger = logging.getLogger(__name__)
13
14
 
14
15
 
16
+ def _build_message(to_email: str, subject: str, html_content: str) -> EmailMessage:
17
+ msg = EmailMessage()
18
+ msg["From"] = get_cached_settings().email_smtp_user
19
+ msg["To"] = to_email.strip()
20
+ msg["Subject"] = subject
21
+ msg.add_alternative(html_content, subtype="html")
22
+ return msg
23
+
24
+
15
25
  def sync_send_email(
16
26
  *,
17
27
  to_email: str,
18
- subject: str = "Gamer.Market",
28
+ subject: str = get_cached_settings().project_name,
19
29
  html_content: str,
20
- emulate: bool = False
30
+ emulate: bool = False,
21
31
  ):
22
- to_email = to_email.strip()
23
-
24
32
  if emulate:
25
- _logger.info(f"emulate email sending, {to_email=}, {subject=}, {html_content=}")
33
+ _logger.info(f"emulate email sending, to_email={to_email!r}, subject={subject!r}")
26
34
  return
27
35
 
28
- message = EmailMessage()
29
- message["From"] = get_cached_settings().email_smtp_user
30
- message["To"] = to_email
31
- message["Subject"] = subject
32
- message.add_alternative(html_content, subtype="html")
33
-
34
- with smtplib.SMTP_SSL(
35
- host=get_cached_settings().email_smtp_hostname,
36
- port=get_cached_settings().email_smtp_port,
37
- timeout=dt.timedelta(seconds=15).total_seconds()
38
-
39
- ) as server:
40
- server.login(
41
- get_cached_settings().email_smtp_user,
42
- get_cached_settings().email_smtp_password
43
- )
44
- server.send_message(message)
45
-
46
- _logger.info(f"email was send, {to_email=}")
36
+ if get_cached_settings().email_smtp_port == 465:
37
+ _logger.info("using port 465 (SSL)")
38
+ with smtplib.SMTP_SSL(
39
+ host=get_cached_settings().email_smtp_hostname,
40
+ port=465,
41
+ timeout=dt.timedelta(seconds=30).total_seconds(),
42
+ context=ssl.create_default_context(),
43
+ ) as server:
44
+ server.login(
45
+ get_cached_settings().email_smtp_user,
46
+ get_cached_settings().email_smtp_password,
47
+ )
48
+ server.send_message(_build_message(to_email, subject, html_content))
49
+
50
+ elif get_cached_settings().email_smtp_port == 587:
51
+ _logger.info("using port 587 (STARTTLS)")
52
+ with smtplib.SMTP(
53
+ host=get_cached_settings().email_smtp_hostname,
54
+ port=587,
55
+ timeout=dt.timedelta(seconds=30).total_seconds(),
56
+ ) as server:
57
+ server.ehlo()
58
+ server.starttls(context=ssl.create_default_context())
59
+ server.ehlo()
60
+ server.login(
61
+ get_cached_settings().email_smtp_user,
62
+ get_cached_settings().email_smtp_password,
63
+ )
64
+ server.send_message(_build_message(to_email, subject, html_content))
65
+ else:
66
+ raise ValueError("Unsupported SMTP port")
67
+
68
+ _logger.info(f"email was sent, to_email={to_email!r}")
47
69
 
48
70
 
49
71
  async def async_send_email(
50
72
  *,
51
73
  to_email: str,
52
- subject: str = "Gamer.Market",
74
+ subject: str = get_cached_settings().project_name,
53
75
  html_content: str,
54
- emulate: bool = False
76
+ emulate: bool = False,
55
77
  ):
56
- to_email = to_email.strip()
57
-
58
78
  if emulate:
59
- _logger.info(f"emulate email sending, {to_email=}, {subject=}, {html_content=}")
79
+ _logger.info(f"emulate email sending, to_email={to_email!r}, subject={subject!r}")
60
80
  return
61
81
 
62
- message = EmailMessage()
63
- message["From"] = get_cached_settings().email_smtp_user
64
- message["To"] = to_email
65
- message["Subject"] = subject
66
- message.add_alternative(html_content, subtype="html")
67
-
68
- await aiosmtplib.send(
69
- message,
70
- hostname=get_cached_settings().email_smtp_hostname,
71
- port=get_cached_settings().email_smtp_port,
72
- username=get_cached_settings().email_smtp_user,
73
- password=get_cached_settings().email_smtp_password,
74
- use_tls=True,
75
- timeout=dt.timedelta(seconds=15).total_seconds()
76
- )
82
+ if get_cached_settings().email_smtp_port == 465:
83
+ _logger.info("using port 465 (SSL)")
84
+ await aiosmtplib.send(
85
+ _build_message(to_email, subject, html_content),
86
+ hostname=get_cached_settings().email_smtp_hostname,
87
+ port=get_cached_settings().email_smtp_port,
88
+ username=get_cached_settings().email_smtp_user,
89
+ password=get_cached_settings().email_smtp_password,
90
+ use_tls=True,
91
+ start_tls=False,
92
+ timeout=dt.timedelta(seconds=30).total_seconds(),
93
+ tls_context=ssl.create_default_context(),
94
+ )
95
+
96
+ elif get_cached_settings().email_smtp_port == 587:
97
+ _logger.info("using port 587 (STARTTLS)")
98
+ await aiosmtplib.send(
99
+ _build_message(to_email, subject, html_content),
100
+ hostname=get_cached_settings().email_smtp_hostname,
101
+ port=get_cached_settings().email_smtp_port,
102
+ username=get_cached_settings().email_smtp_user,
103
+ password=get_cached_settings().email_smtp_password,
104
+ use_tls=False,
105
+ start_tls=True,
106
+ timeout=dt.timedelta(seconds=30).total_seconds(),
107
+ tls_context=ssl.create_default_context(),
108
+ )
109
+ else:
110
+ raise ValueError("Unsupported SMTP port")
77
111
 
78
- _logger.info(f"email was send, {to_email=}")
112
+ _logger.info(f"email was sent, to_email={to_email!r}")
79
113
 
80
114
 
81
115
  async def __async_example():
82
116
  setup_logging()
83
117
  await async_send_email(
84
118
  to_email="arpakit@gmail.com",
85
- html_content="Hello world 2"
119
+ html_content="Hello world 2",
86
120
  )
87
121
 
88
122
 
89
- if __name__ == '__main__':
123
+ if __name__ == "__main__":
90
124
  asyncio.run(__async_example())
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: arpakitlib
3
- Version: 1.8.157
3
+ Version: 1.8.158
4
4
  Summary: arpakitlib
5
5
  License: Apache-2.0
6
6
  Keywords: arpakitlib,arpakit,arpakit-company,arpakitcompany,arpakit_company
@@ -359,7 +359,7 @@ arpakitlib/_arpakit_project_template_v_5/project/tg_bot_notifier/tg_bot_notifier
359
359
  arpakitlib/_arpakit_project_template_v_5/project/util/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
360
360
  arpakitlib/_arpakit_project_template_v_5/project/util/arpakitlib_project_template_util.py,sha256=syA_IuszHVub0zm0sVdB4_7rPJXwXRW4JmQ4qHbjXPk,396
361
361
  arpakitlib/_arpakit_project_template_v_5/project/util/etc.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
362
- arpakitlib/_arpakit_project_template_v_5/project/util/send_email_.py,sha256=ehpBiIt6vpUXeuIY_tvr-80LGM7VBCUhgASGZpWy-Ok,2387
362
+ arpakitlib/_arpakit_project_template_v_5/project/util/send_email_.py,sha256=J1E9ee7s5gd3s0i_GseeND4kyNXkJVwLUFHR3s8JWF4,4140
363
363
  arpakitlib/_arpakit_project_template_v_5/todo.txt,sha256=q132Jbx229ThY77S3YiN-Cj5AVm7k1VlJcMYIbZUHUY,3
364
364
  arpakitlib/ar_additional_model_util.py,sha256=GFg-glLCxH9X95R2bhTJsscVwv37FgE1qbaAAyXrnIE,917
365
365
  arpakitlib/ar_aiogram_util.py,sha256=4bizX5Xg-E2-r2TXXGQGanJozsIWPVf5luO3vKUN8p8,8471
@@ -408,8 +408,8 @@ arpakitlib/ar_ssh_runner_util.py,sha256=yvAwza480MkHKvLkDEsR7JNh2bYNs6P9rCVo4NA8
408
408
  arpakitlib/ar_str_util.py,sha256=2lGpnXDf2h1cBZpVf5i1tX_HCv5iBd6IGnrCw4QWWlY,4350
409
409
  arpakitlib/ar_type_util.py,sha256=Cs_tef-Fc5xeyAF54KgISCsP11NHyzIsglm4S3Xx7iM,4049
410
410
  arpakitlib/ar_yookassa_api_client_util.py,sha256=VozuZeCJjmLd1zj2BdC9WfiAQ3XYOrIMsdpNK-AUlm0,5347
411
- arpakitlib-1.8.157.dist-info/LICENSE,sha256=GPEDQMam2r7FSTYqM1mm7aKnxLaWcBotH7UvQtea-ec,11355
412
- arpakitlib-1.8.157.dist-info/METADATA,sha256=JrajnfsEw_XyxLDxB-5qvd1cjf6LQSaVCmjGtjBahyQ,3706
413
- arpakitlib-1.8.157.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
414
- arpakitlib-1.8.157.dist-info/entry_points.txt,sha256=36xqR3PJFT2kuwjkM_EqoIy0qFUDPKSm_mJaI7emewE,87
415
- arpakitlib-1.8.157.dist-info/RECORD,,
411
+ arpakitlib-1.8.158.dist-info/LICENSE,sha256=GPEDQMam2r7FSTYqM1mm7aKnxLaWcBotH7UvQtea-ec,11355
412
+ arpakitlib-1.8.158.dist-info/METADATA,sha256=Owzc2sjXbylqDiJbrOZHuUsxlWpq7wHOwWNIu_YCvWE,3706
413
+ arpakitlib-1.8.158.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
414
+ arpakitlib-1.8.158.dist-info/entry_points.txt,sha256=36xqR3PJFT2kuwjkM_EqoIy0qFUDPKSm_mJaI7emewE,87
415
+ arpakitlib-1.8.158.dist-info/RECORD,,