-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailing.py
More file actions
87 lines (71 loc) · 3 KB
/
Mailing.py
File metadata and controls
87 lines (71 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import os
def send_individuals(subject, body, recipients, pdf_path=None):
sender_password = "Your-App-Password"
html_body_template = """
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Email</title>
</head>
<body style="margin: 0; padding: 0; background-color: #f4f4f4; font-family: Arial, sans-serif;">
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="background-color: #f4f4f4; padding: 20px 0;">
<tr>
<td align="center">
<table width="600" cellpadding="0" cellspacing="0" border="0" style="background-color: #ffffff; padding: 30px; border-radius: 6px;">
<tr>
<td style="font-size: 16px; color: #333333;">
<p>Hi there 👋,</p>
<p>This is a sample HTML email content. You can customize this message to include anything you like – updates, notifications, or just saying hello!</p>
<p>Here's a quick list:</p>
<ul>
<li>Simple HTML format</li>
<li>Works in most email clients</li>
<li>Sent via Python script</li>
</ul>
<p>Have a great day!<br>– Your Python Script 😊</p>
</td>
</tr>
<tr>
<td align="center" style="padding-top: 30px; font-size: 12px; color: #999999;">
© 2025 - Generated with ❤️ from Python
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
"""
for recipient in recipients:
name = recipient.split('@')[0].capitalize()
html_body = html_body_template.format(name=name)
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = sender_email
msg['To'] = recipient
msg.attach(MIMEText(html_body, 'html'))
if pdf_path and os.path.exists(pdf_path):
with open(pdf_path, 'rb') as pdf_file:
pdf_part = MIMEBase('aapplication', 'octet-stream')
pdf_part.set_payload(pdf_file.read())
encoders.encode_base64(pdf_part)
pdf_part.add_header('Content-Description', f'attachment; filename= {os.path.basename(pdf_path)}')
msg.attach(pdf_part)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
server.login(sender_email, sender_password)
server.sendmail(sender_email, recipient, msg.as_string())
send_individuals(
subject='Writing the python scripts',
body='Learning the Python Automation',
recipients=recipient_list,
pdf_path='myemail.pdf'
)