LightweightMailing 0.1.0__tar.gz
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.
- lightweightmailing-0.1.0/LICENSE +21 -0
- lightweightmailing-0.1.0/LightweightMailing.egg-info/PKG-INFO +13 -0
- lightweightmailing-0.1.0/LightweightMailing.egg-info/SOURCES.txt +10 -0
- lightweightmailing-0.1.0/LightweightMailing.egg-info/dependency_links.txt +1 -0
- lightweightmailing-0.1.0/LightweightMailing.egg-info/top_level.txt +1 -0
- lightweightmailing-0.1.0/PKG-INFO +13 -0
- lightweightmailing-0.1.0/README.md +0 -0
- lightweightmailing-0.1.0/mailer/__init__.py +2 -0
- lightweightmailing-0.1.0/mailer/mail_client.py +71 -0
- lightweightmailing-0.1.0/mailer/sender.py +112 -0
- lightweightmailing-0.1.0/pyproject.toml +22 -0
- lightweightmailing-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Preston David Curtis Johnson [PrestonDJ]
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: LightweightMailing
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A lightweight SMTP wrapper for the simple sending of single and batched personalised emails.
|
|
5
|
+
Author: PrestonDJ
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: homepage, https://github.com/prestondj/LightweightMailing
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Requires-Python: >=3.8
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Dynamic: license-file
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
LightweightMailing.egg-info/PKG-INFO
|
|
5
|
+
LightweightMailing.egg-info/SOURCES.txt
|
|
6
|
+
LightweightMailing.egg-info/dependency_links.txt
|
|
7
|
+
LightweightMailing.egg-info/top_level.txt
|
|
8
|
+
mailer/__init__.py
|
|
9
|
+
mailer/mail_client.py
|
|
10
|
+
mailer/sender.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
mailer
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: LightweightMailing
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A lightweight SMTP wrapper for the simple sending of single and batched personalised emails.
|
|
5
|
+
Author: PrestonDJ
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: homepage, https://github.com/prestondj/LightweightMailing
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Requires-Python: >=3.8
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Dynamic: license-file
|
|
File without changes
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
from .sender import send_email, send_batch
|
|
2
|
+
|
|
3
|
+
class MailClient():
|
|
4
|
+
"""
|
|
5
|
+
A simple SMTP client to send emails. This is a simple wrapper around the send_email and send_batch functions.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
def __init__(self, smtp_server: str, port: int, sender_email: str, sender_password: str):
|
|
9
|
+
"""
|
|
10
|
+
Configure a simple smpt client to send emails. This is a simple wrapper around the send_email and send_batch functions.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
self.smtp_server = smtp_server
|
|
14
|
+
self.port = port
|
|
15
|
+
self.sender_email = sender_email
|
|
16
|
+
self.sender_password = sender_password
|
|
17
|
+
|
|
18
|
+
# wrappers of sender.send_email and send_batch.
|
|
19
|
+
|
|
20
|
+
def send_email(self, recipient: str, subject: str, body: str) -> bool:
|
|
21
|
+
"""
|
|
22
|
+
Sends a single email to the specified recipient.
|
|
23
|
+
|
|
24
|
+
Args:
|
|
25
|
+
recipient (str): The email address of the recipient.
|
|
26
|
+
subject (str): The subject of the email.
|
|
27
|
+
body (str): The body content of the email.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
bool: True if the email was sent successfully, False otherwise.
|
|
31
|
+
"""
|
|
32
|
+
return send_email(
|
|
33
|
+
recipient,
|
|
34
|
+
subject,
|
|
35
|
+
body,
|
|
36
|
+
self.sender_email,
|
|
37
|
+
self.sender_password,
|
|
38
|
+
self.smtp_server,
|
|
39
|
+
self.port
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
def send_batch(self, recipients: list, subject: str, body: str, personalisation: dict) -> None:
|
|
43
|
+
"""
|
|
44
|
+
Sends a batch of emails to the specified recipients. Text may contain personalisation via the personalisation dictionary. Structure:
|
|
45
|
+
|
|
46
|
+
dict = {
|
|
47
|
+
recipient : {
|
|
48
|
+
"personalisation_key": "personalisation_value"
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
recipients (list): A list of email addresses to send the email to.
|
|
54
|
+
subject (str): The subject of the email.
|
|
55
|
+
body (str): The body content of the email.
|
|
56
|
+
personalisation (dict): A dictionary containing personalisation data for each recipient.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
Percentage of successful sends.
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
return send_batch(
|
|
63
|
+
recipients,
|
|
64
|
+
subject,
|
|
65
|
+
body,
|
|
66
|
+
personalisation,
|
|
67
|
+
self.sender_email,
|
|
68
|
+
self.sender_password,
|
|
69
|
+
self.smtp_server,
|
|
70
|
+
self.port
|
|
71
|
+
)
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import smtplib
|
|
2
|
+
|
|
3
|
+
from email.mime.multipart import MIMEMultipart
|
|
4
|
+
from email.mime.text import MIMEText
|
|
5
|
+
|
|
6
|
+
def send_email(recipient: str, subject: str, body: str, sender: str, password: str, smtp_server: str, port: int, live_server: smtplib.SMTP = None) -> bool:
|
|
7
|
+
"""
|
|
8
|
+
Send a single plain-text email.
|
|
9
|
+
|
|
10
|
+
Args:
|
|
11
|
+
recipient: The recipient's email address.
|
|
12
|
+
subject: The email subject.
|
|
13
|
+
body: The plain-text message body.
|
|
14
|
+
sender: The sender's email address.
|
|
15
|
+
password: The sender's SMTP password.
|
|
16
|
+
smtp_server: The SMTP server hostname.
|
|
17
|
+
port: The SMTP server port.
|
|
18
|
+
live_server: An existing connection, used when sending a batch.
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
True if the message was sent successfully, otherwise False.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
# build the MIME message shared by both sending paths.
|
|
25
|
+
msg = MIMEMultipart("alternative")
|
|
26
|
+
msg["From"] = sender
|
|
27
|
+
msg["To"] = recipient
|
|
28
|
+
msg["Subject"] = subject
|
|
29
|
+
msg.attach(MIMEText(body, "plain"))
|
|
30
|
+
|
|
31
|
+
# reuse the caller's connection when sending as part of a batch.
|
|
32
|
+
if live_server:
|
|
33
|
+
try:
|
|
34
|
+
live_server.sendmail(sender, recipient, msg.as_string())
|
|
35
|
+
return True
|
|
36
|
+
except Exception as e:
|
|
37
|
+
print(f"Error sending email to {recipient}: {e}")
|
|
38
|
+
return False
|
|
39
|
+
|
|
40
|
+
# open and authenticate a connection for a standalone message.
|
|
41
|
+
try:
|
|
42
|
+
with smtplib.SMTP(smtp_server, port) as server:
|
|
43
|
+
if port == 587:
|
|
44
|
+
server.starttls()
|
|
45
|
+
|
|
46
|
+
server.login(sender, password)
|
|
47
|
+
server.sendmail(sender, recipient, msg.as_string())
|
|
48
|
+
print(f"Email sent successfully to {recipient}")
|
|
49
|
+
return True
|
|
50
|
+
|
|
51
|
+
except Exception as e:
|
|
52
|
+
print(f"Error sending email to {recipient}: {e}")
|
|
53
|
+
return False
|
|
54
|
+
|
|
55
|
+
def send_batch(recipients: list, subject: str, body: str, personalisation: dict, sender: str, password: str, smtp_server: str, port: int) -> None:
|
|
56
|
+
"""
|
|
57
|
+
Send personalized plain-text emails over one SMTP connection.
|
|
58
|
+
|
|
59
|
+
The personalization mapping is keyed by recipient. Values are substituted
|
|
60
|
+
into the subject and body using ``str.format`` syntax, such as ``{name}``.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
recipients: The recipient email addresses.
|
|
64
|
+
subject: The email subject template.
|
|
65
|
+
body: The plain-text body template.
|
|
66
|
+
personalisation: Recipient-specific values for template substitution.
|
|
67
|
+
sender: The sender's email address.
|
|
68
|
+
password: The sender's SMTP password.
|
|
69
|
+
smtp_server: The SMTP server hostname.
|
|
70
|
+
port: The SMTP server port.
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
The fraction of recipients whose messages were sent successfully.
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
success = 0
|
|
77
|
+
total = len(recipients)
|
|
78
|
+
|
|
79
|
+
try:
|
|
80
|
+
with smtplib.SMTP(smtp_server, port) as server:
|
|
81
|
+
if port == 587: # explicit TLS
|
|
82
|
+
server.starttls()
|
|
83
|
+
|
|
84
|
+
server.login(sender, password)
|
|
85
|
+
|
|
86
|
+
for recipient in recipients:
|
|
87
|
+
try:
|
|
88
|
+
# render templates with values specific to this recipient.
|
|
89
|
+
values = personalisation.get(recipient, {})
|
|
90
|
+
personalised_subject = subject.format(**values)
|
|
91
|
+
personalised_body = body.format(**values)
|
|
92
|
+
|
|
93
|
+
send_email(
|
|
94
|
+
recipient,
|
|
95
|
+
personalised_subject,
|
|
96
|
+
personalised_body,
|
|
97
|
+
sender,
|
|
98
|
+
password,
|
|
99
|
+
smtp_server,
|
|
100
|
+
port,
|
|
101
|
+
live_server=server,
|
|
102
|
+
)
|
|
103
|
+
success += 1
|
|
104
|
+
|
|
105
|
+
except Exception as e:
|
|
106
|
+
print(f"Error sending email to {recipient}: {e}")
|
|
107
|
+
|
|
108
|
+
except Exception as e:
|
|
109
|
+
print(f"Error sending batch emails [No recipient received]: {e}")
|
|
110
|
+
return 0 # no success
|
|
111
|
+
|
|
112
|
+
return success/total # percentage of success
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "LightweightMailing"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A lightweight SMTP wrapper for the simple sending of single and batched personalised emails."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
authors = [
|
|
11
|
+
{name = "PrestonDJ"}
|
|
12
|
+
]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Programming Language :: Python :: 3",
|
|
15
|
+
"Operating System :: OS Independent"
|
|
16
|
+
]
|
|
17
|
+
license = "MIT"
|
|
18
|
+
requires-python = ">=3.8"
|
|
19
|
+
dependencies = []
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
homepage = "https://github.com/prestondj/LightweightMailing"
|