-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
92 lines (74 loc) · 3.08 KB
/
Copy pathmain.py
File metadata and controls
92 lines (74 loc) · 3.08 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
import logging
import argparse
from src.config import settings
from src.utils.logger import setup_logger
from src.domain.email import EmailContent
from src.services.smtp_sender import EmailService
from src.services.rate_limiter import RateLimiter
from src.services.excel_loader import ExcelLoader
from src.services.html_renderer import TemplateService
from src.services.sent_logger import SentEmailLogger
def main():
# Configurar logging
setup_logger()
logger = logging.getLogger("main")
# Argumentos de línea de comandos
parser = argparse.ArgumentParser(description="Send bulk emails from Excel.")
parser.add_argument("--dry-run", action="store_true", help="Run without sending actual emails")
args = parser.parse_args()
excel_path = settings.DATA_DIR / "recipients.xlsx"
logger.info("Starting email sender...")
logger.info(f"Loading recipients from {excel_path}...")
# Cargar destinatarios del Excel
try:
excel_loader = ExcelLoader(excel_path)
recipients = excel_loader.load_recipients()
logger.info(f"Loaded {len(recipients)} recipients.")
except Exception as e:
logger.critical(f"Failed to load recipients: {e}")
return
# Inicializar servicios
rate_limiter = RateLimiter(
max_per_hour=settings.MAX_EMAILS_PER_HOUR,
max_per_interval=settings.MAX_EMAILS_PER_INTERVAL,
interval_minutes=settings.INTERVAL_MINUTES,
jitter_min=settings.JITTER_MIN_SECONDS,
jitter_max=settings.JITTER_MAX_SECONDS,
enable_jitter=settings.ENABLE_JITTER
)
email_service = EmailService(rate_limiter)
template_service = TemplateService(settings.TEMPLATES_DIR)
# Logger de correos enviados
sent_log_path = settings.DATA_DIR / "correos_enviados.xlsx"
sent_logger = SentEmailLogger(sent_log_path)
logger.info(f"Sent emails will be logged to: {sent_log_path}")
# Procesar cada destinatario
success_count = 0
fail_count = 0
for recipient in recipients:
try:
logger.info(f"Processing {recipient.email}...")
# Datos para personalizar la plantilla
context = {
"name": recipient.name,
"company": recipient.company
}
body_html = template_service.render("invitation.html", context)
email_content = EmailContent(
subject=f"Invitación para { recipient.company }",
body_html=body_html,
to_email=recipient.email
)
if args.dry_run:
logger.info(f"[DRY RUN] Would send email to {recipient.email}")
success_count += 1
else:
email_service.send_email(email_content)
sent_logger.log_sent_email(recipient.company, recipient.name, recipient.email)
success_count += 1
except Exception as e:
logger.error(f"Error processing {recipient.email}: {e}")
fail_count += 1
logger.info(f"Completed. Success: {success_count}, Failed: {fail_count}")
if __name__ == "__main__":
main()