as

🧩 Syntax:
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'''\
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Document</title>
        </head>
        <body style="margin: auto;
                    background-color: #fafafa;
                    justify-content: center;
                    font-family: Work Sans, sans-serif;">
            <div style="margin: auto;
                    height: 530px;
                    position: relative;
                    top: 35px;
                    background: #f7f7f7 0% 0% no-repeat padding-box;
                    width: 770px;
                    padding: 10px;">
                <div style="text-align: center;">
                    <img style="position: relative; left: 203px; width: 150px; height: 100px;" src="https://storage.googleapis.com/smartx-public-files/aptpath-logo.png">;
                    <img src="https://storage.googleapis.com/smartx-public-files/aptpath-logo2.png"; style="width: 150px; height: 100px; position: relative; left: 170px; top: 13px;">
                </div>

                <div style="width: 750px">
                    <div style="position:relative;width:60%;float: left;">
                        <div style="position: relative;
                                    top: 53px;
                                    left: 42px;
                                    font-family: Work Sans, sans-serif;">
                            <p style="text-align: left;
                                    letter-spacing: -0.28px;
                                    color: #383838;
                                    opacity: 1;
                                    font-size: 20px;
                                    font-weight: 600;
                                    margin-bottom: 17px;
                                    margin-top: 0px;
                                    font-family: Work Sans, sans-serif;">Dear {name},</p>
                            <p style="text-align: left;
                                    letter-spacing: -0.21px;
                                    color: #383838;
                                    opacity: 1;
                                    line-height: 27.2px;
                                    font-size: 20px;
                                    font-weight: medium;
                                    font-family: Work Sans, sans-serif;
                                    margin-top: 38px;">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. </p>
                                        <p
                                        style="text-align: left;
                                    letter-spacing: -0.21px;
                                    color: #383838;
                                    opacity: 1;
                                    line-height: 27.2px;
                                    font-size: 20px;
                                    font-weight: medium;
                                    font-family: Work Sans, sans-serif;
                                    margin-top: 38px;">Please use the following link to access our application on your mobile
                                        device:</p>
                            <div class="row">
                                <div class="col-sm-6">
                                    <a style="display: block;
                                            width: 115px;
                                            height: 25px;
                                            background: #FF7F00;
                                            padding: 10px;
                                            text-align: center;
                                            border-radius: 5px;
                                            color: white;
                                            text-decoration:none;
                                            font-family: Work Sans, sans-serif;
                                            line-height: 25px;" href="{apt_path}">Login/Register</a>

                                    <div style="text-align: left;
                                                font: normal normal medium 15px/18px Work Sans;
                                                letter-spacing: -0.21px;
                                                color: #383838;
                                                font-size: 15px;
                                                opacity: 1;
                                                font-weight: normal;
                                                line-height: 32px;
                                                font-family: Work Sans, sans-serif;
                                                margin-top: 33px;">
                                        <p>Regards, <br> Team Aptpath</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div style="position:relative;width:28%;float: right;right: -251px;top: 73px;">
                        <div class="man col-sm-6">
                            <img style="width: 289px;
                                        height: 370px;
                                        bottom: 100px;
                                        position: relative;
                                        float: right;
                                        margin-top: 0px;
                                        right: 118px;" src="https://storage.googleapis.com/smartx-public-files/man.jpg">;
                        </div>
                    </div>
                </div>
            </div>
        </body>
        </html>
    '''
    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/&apos;

        # 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.')