from django.core.management.base import BaseCommand from email_project.settings import EMAIL_HOST_USER import openpyxl from django.core.mail import EmailMessage from decouple import config import os def generate_email_html(name, apt_path): html_content = f'''\ Document

Dear {name},

We are excited to inform you that our Aptpath web application is now available in a mobile/tablet Responsive web pages.Please take advantage of the opportunity to find jobs. New jobs are posted regularly.

Please use the following link to access our application on your mobile device:

Login/Register

Regards,
Team Aptpath

''' return html_content def send_email_with_template(name, recipient_email, apt_path): subject = 'Exciting News: Mobile Responsive web pages of AptPath is Available!' html_content = generate_email_html(name, apt_path) # from_email = config(SENDER_EMAIL) sender_email = os.getenv('SENDER_EMAIL') try: email = EmailMessage(subject, html_content, sender_email, [recipient_email]) email.content_subtype = 'html' # Set the content type to HTML email.send() print(f'Email sent successfully to {name} ({recipient_email}).') except Exception as e: print(f'Error sending email to {name} ({recipient_email}): {str(e)}') class Command(BaseCommand): help = 'Send emails to recipients from Excel sheet' def handle(self, *args, **kwargs): file_path = r'C:\Users\get it rent\OneDrive\Desktop\email-scripts\email_project\email_app\aptpath users emails.xlsx' workbook = openpyxl.load_workbook(file_path) sheet = workbook.active apt_path = 'https://beta.aptpath.in/' # Customize email subject # subject = 'Exciting News: Mobile Version of AptPath is Available now!' # Send email to each recipient with HTML content for row_index in range(2, sheet.max_row + 1): name = sheet.cell(row=row_index, column=3).value recipient_email = sheet.cell(row=row_index, column=2).value if name and recipient_email: send_email_with_template(name, recipient_email, apt_path) else: print(f'Skipping row {row_index} due to missing data.') print('Emails processed successfully.')