-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail.py
More file actions
214 lines (177 loc) · 8.12 KB
/
mail.py
File metadata and controls
214 lines (177 loc) · 8.12 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import smtplib
import openpyxl
import time
import random
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
from email.utils import formatdate
import os
import socket
# Get the directory where the script is located
script_directory = os.path.dirname(os.path.abspath(__file__))
# Define a function to check internet connectivity
def is_connected():
try:
socket.create_connection(("8.8.8.8", 53), timeout=5)
return True
except OSError:
pass
return False
# Function to connect to the SMTP server
def connect_to_server():
while True:
if is_connected():
try:
server = smtplib.SMTP_SSL(smtp_server, smtp_port)
server.ehlo()
server.login(smtp_username, smtp_password)
print("Connected to SMTP server successfully.")
return server
except smtplib.SMTPException as e:
print(f'Error connecting to SMTP server: {e}')
server = None
else:
print("No internet connection. Retrying in 60 seconds...")
time.sleep(60)
# Function to send progress notification email
def send_progress_notification(email_count):
try:
if is_connected():
server = connect_to_server()
if server is not None:
recipient = "youemail@gmail.com" #Enter you email address here, when 100 mails complete you will received a notification email
subject = f"{email_count} Emails Sent So Far"
message = f"{email_count} emails have been sent from your list."
msg = MIMEMultipart()
msg['From'] = f'{from_name} <{smtp_username}>'
msg['To'] = recipient
msg['Subject'] = subject
msg['Date'] = formatdate(localtime=True)
email_body = MIMEText(message, 'plain')
msg.attach(email_body)
server.sendmail(smtp_username, [recipient], msg.as_string())
print(f'Progress notification email sent to {recipient}')
except smtplib.SMTPException as e:
print(f'Error sending progress notification email: {e}')
# Load SMTP configuration from Excel
smtp_config_file_path = os.path.join(script_directory, 'smtp.xlsx')
try:
wb_smtp = openpyxl.load_workbook(smtp_config_file_path)
ws_smtp = wb_smtp.active
smtp_username = ws_smtp.cell(row=2, column=2).value
smtp_password = ws_smtp.cell(row=2, column=3).value
smtp_server = ws_smtp.cell(row=2, column=4).value
smtp_port = 465 # Assuming SMTP port is 465 for SSL
from_name = ws_smtp.cell(row=2, column=1).value # "From" name from Excel
except Exception as e:
print(f'Error loading SMTP configuration from Excel: {e}')
exit()
# Initialize SMTP server
server = None
# Load email addresses from Excel
excel_file_path = os.path.join(script_directory, 'email_list.xlsx')
start_row = 2
try:
wb = openpyxl.load_workbook(excel_file_path)
ws = wb.active
email_list = [ws.cell(row=i, column=1).value for i in range(start_row, ws.max_row + 1)]
except Exception as e:
print(f'Error loading email addresses from Excel: {e}')
exit()
# Load blacklist emails
blacklist_emails = ['me.abudhabi@sgs.comTel']
# Read email body HTML content from file
email_body_path = os.path.join(script_directory, 'email.html')
try:
with open(email_body_path, 'r', encoding='utf-8') as email_body_file:
email_body_html = email_body_file.read()
except Exception as e:
print(f'Error reading email body HTML from file: {e}')
exit()
# Initialize variables
current_row = start_row
emails_sent = 0 # Initialize the counter
progress_notification_interval = 100 # Set the interval for progress notifications
# Load the workbook to update the "Sent" status
wb_email_list = openpyxl.load_workbook(excel_file_path)
ws_email_list = wb_email_list.active
# Define a function to update the delivery status
def update_delivery_status(row, status):
ws_email_list.cell(row=row, column=2, value=status)
wb_email_list.save(excel_file_path)
while current_row <= len(email_list):
server = None # Reset the server object at the beginning of each iteration
while not is_connected():
print("No internet connection. Retrying in 60 seconds...")
time.sleep(60)
while server is None:
server = connect_to_server()
if server is None:
continue # Retry the connection until it succeeds
recipient = email_list[current_row - start_row]
try:
if recipient and recipient not in blacklist_emails:
print(f'{emails_sent + 1} Sending email to {recipient}') # Print the counter
emails_sent += 1 # Increment the counter
msg = MIMEMultipart()
msg['From'] = f'{from_name} <{smtp_username}>' # "From" name from Excel
msg['To'] = recipient
msg['Subject'] = ws_smtp.cell(row=2, column=6).value # Subject from Excel
msg['Date'] = formatdate(localtime=True) # Add a date header
# Convert the HTML email body to rich text
email_body = MIMEText(email_body_html, 'html')
msg.attach(email_body)
image_dir = os.path.join(script_directory, 'images')
image_files = [f for f in os.listdir(image_dir) if f.endswith('.jpg')]
for i, image_file in enumerate(image_files, start=1):
with open(os.path.join(image_dir, image_file), "rb") as f:
image_data = f.read()
msg_image = MIMEImage(image_data)
image_cid = f'image{i}'
msg_image.add_header('Content-ID', f'<{image_cid}>')
msg.attach(msg_image)
# Add PDF file attachments from the "pdf" directory
pdf_dir = os.path.join(script_directory, 'pdf')
pdf_files = [f for f in os.listdir(pdf_dir) if f.endswith('.pdf')]
for pdf_file in pdf_files:
pdf_path = os.path.join(pdf_dir, pdf_file)
with open(pdf_path, "rb") as pdf:
pdf_attachment = MIMEApplication(pdf.read(), _subtype="pdf")
pdf_attachment.add_header(
"Content-Disposition", f"attachment; filename={pdf_file}"
)
msg.attach(pdf_attachment)
try:
server.sendmail(smtp_username, [recipient], msg.as_string())
print(f'Successfully sent to {recipient}')
# Update "Delivered" status in Excel sheet
update_delivery_status(current_row, 'Delivered')
# Check if it's time to send a progress notification
if emails_sent % progress_notification_interval == 0:
send_progress_notification(emails_sent)
# Random delay and countdown timer
random_delay = random.randint(20, 40)
print(f'Next email will be sent in {random_delay} seconds.')
for i in range(random_delay, 0, -1):
print(f'Sending email in {i} seconds...', end='\r')
time.sleep(1)
print(' ' * 50, end='\r') # Clear the countdown timer
current_row += 1
except smtplib.SMTPException as e:
print(f'Error sending to {recipient}: {e}')
# Update "Error" status in Excel sheet
update_delivery_status(current_row, 'Error')
current_row += 1 # Continue to the next recipient
else:
print(f'Skipping blacklisted email: {recipient}')
# Update "Not Delivered" status in Excel sheet
update_delivery_status(current_row, 'Not Delivered')
current_row += 1
except Exception as e:
print(f'Error processing {recipient}: {e}')
# Update "Error" status in Excel sheet
update_delivery_status(current_row, 'Error')
current_row += 1
# End of script