bootstrap not working for django send_mail

Viewed 793

In my views.py I have the following code

def cpio(request):
    mainDict9=['hemant','jay','yash','Hari']
    args={'mainDict9':mainDict9,}

    msg_html =render_to_string('/home/user/infracom2/JournalContent/templates/JournalContent/test1.html', 
    {'mainDict9':mainDict9,})

   from_email = 'gorantl.chowdary@ak-enterprise.com'
   to_email = ['gorantla.chowdary@ak-enterprise.com']
   subject="TESTING MAIL"   
   send_mail('email title',subject,from_email,to_email,html_message=msg_html,)

return render(request,'JournalContent/test1.html',args)

In my test1.html I have my following code

<!DOCTYPE html>
    <html>
        <table class="table table-bordered">
        <thead>
            <tr>
                <th scope="col" class="table-secondary"><center>Modified Binaries/components</center></th>
                <th scope="col" class="table-secondary"><center>CRs</center></th>
           </tr>
        </thead>
        <tbody>
            {% for king in mainDict9 %}
            <tr>
                <td style="width: 10px;" class="table-active">{{ king }}</td>
                <td style="width: 10px;" class="table-active"></td>         
           </tr>
           {% endfor %}
       </tbody>
  </table>
</html>

The problem is in my GUI the bootstrap code is working fine but when I send the content in mail bootstrap functions are not applying

2 Answers

You should to put css code inside your template. Because most email client will block extra link to avoid execution of malicious code.

For example:

 <style>
  body {background-color: powderblue;}
  h1 {color: red;}
  p {color: blue;}
</style> 

You can not use bootstrap class for send_mail function because you need bootstrap CSS and js for the bootstrap classes it will work fine only and only when that CSS and js are attached with the template. But when you pass it into send_mail there were no CSS and js in the mail so it will not load the bootstrap classes over there.

You can only use a simple Html tag.

And other things might be if you used cdn of bootstrap CSS and js in send_mail template then might be it will be applied.

Related