Skip to content

Commit 75b01ba

Browse files
authored
Update main.py
1 parent f90491a commit 75b01ba

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

src/main.py

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@
2222
from email_content import html_content
2323
from utils import read_email_list
2424
from email_extractor import extracted_emails
25-
from PyQt6.QtCore import QThread
25+
from PyQt6.QtCore import QThread, pyqtSignal, QObject
26+
2627

2728

2829

2930

3031
class EmailAutomationApp(QMainWindow):
32+
log_signal = pyqtSignal(str)
3133
def __init__(self):
3234
super().__init__()
33-
35+
self.log_signal.connect(self.update_log_output) # اتصال سیگنال به متد
3436
self.setWindowTitle("Email Automation Tool")
3537
self.setGeometry(100, 100, 800, 600)
3638

@@ -42,7 +44,11 @@ def __init__(self):
4244
self.create_link_extractor_tab()
4345
self.create_email_extractor_tab() # New tab for extracting emails from URLs
4446
self.create_email_sender_tab()
45-
# =======================end tab===============================
47+
#============================signals=======================
48+
def update_log_output(self, message):
49+
self.log_output.append(message) # به روز رسانی لاگ در ترد اصلی
50+
# ==========================================================
51+
# =======================end tab===============================
4652
# ================== start tab ==================
4753
def create_link_extractor_tab(self):
4854
"""Tab for extracting links from search results"""
@@ -335,6 +341,7 @@ def save_email_to_file(self):
335341
QMessageBox.critical(self, "Error", f"An error occurred while saving the file: {e}")
336342
# ===========================end extract email ==================
337343
# ======================== start email sending======================
344+
338345
def browse_file(self):
339346
"""Open a file dialog to select a file."""
340347
file_name, _ = QFileDialog.getOpenFileName(self, "Select File", "", "All Files (*)")
@@ -346,10 +353,12 @@ def start_email_sending(self):
346353
sender_email = self.sender_email_input.text()
347354
password = self.password_input.text()
348355
email_file = self.email_file_input.text()
349-
attachment_path = self.attachment_input.text() # گرفتن مسیر فایل از QLineEdit
350-
351-
if sender_email and password and email_file and attachment_path:
352-
threading.Thread(target=self.send_emails, args=(sender_email, password, email_file, attachment_path), daemon=True).start()
356+
attachment_path = self.attachment_input.text() # Get the file path from QLineEdit
357+
attachment_path if attachment_path else None
358+
if sender_email and password and email_file:
359+
360+
attachment_path = attachment_path if attachment_path else None
361+
threading.Thread(target=self.send_emails, args=(sender_email, password, email_file, attachment_path), daemon=False).start()
353362
else:
354363
QMessageBox.warning(self, "Input Error", "Please fill in all fields.")
355364

@@ -358,27 +367,38 @@ def send_emails(self, sender_email, password, email_file, attachment_path):
358367
"""Function to send emails to extracted email addresses with attachment"""
359368
asyncio.run(self.send_emails_async(sender_email, password, email_file, attachment_path))
360369

361-
362370
async def send_emails_async(self, sender_email, password, email_file, attachment_path):
363371
"""Asynchronous email sending with attachment"""
364372
try:
365373
email_list = await read_email_list(email_file)
366-
367-
tasks = []
368374
for receiver_email in email_list:
369-
tasks.append(send_email(
370-
receiver_email=receiver_email,
371-
html_content=html_content,
372-
sender_email=sender_email,
373-
password=password,
374-
attachment_path=attachment_path # استفاده از مسیر جدید
375-
))
376-
await asyncio.gather(*tasks) # Send emails concurrently
375+
await self.send_email_with_retry(receiver_email, sender_email, password, attachment_path)
376+
await asyncio.sleep(10) # Delay of 10 seconds between each email to avoid rate limits
377+
377378
self.log_output.append("Emails sent successfully!")
378379
QMessageBox.information(self, "Success", "Emails sent successfully!")
379380
except Exception as e:
380381
QMessageBox.critical(self, "Error", f"An error occurred while sending emails: {e}")
381382

383+
async def send_email_with_retry(self, receiver_email, sender_email, password, attachment_path=None, retries=3, delay=5):
384+
"""Send email with retry mechanism"""
385+
for attempt in range(retries):
386+
try:
387+
await send_email(
388+
receiver_email=receiver_email,
389+
html_content=html_content,
390+
sender_email=sender_email,
391+
password=password,
392+
attachment_path=attachment_path
393+
)
394+
self.log_signal.emit(f"Email sent successfully to {receiver_email}!")
395+
break # If successful, exit the retry loop
396+
except Exception as e:
397+
self.log_signal.emit(f"Failed to send email to {receiver_email}: {e}")
398+
if attempt < retries - 1:
399+
await asyncio.sleep(delay) # Wait before retrying
400+
else:
401+
self.log_signal.emit(f"Giving up on sending email to {receiver_email} after {retries} attempts.") # استفاده از سیگنال
382402
# ======================== end email sending =====================
383403

384404

0 commit comments

Comments
 (0)