By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 email_send.py

View raw Download
text/x-script.python • 891 B
Python script, ASCII text executable
        
            
1
import config
2
from jinja2 import Environment, FileSystemLoader, select_autoescape
3
import smtplib
4
from email.mime.multipart import MIMEMultipart
5
from email.mime.text import MIMEText
6
7
8
def make_multipart_message(subject, sender, receiver, template, **kwargs):
9
env_html = Environment(
10
loader=FileSystemLoader("email_templates"),
11
autoescape=select_autoescape(["html", "xml"])
12
)
13
env_plain = Environment(loader=FileSystemLoader("email_templates"))
14
15
message = MIMEMultipart("alternative")
16
message["Subject"] = subject
17
message["From"] = sender
18
message["To"] = receiver
19
20
text = MIMEText(env_html.get_template(template+".txt").render(**kwargs, config=config), "plain")
21
html = MIMEText(env_html.get_template(template+".html").render(**kwargs, config=config), "html")
22
23
message.attach(text)
24
message.attach(html)
25
26
return message.as_string()
27