P390
P390

Reputation: 65

Send dynamic html file on mail

I tried sending html on mail using django-mailgun but it sends whole HTML contain rather than just rendered HTML table. What I want, is to send rendered table on mail.

Here is my code:

def send_email():
  dict = [....]        #it contains list of data that I want to render 
  html_message = render_to_string('email.html', {'data': dict})
  msg = EmailMessage('Hello', html_message,'[email protected]', ['[email protected]'])
  msg.content_subtype = "html"  
  msg.send() 

Here's my HTML table:

  <table style=" border:1px solid black; border-top:1px solid transparent; border-left:1px solid transparent; border-right:1px solid transparent; width:80%;  border-collapse: collapse; font-size:95%; margin-left:10%; margin-right:10%; margin-top:5%; ">
    <thead>
        <tr><th colspan=5  style=" border-bottom:1px solid black; border-right:none; border-left:none; text-align:center; padding:4x"><h4>Activity List</h4></th></tr>
        <tr style="border:1px solid black">
            <th style="border:1px solid black">col 1</th>
            <th style="border:1px solid black">col 2</th>
            <th style="border:1px solid black">col 3</th>
            <th style="border:1px solid black">col 4</th>
            <th style="border:1px solid black">col 5</th>
        </tr>
    </thead>
    <body>
        {% for item in data %}
        <tr>
            <td style="border:1px solid; text-align:right; padding: 4px">{{ item.data1 }}</td>
            <td style="border:1px solid; text-align:right; padding: 4px">{{ item.data2 }}</td>
            <td style="border:1px solid; text-align:right; padding: 4px">{{ item.data3 }}</td>
            <td style="border:1px solid; text-align:right; padding: 4px">{{ item.data4 }}</td>
            <td style="border:1px solid; text-align:right; padding: 4px">{{ item.data5 }}</td>
        </tr>
        {% endfor %}
    </body>

I am getting this on my mail:

<table style=" border:1px solid black; border-top:1px solid transparent; border-left:1px solid transparent; border-right:1px solid transparent; width:80%;  border-collapse: collapse; font-size:95%; margin-left:10%; margin-right:10%; margin-top:5%; ">
    <thead>
        <tr><th colspan=5  style=" border-bottom:1px solid black; border-right:none; border-left:none; text-align:center; padding:4x"><h4>Activity List</h4></th></tr>
        <tr style="border:1px solid black">
            <th style="border:1px solid black">col 1</th>
            <th style="border:1px solid black">col 2</th>
            <th style="border:1px solid black">col 3</th>
            <th style="border:1px solid black">col 4</th>
            <th style="border:1px solid black">col 5</th>
        </tr>
    </thead>
    <tbody>

        <tr>
            <td style="border:1px solid; text-align:right; padding: 4px">data 11</td>
            <td style="border:1px solid; text-align:right; padding: 4px">data 12</td>
            <td style="border:1px solid; text-align:right; padding: 4px">data 13</td>
            <td style="border:1px solid; text-align:right; padding: 4px">data 14</td>
            <td style="border:1px solid; text-align:right; padding: 4px">data 15</td>
        </tr>

        <tr>
            <td style="border:1px solid; text-align:right; padding: 4px">data 21</td>
            <td style="border:1px solid; text-align:right; padding: 4px">data 22</td>
            <td style="border:1px solid; text-align:right; padding: 4px">data 23</td>
            <td style="border:1px solid; text-align:right; padding: 4px">data 24</td>
            <td style="border:1px solid; text-align:right; padding: 4px">data 25</td>
        </tr>

        </tbody>

</table>

Upvotes: 2

Views: 439

Answers (1)

Bipin Raut
Bipin Raut

Reputation: 106

Using Django’s email library, you can do this using the EmailMultiAlternatives class.

from django.core.mail import EmailMultiAlternatives

subject, from_email, to = 'hello', '[email protected]', '[email protected]'
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()

It worked for me. Try this. Refer: https://docs.djangoproject.com/en/1.8/topics/email/#sending-alternative-content-types

Upvotes: 3

Related Questions