arpakitlib 1.8.157__py3-none-any.whl → 1.8.159__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,7 +2,9 @@ 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
7
+ from email.utils import formataddr
6
8
 
7
9
  import aiosmtplib
8
10
 
@@ -12,79 +14,137 @@ from project.core.util import setup_logging
12
14
  _logger = logging.getLogger(__name__)
13
15
 
14
16
 
17
+ def _build_email_message(
18
+ *,
19
+ from_email: str,
20
+ to_email: str,
21
+ subject: str,
22
+ html_content: str,
23
+ from_name: str | None = None,
24
+ ) -> EmailMessage:
25
+ msg = EmailMessage()
26
+ if from_name:
27
+ msg["From"] = formataddr((from_name, from_email))
28
+ else:
29
+ msg["From"] = from_email # без имени
30
+ msg["To"] = to_email.strip()
31
+ msg["Subject"] = subject
32
+ msg.add_alternative(html_content, subtype="html")
33
+ return msg
34
+
35
+
15
36
  def sync_send_email(
16
37
  *,
38
+ from_name: str | None = get_cached_settings().project_name,
17
39
  to_email: str,
18
- subject: str = "Gamer.Market",
40
+ subject: str = get_cached_settings().project_name,
19
41
  html_content: str,
20
- emulate: bool = False
42
+ emulate: bool = False,
21
43
  ):
22
- to_email = to_email.strip()
23
-
24
44
  if emulate:
25
- _logger.info(f"emulate email sending, {to_email=}, {subject=}, {html_content=}")
45
+ _logger.info(f"emulate email sending, to_email={to_email!r}, subject={subject!r}")
26
46
  return
27
47
 
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)
48
+ message = _build_email_message(
49
+ from_email=get_cached_settings().email_smtp_user,
50
+ from_name=from_name,
51
+ to_email=to_email, subject=subject, html_content=html_content
52
+ )
45
53
 
46
- _logger.info(f"email was send, {to_email=}")
54
+ if get_cached_settings().email_smtp_port == 465:
55
+ _logger.info("using port 465 (SSL)")
56
+ with smtplib.SMTP_SSL(
57
+ host=get_cached_settings().email_smtp_hostname,
58
+ port=465,
59
+ timeout=dt.timedelta(seconds=15).total_seconds(),
60
+ context=ssl.create_default_context(),
61
+ ) as server:
62
+ server.login(
63
+ get_cached_settings().email_smtp_user,
64
+ get_cached_settings().email_smtp_password,
65
+ )
66
+ server.send_message(message)
67
+
68
+ elif get_cached_settings().email_smtp_port == 587:
69
+ _logger.info("using port 587 (STARTTLS)")
70
+ with smtplib.SMTP(
71
+ host=get_cached_settings().email_smtp_hostname,
72
+ port=587,
73
+ timeout=dt.timedelta(seconds=15).total_seconds(),
74
+ ) as server:
75
+ server.ehlo()
76
+ server.starttls(context=ssl.create_default_context())
77
+ server.ehlo()
78
+ server.login(
79
+ get_cached_settings().email_smtp_user,
80
+ get_cached_settings().email_smtp_password,
81
+ )
82
+ server.send_message(message)
83
+ else:
84
+ raise ValueError("Unsupported SMTP port")
85
+
86
+ _logger.info(f"email was sent, to_email={to_email!r}")
47
87
 
48
88
 
49
89
  async def async_send_email(
50
90
  *,
91
+ from_name: str | None = get_cached_settings().project_name,
51
92
  to_email: str,
52
- subject: str = "Gamer.Market",
93
+ subject: str = get_cached_settings().project_name,
53
94
  html_content: str,
54
- emulate: bool = False
95
+ emulate: bool = False,
55
96
  ):
56
- to_email = to_email.strip()
57
-
58
97
  if emulate:
59
- _logger.info(f"emulate email sending, {to_email=}, {subject=}, {html_content=}")
98
+ _logger.info(f"emulate email sending, to_email={to_email!r}, subject={subject!r}")
60
99
  return
61
100
 
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()
101
+ message = _build_email_message(
102
+ from_email=get_cached_settings().email_smtp_user,
103
+ from_name=from_name,
104
+ to_email=to_email, subject=subject, html_content=html_content
76
105
  )
77
106
 
78
- _logger.info(f"email was send, {to_email=}")
107
+ if get_cached_settings().email_smtp_port == 465:
108
+ _logger.info("using port 465 (SSL)")
109
+ await aiosmtplib.send(
110
+ message,
111
+ hostname=get_cached_settings().email_smtp_hostname,
112
+ port=465,
113
+ username=get_cached_settings().email_smtp_user,
114
+ password=get_cached_settings().email_smtp_password,
115
+ use_tls=True,
116
+ start_tls=False,
117
+ timeout=dt.timedelta(seconds=15).total_seconds(),
118
+ tls_context=ssl.create_default_context(),
119
+ )
120
+
121
+ elif get_cached_settings().email_smtp_port == 587:
122
+ _logger.info("using port 587 (STARTTLS)")
123
+ await aiosmtplib.send(
124
+ message,
125
+ hostname=get_cached_settings().email_smtp_hostname,
126
+ port=587,
127
+ username=get_cached_settings().email_smtp_user,
128
+ password=get_cached_settings().email_smtp_password,
129
+ use_tls=False,
130
+ start_tls=True,
131
+ timeout=dt.timedelta(seconds=15).total_seconds(),
132
+ tls_context=ssl.create_default_context(),
133
+ )
134
+ else:
135
+ raise ValueError("Unsupported SMTP port")
136
+
137
+ _logger.info(f"email was sent, to_email={to_email!r}")
79
138
 
80
139
 
81
140
  async def __async_example():
82
141
  setup_logging()
83
142
  await async_send_email(
84
143
  to_email="arpakit@gmail.com",
85
- html_content="Hello world 2"
144
+ html_content="Hello world 2",
145
+ from_name="Gamer Market"
86
146
  )
87
147
 
88
148
 
89
- if __name__ == '__main__':
149
+ if __name__ == "__main__":
90
150
  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.159
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=ZfJkdCxyZhbNs4A2FgOQcath75BtJLLIhMFwYhVxmqg,4707
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.159.dist-info/LICENSE,sha256=GPEDQMam2r7FSTYqM1mm7aKnxLaWcBotH7UvQtea-ec,11355
412
+ arpakitlib-1.8.159.dist-info/METADATA,sha256=jHymW1AC6wM8BZW-PSxW_USIXesFIPr6GYZW5P8Ey6k,3706
413
+ arpakitlib-1.8.159.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
414
+ arpakitlib-1.8.159.dist-info/entry_points.txt,sha256=36xqR3PJFT2kuwjkM_EqoIy0qFUDPKSm_mJaI7emewE,87
415
+ arpakitlib-1.8.159.dist-info/RECORD,,