Forums

something like render_template to send an email?

My project is coming along nicely!

I now have my SMS Log working fairly well, and I've built the blueprint for the actual chat bot. Next up, I'd like to figure out how to send an email with the log and clear all entries from that number at the end of a conversation.

I've figured out how to send the email using send_email(message) where message is any text I want to assign. I can also assign all items in each database row to a variable, and to clear the table. I haven't yet, but I'm pretty sure I can clear just the matching rows in the table. What I CAN'T figure out is how to send an email that intelligently parses those variables into an html format.

Apparently render_template is designed to render output for flask, because trying that popped up a bunch of error codes I didn't fully understand. I'd like to do something like:

message = render_template("email_template.html", smsLog = filtered_SMS_log)
send_email(message)

where the email template filters through the rows in the log. I feel like I won't get away with it, but I'd like to avoid having to install new stuff into my virtualenv if possible (but, obviously, if it's the only way...). Topics on this online are scattered and I'm not sure where to go.

How do I do this?

What errors did you get when you used the normal render_template function?

Apparently something unrelated! I tried again with a basic page and template, and it worked. Kind of.

The email I got includes the HTML, but not rendered. Like, it actually says the html stuff:

<html>
    <head>
        <title>Emaili test page</title>
    </head>

    <body>
        <div name = "table">
        <p>
            Yay it worked!
        </p>
        <p>
            <table name = "table" value = "table">
                <tr>
                    <td>
                        Name
                    </td>
                    <td>
                        Number
                    </td>
                    <td>
                        Statement
                    </td>
                </tr>
            </table>
        <p>
            The template worked!
        </p>
</body>
</html>

This Stack Overflow answer has some sample code that shows how to set the HTML and text versions of a message (it's good practice to send both versions so that people with non-HTML mail readers -- they still exist! -- can see something useful).

I tried just using smtplib's MIMEText feature after a little more research. I tried with a fairly basic template (above), and got the following error. I'm assuming there's something I'm missing?

OSError: [Errno 90] Message too long

You're logging something (possibly using print) that is really long and so our logging system rejects it. It only cares about the length of individual log messages, so try to shorten any long log messages you're sending.

Ohhhhh. That makes sense. I got past it but not by intentionally changing anything in the log. I did have some print (object) statements out there that got fairly lengthy.