-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail.py
More file actions
214 lines (178 loc) Β· 7 KB
/
Copy pathemail.py
File metadata and controls
214 lines (178 loc) Β· 7 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 time
import os
import csv
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email_validator import validate_email, EmailNotValidError
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor, as_completed
# π Your Email Credentials
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
EMAIL_ADDRESS = os.getenv("EMAIL_ADDRESS")
EMAIL_PASSWORD = os.getenv("EMAIL_PASSWORD")
# π File Paths
EMAIL_CSV_FILE = "jobseekers_email.csv"
SENT_EMAILS_FILE = "sent_emails_fyers.txt"
DAILY_COUNT_FILE = "daily_count_fyers.txt"
# π Constants
EMAILS_PER_BATCH = 1
BATCH_DELAY_SECONDS = 10
DAILY_EMAIL_LIMIT = 400
RETRY_DELAY_SECONDS = 60 # Retry delay if sending fails
WAIT_24_HOURS_SECONDS = 86400 # Wait 24 hours if limit is reached
# β
Load & Validate Emails from CSV
def load_valid_emails():
"""Load valid emails from the CSV file and exclude already sent ones."""
valid_emails = []
if not os.path.exists(EMAIL_CSV_FILE):
print(f"β Email CSV file '{EMAIL_CSV_FILE}' not found.")
return []
# Load already sent emails
sent_emails = set()
if os.path.exists(SENT_EMAILS_FILE):
with open(SENT_EMAILS_FILE, "r", encoding="utf-8") as file:
sent_emails = {line.strip() for line in file}
# Read CSV and validate emails
with open(EMAIL_CSV_FILE, "r", encoding="utf-8") as csv_file:
reader = csv.reader(csv_file)
for row in reader:
if not row or not row[0].strip():
continue # Skip empty rows
email = row[0].strip()
if email in sent_emails: # Skip already sent emails
continue
if validate_email_safely(email):
valid_emails.append(email)
else:
print(f"β οΈ Skipping invalid email: {email}")
return valid_emails
# β
Validate Emails Safely
def validate_email_safely(email):
"""Check if an email is valid."""
try:
v = validate_email(email, check_deliverability=True)
return v["email"]
except EmailNotValidError:
return None
# β
Check if Email Was Sent Before
def has_email_been_sent(email):
if not os.path.exists(SENT_EMAILS_FILE):
return False
with open(SENT_EMAILS_FILE, "r", encoding="utf-8") as file:
return email in {line.strip() for line in file}
# β
Log Sent Emails
def log_sent_email(email):
with open(SENT_EMAILS_FILE, "a", encoding="utf-8") as file:
file.write(email + "\n")
# β
Send Email Function
def send_email(recipient_email):
"""Send an email to the recipient."""
print(f"π€ Attempting to send email to: {recipient_email}")
if has_email_been_sent(recipient_email):
print(f"β οΈ Email already sent to: {recipient_email}")
return False
# π§ Email Content
subject = "You are shortlisted..."
body = """
<html>
<head>
<style>
body { font-family: Arial, sans-serif; color: #333; }
.container { max-width: 600px; padding: 20px; border-radius: 10px; background: #f9f9f9; }
h2 { color: #0066cc; }
.btn { display: inline-block; padding: 10px 15px; color: #fff; background: #007bff;
text-decoration: none; border-radius: 5px; font-weight: bold; }
.footer { font-size: 12px; color: #777; margin-top: 20px; }
</style>
</head>
<body>
<div class="container">
<h2>π€ JobFinder Bot</h2>
<p>π Get real-time job alerts, career tips, and hiring updatesβall in one place for both freshers and experienced professionals.</p>
<h3>β¨ Why Join?</h3>
<ul>
<li>β
No Ads β Enjoy pure job listings without distractions.</li>
<li>β
Direct Links β Apply instantly with no redirections.</li>
<li>β
Work From Home β Easily find remote opportunities.</li>
<li>β
Instant Alerts β Stay ahead with real-time updates.</li>
<li>β
Absolutely Free β Get all these benefits at no cost!</li>
</ul>
<p>π <strong>Never miss an opportunity!</strong></p>
<p><a href="https://t.me/+-edEYkC7C7gwODdl" class="btn">π² Join Now</a></p>
<p class="footer">πΌ Spread successβshare it!</p>
</div>
</body>
</html>
"""
# π₯ FIX: Move this line above `msg.attach(...)`
msg = MIMEMultipart()
msg["From"] = EMAIL_ADDRESS
msg["To"] = recipient_email
msg["Subject"] = subject
msg.attach(MIMEText(body, "html", "utf-8")) # Use UTF-8 encoding
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
server.sendmail(EMAIL_ADDRESS, recipient_email, msg.as_string())
server.quit()
print(f"β
Email sent ")
log_sent_email(recipient_email)
increment_daily_count()
return True
except Exception as e:
print(f"β Failed to send email to {recipient_email}: {e}")
return False
# β
Track Daily Email Count
def increment_daily_count():
today = datetime.now().date()
count = 0
if os.path.exists(DAILY_COUNT_FILE):
with open(DAILY_COUNT_FILE, "r", encoding="utf-8") as file:
date_str, count_str = file.readline().strip().split(",")
last_date = datetime.strptime(date_str, "%Y-%m-%d").date()
count = int(count_str) if last_date == today else 0
count += 1
with open(DAILY_COUNT_FILE, "w", encoding="utf-8") as file:
file.write(f"{today},{count}")
return count
# β
Get Current Daily Count
def get_daily_count():
today = datetime.now().date()
if os.path.exists(DAILY_COUNT_FILE):
with open(DAILY_COUNT_FILE, "r", encoding="utf-8") as file:
date_str, count_str = file.readline().strip().split(",")
last_date = datetime.strptime(date_str, "%Y-%m-%d").date()
if last_date == today:
return int(count_str)
return 0
# β
Bulk Email Sender
def bulk_send_emails():
emails = load_valid_emails()
if not emails:
print("π« No valid emails found.")
return
while True:
daily_count = get_daily_count()
if daily_count >= DAILY_EMAIL_LIMIT:
print("β³ Daily limit reached. Waiting 24 hours...")
time.sleep(WAIT_24_HOURS_SECONDS)
continue
with ThreadPoolExecutor(max_workers=EMAILS_PER_BATCH) as executor:
futures = []
for email in emails[daily_count : daily_count + EMAILS_PER_BATCH]:
futures.append(executor.submit(send_email, email))
for future in as_completed(futures):
try:
future.result()
except Exception as e:
print(f"β οΈ Error occurred: {e}. Retrying after delay...")
time.sleep(RETRY_DELAY_SECONDS)
time.sleep(BATCH_DELAY_SECONDS)
# π Run the Program
if __name__ == "__main__":
print("π Starting bulk email sender...")
bulk_send_emails()