kootenpv / yagmail

Send email in Python conveniently for gmail using yagmail
MIT License
2.66k stars 265 forks source link

How to send html file as attachment? #169

Open smjure opened 4 years ago

smjure commented 4 years ago

First of all thank you for the great app.

I have to send .html reports automatically. Idea is to send it as either attachment or inline. I tried several things:

yag=yagmail.SMTP('user@gmail.com','PASS')
contents = ["//path/to/file.html"]
yag.send('xxx@gmail.com', f'Subject',contents)

and

yag.send('xxx@gmail.com', f'Subject',yagmail.inline("//path/to/file.html"))

and

yag.send('xxx@gmail.com', f'Subject',yagmail.raw("//path/to/file.html")) 

If I sent it as html attachment or inline, this is the result where I cannot see the html content. Selection_402

If I change ".html" file name, to e.g. ".tt", the file is successfully attached. So, how can I attache a .html file without renaming it?

MLpranav commented 4 years ago

If you want to send it as inline, just write the HTML code directly in contents. contents = "<h1>This is a big title!</h1>"

If you want to attach HTML files inline, write a function to extract the code from it and pass it to contents as a string.