##// END OF EJS Templates
Fixed problems with unicode cache keys in celery
Fixed problems with unicode cache keys in celery

File last commit:

r1639:95c3e33e merge default
r1641:cd1c21af default
Show More
smtp_mailer.py
165 lines | 5.9 KiB | text/x-python | PythonLexer
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to...
r981 # -*- coding: utf-8 -*-
"""
rhodecode.lib.smtp_mailer
~~~~~~~~~~~~~~~~~~~~~~~~~
source code cleanup: remove trailing white space, normalize file endings
r1203
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to...
r981 Simple smtp mailer used in RhodeCode
source code cleanup: remove trailing white space, normalize file endings
r1203
Added limit option for revision ranges
r984 :created_on: Sep 13, 2010
updated docstrings
r1532 :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com>
:license: GPLv3, see COPYING for more details.
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to...
r981 """
updated docstrings
r1532 # 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/>.
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to...
r981
renamed project to rhodecode
r547 import logging
import smtplib
import mimetypes
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to...
r981 from socket import sslerror
renamed project to rhodecode
r547 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
pep8ify
r1307
renamed project to rhodecode
r547 class SmtpMailer(object):
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to...
r981 """SMTP mailer class
source code cleanup: remove trailing white space, normalize file endings
r1203
made auth type optional in constructor
r1583 mailer = SmtpMailer(mail_from, user, passwd, mail_server, smtp_auth
pep8ify
r1307 mail_port, ssl, tls)
source code cleanup: remove trailing white space, normalize file endings
r1203 mailer.send(recipients, subject, body, attachment_files)
renamed project to rhodecode
r547 :param recipients might be a list of string or single string
source code cleanup: remove trailing white space, normalize file endings
r1203 :param attachment_files is a dict of {filename:location}
it tries to guess the mimetype and attach the file
renamed project to rhodecode
r547 """
made auth type optional in constructor
r1583 def __init__(self, mail_from, user, passwd, mail_server, smtp_auth=None,
mail_port=None, ssl=False, tls=False, debug=False):
fixes #59, notifications for user registrations + some changes to mailer
r689
renamed project to rhodecode
r547 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
control mailer debug with the .ini file
r1169 self.debug = debug
Les Peabody
applied smth_auth options update patch
r1581 self.auth = smtp_auth
fixes #59, notifications for user registrations + some changes to mailer
r689
removed immutable default argument from mailer
r1029 def send(self, recipients=[], subject='', body='', attachment_files=None):
renamed project to rhodecode
r547
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:
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to...
r981 smtp_serv.ehlo()
renamed project to rhodecode
r547 smtp_serv.starttls()
fixes #59, notifications for user registrations + some changes to mailer
r689
if self.debug:
renamed project to rhodecode
r547 smtp_serv.set_debuglevel(1)
fixed ehlo command for mailing....
r952 smtp_serv.ehlo()
Les Peabody
applied smth_auth options update patch
r1581 if self.auth:
smtp_serv.esmtp_features["auth"] = self.auth
renamed project to rhodecode
r547
made auth type optional in constructor
r1583 # if server requires authorization you must provide login and password
# but only if we have them
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to...
r981 if self.user and self.passwd:
smtp_serv.login(self.user, self.passwd)
renamed project to rhodecode
r547 date_ = formatdate(localtime=True)
msg = MIMEMultipart()
fixes #223 improve password reset form
r1417 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')
renamed project to rhodecode
r547 msg['From'] = self.mail_from
msg['To'] = ','.join(recipients)
msg['Date'] = date_
msg['Subject'] = subject
fixes #223 improve password reset form
r1417 msg.attach(text_msg)
renamed project to rhodecode
r547
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)
fixed smtp_mailer with Johan Walles suggestion, and made some patches according to...
r981
try:
smtp_serv.quit()
except sslerror:
# sslerror is raised in tls connections on closing sometimes
pass
renamed project to rhodecode
r547 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)
pep8ify
r1307 logging.info("guessing file %s type based on %s", ctype,
f_name)
renamed project to rhodecode
r547 if ctype is None or encoding is not None:
pep8ify
r1307 # No guess could be made, or the file is encoded
# (compressed), so use a generic bag-of-bits type.
renamed project to rhodecode
r547 ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
if maintype == 'text':
# Note: we should handle calculating the charset
fixes #59, notifications for user registrations + some changes to mailer
r689 file_part = MIMEText(self.get_content(msg_file),
renamed project to rhodecode
r547 _subtype=subtype)
elif maintype == 'image':
fixes #59, notifications for user registrations + some changes to mailer
r689 file_part = MIMEImage(self.get_content(msg_file),
renamed project to rhodecode
r547 _subtype=subtype)
elif maintype == 'audio':
fixes #59, notifications for user registrations + some changes to mailer
r689 file_part = MIMEAudio(self.get_content(msg_file),
renamed project to rhodecode
r547 _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
fixes #59, notifications for user registrations + some changes to mailer
r689 file_part.add_header('Content-Disposition', 'attachment',
renamed project to rhodecode
r547 filename=f_name)
file_part.add_header('Content-Type', ctype, name=f_name)
msg.attach(file_part)
else:
fixes #59, notifications for user registrations + some changes to mailer
r689 raise Exception('Attachment files should be'
'a dict in format {"filename":"filepath"}')
renamed project to rhodecode
r547
def get_content(self, msg_file):
removed immutable default argument from mailer
r1029 """Get content based on type, if content is a string do open first
renamed project to rhodecode
r547 else just read because it's a probably open file object
source code cleanup: remove trailing white space, normalize file endings
r1203
fixed @repo into :repo for docs...
r604 :param msg_file:
removed immutable default argument from mailer
r1029 """
renamed project to rhodecode
r547 if isinstance(msg_file, str):
return open(msg_file, "rb").read()
else:
made auth type optional in constructor
r1583 # just for safe seek to 0
renamed project to rhodecode
r547 msg_file.seek(0)
return msg_file.read()
made auth type optional in constructor
r1583