changeset 981:c45c910714e4 beta

fixed smtp_mailer with Johan Walles suggestion, and made some patches according to paster reporter email
author Marcin Kuzminski <marcin@python-works.com>
date Mon, 31 Jan 2011 15:10:37 +0100
parents 01eb7098681e
children 134e169f11a4
files rhodecode/lib/smtp_mailer.py
diffstat 1 files changed, 29 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/lib/smtp_mailer.py	Mon Jan 31 01:23:31 2011 +0100
+++ b/rhodecode/lib/smtp_mailer.py	Mon Jan 31 15:10:37 2011 +0100
@@ -1,6 +1,20 @@
+# -*- coding: utf-8 -*-
+"""
+    rhodecode.lib.smtp_mailer
+    ~~~~~~~~~~~~~~~~~~~~~~~~~
+    
+    Simple smtp mailer used in RhodeCode
+    
+    :created_on: Sep 13, 2011
+    :copyright: (c) 2011 by marcink.
+    :license: LICENSE_NAME, see LICENSE_FILE for more details.
+"""
+
 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
@@ -10,14 +24,15 @@
 from email import encoders
 
 class SmtpMailer(object):
-    """simple smtp mailer class
+    """SMTP mailer class
     
     mailer = SmtpMailer(mail_from, user, passwd, mail_server, 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
+        it tries to guess the mimetype and attach the file 
+    
     """
 
     def __init__(self, mail_from, user, passwd, mail_server,
@@ -42,6 +57,7 @@
             smtp_serv = smtplib.SMTP(self.mail_server, self.mail_port)
 
         if self.tls:
+            smtp_serv.ehlo()
             smtp_serv.starttls()
 
         if self.debug:
@@ -50,7 +66,10 @@
         smtp_serv.ehlo()
 
         #if server requires authorization you must provide login and password
-        smtp_serv.login(self.user, self.passwd)
+        #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()
@@ -67,7 +86,13 @@
 
         smtp_serv.sendmail(self.mail_from, recipients, msg.as_string())
         logging.info('MAIL SEND TO: %s' % recipients)
-        smtp_serv.quit()
+
+        try:
+            smtp_serv.quit()
+        except sslerror:
+            # sslerror is raised in tls connections on closing sometimes
+            pass
+
 
 
     def __atach_files(self, msg, attachment_files):