[Checkins] SVN: zope.sendmail/branches/ajung-encryption-branch/src/zope/sendmail/mailer.py SSL/TLS support added

Andreas Jung andreas at andreas-jung.com
Sat Aug 18 11:47:03 EDT 2007


Log message for revision 78945:
  SSL/TLS support added
  

Changed:
  U   zope.sendmail/branches/ajung-encryption-branch/src/zope/sendmail/mailer.py

-=-
Modified: zope.sendmail/branches/ajung-encryption-branch/src/zope/sendmail/mailer.py
===================================================================
--- zope.sendmail/branches/ajung-encryption-branch/src/zope/sendmail/mailer.py	2007-08-18 15:21:06 UTC (rev 78944)
+++ zope.sendmail/branches/ajung-encryption-branch/src/zope/sendmail/mailer.py	2007-08-18 15:47:03 UTC (rev 78945)
@@ -19,12 +19,16 @@
 """
 __docformat__ = 'restructuredtext'
 
+import socket
 from smtplib import SMTP
 
 from zope.interface import implements
 from zope.sendmail.interfaces import ISMTPMailer
 
+from Products.MailHost.MailHost import MailHostError
 
+have_ssl = hasattr(socket, 'ssl')
+
 class SMTPMailer(object):
 
     implements(ISMTPMailer)
@@ -40,7 +44,24 @@
 
     def send(self, fromaddr, toaddrs, message):
         connection = self.smtp(self.hostname, str(self.port))
-        if self.username is not None and self.password is not None:
-            connection.login(self.username, self.password)
+
+        # send EHLO
+        code, response = connection.helo()
+        if code < 200 or code >300:
+            raise MailHostError('Error sending EHLO to the SMTP server '
+                                '(code=%s, response=%s)' % (code, response))
+
+        # encryption support
+        if connection.has_extn('starttls') and have_ssl:
+            connection.starttls()
+            connection.ehlo()
+
+        if connection.does_esmtp: 
+            if self.username is not None and self.password is not None:
+                connection.login(self.username, self.password)
+        elif self.username:
+            raise MailHostError('Mailhost does not support ESMTP but a username '
+                                'is configured')
+
         connection.sendmail(fromaddr, toaddrs, message)
         connection.quit()



More information about the Checkins mailing list