[Zope3-checkins] CVS: Zope3/src/zope/app/mail - mailer.py:1.3.2.2

Albertas Agejevas alga@codeworks.lt
Thu, 22 May 2003 14:09:34 -0400


Update of /cvs-repository/Zope3/src/zope/app/mail
In directory cvs.zope.org:/tmp/cvs-serv6671

Added Files:
      Tag: cw-mail-branch
	mailer.py 
Log Message:
Mailers sending out mail via SMTP and /usr/lib/sendmail.  No error
handling so far.


=== Zope3/src/zope/app/mail/mailer.py 1.3.2.1 => 1.3.2.2 ===
--- /dev/null	Thu May 22 14:09:34 2003
+++ Zope3/src/zope/app/mail/mailer.py	Thu May 22 14:09:02 2003
@@ -0,0 +1,62 @@
+##############################################################################
+#
+# Copyright (c) 2003 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+"""These are classes which abstract different channels an email
+message could be sent out by.
+
+$Id$
+"""
+
+from zope.interface import implements
+from zope.app.interfaces.mail import ISendmailMailer, ISMTPMailer
+from os import popen
+from smtplib import SMTP
+
+__metaclass__ = type
+
+class SendmailMailer:
+
+    implements(ISendmailMailer)
+
+    # A hook for unit tests
+    popen = popen
+
+    def __init__(self, command="/usr/lib/sendmail -oem -oi -f %(from)s %(to)s"):
+        self.command = command
+
+    def send(self, fromaddr, toaddrs, message):
+        command = self.command % {'from': fromaddr, 'to': " ".join(toaddrs)}
+        f = self.popen(command, "w")
+        f.write(message)
+        f.close()
+
+class SMTPMailer:
+
+    implements(ISMTPMailer)
+
+    smtp = SMTP
+
+    def __init__(self, hostname='localhost', port=25,
+                 username=None, password=None):
+        self.hostname = hostname
+        self.port = port
+        self.username = username
+        self.password = password
+
+    def send(self, fromaddr, toaddrs, message):
+        connection = self.smtp(self.hostname, self.port)
+        if self.username is not None and self.password is not None:
+            connection.login(self.username, self.password)
+        connection.sendmail(fromaddr, toaddrs, message)
+        connection.quit()