lavr / python-emails

Modern python library for emails
http://python-emails.readthedocs.org
Other
405 stars 54 forks source link

Unable to inject variables into html template #137

Closed manobeeslaar closed 5 years ago

manobeeslaar commented 5 years ago

Hi There,

I have search the internet for a solution, but unable to use a template and inject variables into it from python to the HTML template: Example: In the HTML template:

Hi {name}

Unable to add the name = name from python code.

Thank you and a great piece of software app.

Regards,

lavr commented 5 years ago

Hi! Can you show a piece of code?

manobeeslaar commented 5 years ago

Thank you! PYTHON section:

store = 'ALL'
name = 'Mano'

message = emails.html(html=open('email_template.html'),
                     subject='SOH AGING - '+store,
                     mail_from=('URB SOH Management', 'mano@urb.co.za'))

message.attach(data=open('SOHAgingPerStore.xlsx', 'rb'), filename='SOHAgingPerStore.xlsx')
message.attach(data=open('logo.png', 'rb'), filename='logo.png')
message.attachments['logo.png'].is_inline = True
message.transformer.synchronize_inline_images()
message.transformer.save()

HTML: `

Y'ello {name},

</br>
  <p>Attached you will find the store's stock aging:</p>
</br>
  <p>Please take note of the below colors:</p>
</br>
<ul>
  <li>0 - 14 days WHITE</li>
  <li style="color: #C4D79B">15 - 27 days GREEEN</li>
  <li style="color: #DA9694">28 - 44 days LIGHT RED</li>
  <li style="color: #C00000">45+ days</li>
</ul>


`

Hope this gives more insight! Thank you,

lavr commented 5 years ago

You should use template object and pass variables to send method as shown in section "Use templates" in docs: image

manobeeslaar commented 5 years ago

Understood, but how do you render the template from a file as per code: message = emails.html(html=open('email_template.html'),

I have tried to replace the open with the T -> html=T('email_template.html'), but this does not work. Note that the html code lives in a separate file. Apologies for the questions, but I am trying to wrap my head around this. Thank you!

lavr commented 5 years ago

T(open('email_template.html').read())

manobeeslaar commented 5 years ago

Thank you, closing ticket!