Files
@ 95c3e33ef32e
Branch filter:
Location: kallithea/rhodecode/lib/smtp_mailer.py - annotation
95c3e33ef32e
5.9 KiB
text/x-python
merged upto rev 019026a8cf67
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | c45c910714e4 c45c910714e4 c45c910714e4 c45c910714e4 6832ef664673 c45c910714e4 6832ef664673 ccd323bfa121 2afe9320d5e6 2afe9320d5e6 c45c910714e4 2afe9320d5e6 2afe9320d5e6 2afe9320d5e6 2afe9320d5e6 2afe9320d5e6 2afe9320d5e6 2afe9320d5e6 2afe9320d5e6 2afe9320d5e6 2afe9320d5e6 2afe9320d5e6 2afe9320d5e6 c45c910714e4 1e757ac98988 1e757ac98988 1e757ac98988 c45c910714e4 c45c910714e4 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 c1516b35f91d 1e757ac98988 c45c910714e4 6832ef664673 8e77c75bd65a c1516b35f91d 6832ef664673 6832ef664673 1e757ac98988 6832ef664673 6832ef664673 6832ef664673 1e757ac98988 1e757ac98988 8e77c75bd65a 8e77c75bd65a ecc566f8b69f 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 f6dca275c5a8 67377fd685be ecc566f8b69f 3bf8898ce66d 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 c45c910714e4 1e757ac98988 ecc566f8b69f ecc566f8b69f 1e757ac98988 1e757ac98988 beabd452f08b 67377fd685be 67377fd685be 1e757ac98988 8e77c75bd65a 8e77c75bd65a c45c910714e4 c45c910714e4 c45c910714e4 1e757ac98988 1e757ac98988 5875955def39 5875955def39 5875955def39 5875955def39 5875955def39 5875955def39 5875955def39 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 5875955def39 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 c45c910714e4 c45c910714e4 c45c910714e4 c45c910714e4 c45c910714e4 c45c910714e4 c45c910714e4 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 c1516b35f91d c1516b35f91d 1e757ac98988 c1516b35f91d c1516b35f91d 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 ecc566f8b69f 1e757ac98988 1e757ac98988 ecc566f8b69f 1e757ac98988 1e757ac98988 ecc566f8b69f 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 ecc566f8b69f 1e757ac98988 1e757ac98988 1e757ac98988 1e757ac98988 ecc566f8b69f ecc566f8b69f 1e757ac98988 1e757ac98988 3bf8898ce66d 1e757ac98988 6832ef664673 5cc96df705b9 3bf8898ce66d 1e757ac98988 1e757ac98988 1e757ac98988 8e77c75bd65a 1e757ac98988 1e757ac98988 8e77c75bd65a | # -*- coding: utf-8 -*-
"""
rhodecode.lib.smtp_mailer
~~~~~~~~~~~~~~~~~~~~~~~~~
Simple smtp mailer used in RhodeCode
:created_on: Sep 13, 2010
:copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com>
:license: GPLv3, see COPYING for more details.
"""
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import smtplib
import mimetypes
from socket import sslerror
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email import encoders
class SmtpMailer(object):
"""SMTP mailer class
mailer = SmtpMailer(mail_from, user, passwd, mail_server, smtp_auth
mail_port, ssl, tls)
mailer.send(recipients, subject, body, attachment_files)
:param recipients might be a list of string or single string
:param attachment_files is a dict of {filename:location}
it tries to guess the mimetype and attach the file
"""
def __init__(self, mail_from, user, passwd, mail_server, smtp_auth=None,
mail_port=None, ssl=False, tls=False, debug=False):
self.mail_from = mail_from
self.mail_server = mail_server
self.mail_port = mail_port
self.user = user
self.passwd = passwd
self.ssl = ssl
self.tls = tls
self.debug = debug
self.auth = smtp_auth
def send(self, recipients=[], subject='', body='', attachment_files=None):
if isinstance(recipients, basestring):
recipients = [recipients]
if self.ssl:
smtp_serv = smtplib.SMTP_SSL(self.mail_server, self.mail_port)
else:
smtp_serv = smtplib.SMTP(self.mail_server, self.mail_port)
if self.tls:
smtp_serv.ehlo()
smtp_serv.starttls()
if self.debug:
smtp_serv.set_debuglevel(1)
smtp_serv.ehlo()
if self.auth:
smtp_serv.esmtp_features["auth"] = self.auth
# if server requires authorization you must provide login and password
# but only if we have them
if self.user and self.passwd:
smtp_serv.login(self.user, self.passwd)
date_ = formatdate(localtime=True)
msg = MIMEMultipart()
msg.set_type('multipart/alternative')
msg.preamble = 'You will not see this in a MIME-aware mail reader.\n'
text_msg = MIMEText(body)
text_msg.set_type('text/plain')
text_msg.set_param('charset', 'UTF-8')
msg['From'] = self.mail_from
msg['To'] = ','.join(recipients)
msg['Date'] = date_
msg['Subject'] = subject
msg.attach(text_msg)
if attachment_files:
self.__atach_files(msg, attachment_files)
smtp_serv.sendmail(self.mail_from, recipients, msg.as_string())
logging.info('MAIL SEND TO: %s' % recipients)
try:
smtp_serv.quit()
except sslerror:
# sslerror is raised in tls connections on closing sometimes
pass
def __atach_files(self, msg, attachment_files):
if isinstance(attachment_files, dict):
for f_name, msg_file in attachment_files.items():
ctype, encoding = mimetypes.guess_type(f_name)
logging.info("guessing file %s type based on %s", ctype,
f_name)
if ctype is None or encoding is not None:
# No guess could be made, or the file is encoded
# (compressed), so use a generic bag-of-bits type.
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
if maintype == 'text':
# Note: we should handle calculating the charset
file_part = MIMEText(self.get_content(msg_file),
_subtype=subtype)
elif maintype == 'image':
file_part = MIMEImage(self.get_content(msg_file),
_subtype=subtype)
elif maintype == 'audio':
file_part = MIMEAudio(self.get_content(msg_file),
_subtype=subtype)
else:
file_part = MIMEBase(maintype, subtype)
file_part.set_payload(self.get_content(msg_file))
# Encode the payload using Base64
encoders.encode_base64(msg)
# Set the filename parameter
file_part.add_header('Content-Disposition', 'attachment',
filename=f_name)
file_part.add_header('Content-Type', ctype, name=f_name)
msg.attach(file_part)
else:
raise Exception('Attachment files should be'
'a dict in format {"filename":"filepath"}')
def get_content(self, msg_file):
"""Get content based on type, if content is a string do open first
else just read because it's a probably open file object
:param msg_file:
"""
if isinstance(msg_file, str):
return open(msg_file, "rb").read()
else:
# just for safe seek to 0
msg_file.seek(0)
return msg_file.read()
|