Skip to content

Instantly share code, notes, and snippets.

@laughingman7743
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save laughingman7743/3c9358606379389bc219 to your computer and use it in GitHub Desktop.

Select an option

Save laughingman7743/3c9358606379389bc219 to your computer and use it in GitHub Desktop.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="format-detection" content="telephone=no, date=no, address=no"/>
<title></title>
<style type="text/css">
#outlook a {padding:0;}
body{width:100% !important; -webkit-text-size-adjust:100%; -ms-text-size-adjust:100%; margin:0; padding:0;}
.ExternalClass {width:100%;}
.ExternalClass, .ExternalClass p, .ExternalClass span, .ExternalClass font, .ExternalClass td, .ExternalClass div {line-height: 100%;}
#backgroundTable {margin:0; padding:0; width:100% !important; line-height: 100% !important;}
img {outline:none; text-decoration:none; -ms-interpolation-mode: bicubic;}
a img {border:none;}
.image_fix {display:block;}
p {margin: 1em 0;}
h1, h2, h3, h4, h5, h6 {color: black !important;}
h1 a, h2 a, h3 a, h4 a, h5 a, h6 a {color: blue !important;}
h1 a:active, h2 a:active, h3 a:active, h4 a:active, h5 a:active, h6 a:active {color: red !important;}
h1 a:visited, h2 a:visited, h3 a:visited, h4 a:visited, h5 a:visited, h6 a:visited {color: purple !important;}
table td {border-collapse: collapse;}
table { border-collapse:collapse; mso-table-lspace:0pt; mso-table-rspace:0pt; }
a {color: orange;}
@media only screen and (max-device-width: 480px) {
a[href^="tel"], a[href^="sms"] {
text-decoration: none;
color: blue;
pointer-events: none;
cursor: default;
}
.mobile_link a[href^="tel"], .mobile_link a[href^="sms"] {
text-decoration: default;
color: orange !important;
pointer-events: auto;
cursor: default;
}
}
@media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
a[href^="tel"], a[href^="sms"] {
text-decoration: none;
color: blue;
pointer-events: none;
cursor: default;
}
.mobile_link a[href^="tel"], .mobile_link a[href^="sms"] {
text-decoration: default;
color: orange !important;
pointer-events: auto;
cursor: default;
}
}
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
/* Put your iPhone 4g styles in here */
}
/* Android targeting */
@media only screen and (-webkit-device-pixel-ratio:.75){
/* Put CSS for low density (ldpi) Android layouts in here */
}
@media only screen and (-webkit-device-pixel-ratio:1){
/* Put CSS for medium density (mdpi) Android layouts in here */
}
@media only screen and (-webkit-device-pixel-ratio:1.5){
/* Put CSS for high density (hdpi) Android layouts in here */
}
/* end Android targeting */
</style>
<!-- Targeting Windows Mobile -->
<!--[if IEMobile 7]>
<style type="text/css">
</style>
<![endif]-->
<!--[if gte mso 9]>
<style>
/* Target Outlook 2007 and 2010 */
</style>
<![endif]-->
</head>
<body style="padding: 0 10px 20px;">
<table cellpadding="0" cellspacing="0" border="0" id="backgroundTable">
<tr>
<td valign="top">
<p>
{{ message }}
</p>
</td>
</tr>
</table>
</body>
</html>
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from email import encoders
from email.header import Header
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
import smtplib
from jinja2.environment import Environment, Template
from jinja2.loaders import FileSystemLoader
import setting
class Mail(object):
def __init__(self, host, port, tls, user, password, msg):
self.host = host
self.port = port
self.tls = tls
self.user = user
self.password = password
self.msg = msg
def send(self, *args, **kwargs):
server = smtplib.SMTP(self.host, self.port)
server.ehlo()
if self.tls:
server.starttls()
server.ehlo()
if self.user and self.password:
server.login(self.user, self.password)
msg = self._create_message(*args, **kwargs)
server.sendmail(self.msg.from_addr,
list(set(self.msg.to_addr.split(',') +
(self.msg.cc_addr.split(',') if self.msg.cc_addr is not None else []) +
(self.msg.bcc_addr.split(',') if self.msg.bcc_addr is not None else []))),
msg.as_string())
server.quit()
def _create_message(self, *args, **kwargs):
msg = MIMEMultipart()
msg['Subject'] = Header(self.msg.render_subject(*args, **kwargs), self.msg.encoding)
msg['From'] = self.msg.from_addr
msg['To'] = self.msg.to_addr
msg['Cc'] = self.msg.cc_addr
msg['Date'] = formatdate(localtime=True)
msg.add_header('reply-to', self.msg.reply_to)
msg.attach(MIMEText(self.msg.render_message(*args, **kwargs), 'html', self.msg.encoding))
if self.msg.attach:
attach = MIMEBase(self.msg.attach.mimetype, self.msg.attach.subtype)
attach.set_payload(open(self.msg.attach.file, 'rb').read())
encoders.encode_base64(attach)
attach.add_header('Content-Disposition', "attachment", filename=self.msg.attach.name)
msg.attach(attach)
return msg
class MailMessage(object):
def __init__(self, subject, from_addr, to_addr, cc_addr, tmpl_dir, body_tmpl,
bcc_addr=None, reply_to=None, attach=None, encoding='utf-8'):
self.env = Environment(loader=FileSystemLoader(tmpl_dir, encoding=encoding))
self.subject = subject
self.from_addr = from_addr
self.to_addr = to_addr
self.cc_addr = cc_addr
self.body_tmpl = self.env.get_template(body_tmpl)
self.bcc_addr = bcc_addr
self.reply_to = reply_to
self.attach = attach
self.encoding = encoding
def render_subject(self, *args, **kwargs):
subject = Template(self.subject)
return subject.render(*args, **kwargs)
def render_message(self, *args, **kwargs):
return self.body_tmpl.render(*args, **kwargs)
def add_filter(self, name, filter):
self.env.filters[name] = filter
class MailAttachment(object):
def __init__(self, name, file, mimetype, subtype):
self.name = name
self.file = file
self.mimetype = mimetype
self.subtype = subtype
if __name__ == '__main__':
msg = MailMessage(setting.MAIL_SUBJECT,
setting.MAIL_FROM,
setting.MAIL_TO,
setting.MAIL_CC,
setting.MAIL_TEMPLATE_DIR,
setting.MAIL_BODY_TEMPLATE)
mail = Mail(setting.MAIL_HOST,
setting.MAIL_PORT,
setting.MAIL_TLS,
setting.MAIL_USER,
setting.MAIL_PASSWORD,
msg)
mail_args = {
'subject': 'test mail subject',
'message': 'test mail message'
}
mail.send(mail_args)
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import os
MAIL_HOST = 'smtp.gmail.com'
MAIL_PORT = 587
MAIL_TLS = True
MAIL_USER = '[email protected]'
MAIL_PASSWORD = 'PASSWORD'
MAIL_FROM = '[email protected]'
MAIL_TO = 'MAIL_TO'
MAIL_CC = 'MAIL_CC'
MAIL_SUBJECT = '{{ subject }}'
MAIL_TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), 'template', 'mail')
MAIL_BODY_TEMPLATE = 'mail.html.jinja2'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment