0

I'm trying to customise the message which I want to send to a gmail, like you may have seen some emails having nice layout, buttons and images. I want customise my message like that. But I'm not getting how to do it in django.

Can anyone guide me how to do it? I'd appreciate some suggestions.

Thank you.

Rob
  • 14,746
  • 28
  • 47
  • 65
Avatar
  • 13
  • 3

2 Answers2

0

In your views.py you can do something like this

from os import error
import smtplib
import  csv 
from email.mime.text import MIMEText
def mass_mailing_script():

    with open('receipients.csv', 'r') as csvfile:
        reader = csv.reader(csvfile)
    
    try:
        count = 0
        for line in reader:
            if(len(line) < 1):
                continue
            email_send = line[0]
            subject ="Your message for the email" 
            html_body = open("text.html")
            msg = MIMEText(html_body.read(), "html")
            msg['From'] = email_user
            msg['To'] = email_send
            msg['Subject'] = subject
            text = msg.as_string()
            server.login(email_user, password)
            server.sendmail(email_user, email_send, text)
            count +=1
            print(str(count) + ". Sent Email to: " + email_send)
                
    except:
        print("error")

then in your template in this case it would be text.htmml

<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:v="urn:schemas-microsoft-com:vml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;" />
    <!--[if !mso]-->
    <!-- -->
    <link href='https://fonts.googleapis.com/css?family=Work+Sans:300,400,500,600,700' rel="stylesheet">
    <link href='https://fonts.googleapis.com/css?family=Quicksand:300,400,700' rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.0-2/js/brands.min.js" integrity="sha256-OjapPU7ZR5ESNNSe21vgHeQCya5MZT4Y3X94x0JrP90=" crossorigin="anonymous"></script>
    <!-- <![endif]-->

    <title>MPT 1.0 Results</title>

    <style type="text/css">
        body {{
            width: 100%;
            background-color: #ffffff;
            margin: 0;
            padding: 0;
            -webkit-font-smoothing: antialiased;
            mso-margin-top-alt: 0px;
            mso-margin-bottom-alt: 0px;
            mso-padding-alt: 0px 0px 0px 0px;
        }}

        p,
        h1,
        h2,
        h3,
        h4 {{
            margin-top: 0;
            margin-bottom: 0;
            padding-top: 0;
            padding-bottom: 0;
        }}

        span.preheader {{
            display: none;
            font-size: 1px;
        }}

        html {{
            width: 100%;
        }}

        table {{
            font-size: 14px;
            border: 0;
        }}

        /* ----------- responsivity ----------- */

        @media only screen and (max-width: 640px) {{

            /*------ top header ------ */
            .main-header {{
                font-size: 20px !important;
            }}

            .main-section-header {{
                font-size: 28px !important;
            }}

            .show {{
                display: block !important;
            }}

            .hide {{
                display: none !important;
            }}

            .align-center {{
                text-align: center !important;
            }}

            .no-bg {{
                background: none !important;
            }}

            /*----- main image -------*/
            .main-image img {{
                width: 440px !important;
                height: auto !important;
            }}

            /* ====== divider ====== */
            .divider img {{
                width: 440px !important;
            }}

            /*-------- container --------*/
            .container590 {{
                width: 440px !important;
            }}

            .container580 {{
                width: 400px !important;
            }}

            .main-button {{
                width: 220px !important;
            }}

            /*-------- secions ----------*/
            .section-img img {{
                width: 320px !important;
                height: auto !important;
            }}

            .team-img img {{
                width: 100% !important;
                height: auto !important;
            }}
        }}

        @media only screen and (max-width: 479px) {{

            /*------ top header ------ */
            .main-header {{
                font-size: 18px !important;
            }}

            .main-section-header {{
                font-size: 26px !important;
            }}

            /* ====== divider ====== */
            .divider img {{
                width: 280px !important;
            }}

            /*-------- container --------*/
            .container590 {{
                width: 280px !important;
            }}

            .container590 {{
                width: 280px !important;
            }}

            .container580 {{
                width: 260px !important;
            }}

            /*-------- secions ----------*/
            .section-img img {{
                width: 280px !important;
                height: auto !important;
            }}
        }}
    </style>
    <!-- [if gte mso 9]><style type=”text/css”>
        body {{
        font-family: arial, sans-serif!important;
        }}
        </style>
    <![endif]-->
</head>


<body class="respond" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
    <!-- pre-header -->
    <table style="display:none!important;">
        <tr>
            <td>
                <div
                    style="overflow:hidden;display:none;font-size:1px;color:#ffffff;line-height:1px;font-family:Arial;maxheight:0px;max-width:0px;opacity:0;">
                   
                </div>
            </td>
        </tr>
    </table>
    <!-- pre-header end -->
    <!-- header -->
    <table border="0" width="100%" cellpadding="0" cellspacing="0" bgcolor="ffffff">

        <tr>
            <td align="center">
                <table border="0" align="center" width="590" cellpadding="0" cellspacing="0" class="container590">

                    <tr>
                        <td height="25" style="font-size: 25px; line-height: 25px;">&nbsp;</td>
                    </tr>

                    <tr>
                        <td align="center">

                            <table border="0" align="center" width="590" cellpadding="0" cellspacing="0"
                                class="container590">

                                <tr>
                                    <td align="center" height="70" style="height:70px;">
                                        <a href=""
                                            style="display: block; border-style: none !important; border: 0 !important;"><img
                                                width="300" border="0" style="display: block; width: 300px;"
                                                src="https://d33wubrfki0l68.cloudfront.net/981a1f42efb275c0eb1bf3d22cbcd4feba6cf195/d5c72/assets/img/kit/dsclogo.png" alt="" /></a>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>

                    <tr>
                        <td height="25" style="font-size: 25px; line-height: 25px;">&nbsp;</td>
                    </tr>

                </table>
            </td>
        </tr>
    </table>
    <!-- end header -->

    <!-- big image section -->
    <table border="0" width="100%" cellpadding="0" cellspacing="0" bgcolor="ffffff" class="bg_color">

        <tr>
            <td align="center">
                <table border="0" align="center" width="590" cellpadding="0" cellspacing="0" class="container590">
                    
                    <tr>
                        <td height="20" style="font-size: 20px; line-height: 20px;">&nbsp;</td>
                    </tr>
                    <tr>
                        <td align="center"
                            style="color: #343434; font-size: 24px; font-family: Quicksand, Calibri, sans-serif; font-weight:700;letter-spacing: 3px; line-height: 35px;"
                            class="main-header">


                            <div style="line-height: 35px">

                                HEY <span style="color: #5caad2;">{name}</span>

                            </div>
                        </td>
                    </tr>

                    <tr>
                        <td height="10" style="font-size: 10px; line-height: 10px;">&nbsp;</td>
                    </tr>

                    <tr>
                        <td align="center">
                            <table border="0" width="40" align="center" cellpadding="0" cellspacing="0"
                                bgcolor="eeeeee">
                                <tr>
                                    <td height="2" style="font-size: 2px; line-height: 2px;">&nbsp;</td>
                                </tr>
                            </table>
                        </td>
                    </tr>

                    <tr>
                        <td height="20" style="font-size: 20px; line-height: 20px;">&nbsp;</td>
                    </tr>

                    <tr>
                        <td align="center">
                            <table border="0" width="400" align="center" cellpadding="0" cellspacing="0"
                                class="container590">
                                <tr>
                                    <td align="center"
                                        style="color: #888888; font-size: 16px; font-family: 'Work Sans', Calibri, sans-serif; line-height: 24px;">


                                        <div style="line-height: 24px">

                                            Kudos on your excellent success and good luck for more progress. Don't get disheartened if your scores aren't satisfying. You'll get better with more practice!

                                            If you want to know where you stand, we have released the leaderboard below.
                                        </div>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>

                    <tr>
                        <td height="25" style="font-size: 25px; line-height: 25px;">&nbsp;</td>
                    </tr>

                    <tr>
                        <td align="center">
                            <table border="0" align="center" width="160" cellpadding="0" cellspacing="0"
                                bgcolor="5caad2">

                                <tr>
                                    <td height="10" style="font-size: 10px; line-height: 10px;">&nbsp;</td>
                                </tr>

                                <tr>
                                    <td align="center"
                                        style="color: #ffffff; font-size: 14px; font-family: 'Work Sans', Calibri, sans-serif; line-height: 26px;">


                                        <div style="line-height: 26px;">
                                            <a href="#" style="color: #ffffff; text-decoration: none;">RESULTS</a>
                                        </div>
                                    </td>
                                </tr>

                                <tr>
                                    <td height="10" style="font-size: 10px; line-height: 10px;">&nbsp;</td>
                                </tr>

                            </table>
                        </td>
                    </tr>

                    <tr>
                        <td align="center">
                            <table border="0" width="400" align="center" cellpadding="0" cellspacing="0"
                                class="container590">
                                <tr>
                                    <td align="center"
                                        style="color: #888888; font-size: 16px; font-family: 'Work Sans', Calibri, sans-serif; line-height: 24px;">


                                        <div style="line-height: 24px">
                                            <br>
                                            Learn and analyze from your mistakes and see where you went wrong <u><a href="https://drive.google.com/file/d/1ao-9ncCpSUOx-YkdI45OLLFiVWwLnO_J/view?usp=sharing" target="_blank">here</a></u>.<br>
                                            Also, find your personalized analysis report <u><a href="{link}" target="_blank">here</a></u>.<br><br>

                                            If you haven't filled out the <b><u><a href="#" target="_blank">feedback form</a></u></b> yet, please do it now. It will be really useful for us to conduct the upcoming tests. <br><br>

                                            <i>P.S Follow us on Instagram at dscjssstu for Quick Updates regarding MPT 2.0 and further events.</i>
                                        </div>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>


                </table>

            </td>
        </tr>

    </table>
    <!-- end section -->

    <!-- contact section -->
    <table border="0" width="100%" cellpadding="0" cellspacing="0" bgcolor="ffffff" class="bg_color">

        <tr class="hide">
            <td height="25" style="font-size: 25px; line-height: 25px;">&nbsp;</td>
        </tr>
        <tr>
            <td height="40" style="font-size: 40px; line-height: 40px;">&nbsp;</td>
        </tr>

        <tr>
            <td height="60" style="border-top: 1px solid #e0e0e0;font-size: 60px; line-height: 60px;">&nbsp;</td>
        </tr>

        <tr>
            <td align="center">
                <table border="0" align="center" width="590" cellpadding="0" cellspacing="0"
                    class="container590 bg_color">

                    <tr>
                        <td>
                            <table border="0" width="300" align="left" cellpadding="0" cellspacing="0"
                                style="border-collapse:collapse; mso-table-lspace:0pt; mso-table-rspace:0pt;"
                                class="container590">

                                <tr>
                                    <!-- logo -->
                                    <td align="left">
                                        <a href=""
                                            style="display: block; border-style: none !important; border: 0 !important;"><img
                                                width="100" border="0" style="display: block; width: 100px;"
                                                src="https://d33wubrfki0l68.cloudfront.net/58f92715e27dd6332fc56c0acde5d772734ec17d/b31ed/assets/img/logos/dsc_header.png" alt="" /></a>
                                    </td>
                                </tr>

                                <tr>
                                    <td height="25" style="font-size: 25px; line-height: 25px;">&nbsp;</td>
                                </tr>

                                <tr>
                                    <td align="left"
                                        style="color: #888888; font-size: 14px; font-family: 'Work Sans', Calibri, sans-serif; line-height: 23px;"
                                        class="text_color">
                                        <div
                                            style="color: #333333; font-size: 14px; font-family: 'Work Sans', Calibri, sans-serif; font-weight: 600; mso-line-height-rule: exactly; line-height: 23px;">

                                            Email us: <br /> <a href="mailto:dsc.jssstu@gmail.com"
                                                style="color: #888888; font-size: 14px; font-family: 'Hind Siliguri', Calibri, Sans-serif; font-weight: 400;">dsc.jssstu@gmail.com</a>

                                        </div>
                                    </td>
                                </tr>

                            </table>

                            <table border="0" width="2" align="left" cellpadding="0" cellspacing="0"
                                style="border-collapse:collapse; mso-table-lspace:0pt; mso-table-rspace:0pt;"
                                class="container590">
                                <tr>
                                    <td width="2" height="10" style="font-size: 10px; line-height: 10px;"></td>
                                </tr>
                            </table>

                            <table border="0" width="200" align="right" cellpadding="0" cellspacing="0"
                                style="border-collapse:collapse; mso-table-lspace:0pt; mso-table-rspace:0pt;"
                                class="container590">

                                <tr>
                                    <td class="hide" height="45" style="font-size: 45px; line-height: 45px;">&nbsp;</td>
                                </tr>



                                <tr>
                                    <td height="15" style="font-size: 15px; line-height: 15px;">&nbsp;</td>
                                </tr>

                                <tr>
                                    <td>
                                        <table border="0" align="right" cellpadding="0" cellspacing="0">
                                            <tr>
                                                <td>
                                                    <a href="https://www.instagram.com/dscjssstu/"
                                                        style="display: block; border-style: none !important; border: 0 !important;"><img width="24" border="0" style="display: block;" src="https://i.imgur.com/JGhQe6S.png" alt=""></a>
                                                </td>
                                                <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                                                <td>
                                                    <a href="https://github.com/dsc-jssstu"
                                                        style="display: block; border-style: none !important; border: 0 !important;"><img width="24" border="0" style="display: block;" src="https://i.imgur.com/RQFfyuT.png" alt=""></a>
                                                </td>
                                                <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                                                <td>
                                                    <a href="https://www.facebook.com/dsc.jsstu.3"
                                                        style="display: block; border-style: none !important; border: 0 !important;"><img width="24" border="0" style="display: block;" src="https://i.imgur.com/MoaKQkE.png" alt=""></a>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>

                            </table>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>

        <tr>
            <td height="60" style="font-size: 60px; line-height: 60px;">&nbsp;</td>
        </tr>

    </table>
    <!-- end section -->

</body>

</html>
Mugoya Dihfahsih
  • 425
  • 3
  • 11
0

this is the way to send your desired html page via email . implement in views.py :

 html_user_message = render_to_string(
        'text.html',  # your html path
        {
           # these are contexts to send to template 
           'email': user.email,
           ....
            
        }
    )
    send_mail(
        "Login Attempt",  # Title
        '-',
        [],
        [user.email],    # recipient
        fail_silently=False,  # no errors on failure
        html_message=html_user_message) # the html template to send via email 

and in the end you need to set smpt and email config in your settings.py

hope you find it helpful.