xref: /trunk/main/scripting/source/pyprov/mailmerge.py (revision 16c3da916017a0e7db7dc4f93ae00dd1c24b61c9)
19f22d7c2SAndrew Rist# *************************************************************
29f22d7c2SAndrew Rist#
39f22d7c2SAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
49f22d7c2SAndrew Rist#  or more contributor license agreements.  See the NOTICE file
59f22d7c2SAndrew Rist#  distributed with this work for additional information
69f22d7c2SAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
79f22d7c2SAndrew Rist#  to you under the Apache License, Version 2.0 (the
89f22d7c2SAndrew Rist#  "License"); you may not use this file except in compliance
99f22d7c2SAndrew Rist#  with the License.  You may obtain a copy of the License at
109f22d7c2SAndrew Rist#
119f22d7c2SAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
129f22d7c2SAndrew Rist#
139f22d7c2SAndrew Rist#  Unless required by applicable law or agreed to in writing,
149f22d7c2SAndrew Rist#  software distributed under the License is distributed on an
159f22d7c2SAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169f22d7c2SAndrew Rist#  KIND, either express or implied.  See the License for the
179f22d7c2SAndrew Rist#  specific language governing permissions and limitations
189f22d7c2SAndrew Rist#  under the License.
199f22d7c2SAndrew Rist#
209f22d7c2SAndrew Rist# *************************************************************
219f22d7c2SAndrew Rist
22cdf0e10cSrcweir# Caolan McNamara caolanm@redhat.com
23cdf0e10cSrcweir# a simple email mailmerge component
24cdf0e10cSrcweir
25cdf0e10cSrcweir# manual installation for hackers, not necessary for users
26cdf0e10cSrcweir# cp mailmerge.py /usr/lib/openoffice.org2.0/program
27cdf0e10cSrcweir# cd /usr/lib/openoffice.org2.0/program
28cdf0e10cSrcweir# ./unopkg add --shared mailmerge.py
29cdf0e10cSrcweir# edit ~/.openoffice.org2/user/registry/data/org/openoffice/Office/Writer.xcu
30cdf0e10cSrcweir# and change EMailSupported to as follows...
31cdf0e10cSrcweir#  <prop oor:name="EMailSupported" oor:type="xs:boolean">
32cdf0e10cSrcweir#   <value>true</value>
33cdf0e10cSrcweir#  </prop>
34cdf0e10cSrcweir
35cdf0e10cSrcweirimport unohelper
36cdf0e10cSrcweirimport uno
37cdf0e10cSrcweirimport re
38cdf0e10cSrcweir
39cdf0e10cSrcweir#to implement com::sun::star::mail::XMailServiceProvider
40cdf0e10cSrcweir#and
41cdf0e10cSrcweir#to implement com.sun.star.mail.XMailMessage
42cdf0e10cSrcweir
43cdf0e10cSrcweirfrom com.sun.star.mail import XMailServiceProvider
44cdf0e10cSrcweirfrom com.sun.star.mail import XMailService
45cdf0e10cSrcweirfrom com.sun.star.mail import XSmtpService
46cdf0e10cSrcweirfrom com.sun.star.mail import XConnectionListener
47cdf0e10cSrcweirfrom com.sun.star.mail import XAuthenticator
48cdf0e10cSrcweirfrom com.sun.star.mail import XMailMessage
49cdf0e10cSrcweirfrom com.sun.star.mail.MailServiceType import SMTP
50cdf0e10cSrcweirfrom com.sun.star.mail.MailServiceType import POP3
51cdf0e10cSrcweirfrom com.sun.star.mail.MailServiceType import IMAP
52cdf0e10cSrcweirfrom com.sun.star.uno import XCurrentContext
53cdf0e10cSrcweirfrom com.sun.star.lang import IllegalArgumentException
54cdf0e10cSrcweirfrom com.sun.star.lang import EventObject
55cdf0e10cSrcweirfrom com.sun.star.mail import SendMailMessageFailedException
56cdf0e10cSrcweir
57cdf0e10cSrcweirfrom email.MIMEBase import MIMEBase
58cdf0e10cSrcweirfrom email.Message import Message
59cdf0e10cSrcweirfrom email import Encoders
60cdf0e10cSrcweirfrom email.Header import Header
61cdf0e10cSrcweirfrom email.MIMEMultipart import MIMEMultipart
62cdf0e10cSrcweirfrom email.Utils import formatdate
63cdf0e10cSrcweirfrom email.Utils import parseaddr
64d3d1f4e0SAriel Constenla-Hailefrom socket import _GLOBAL_DEFAULT_TIMEOUT
65cdf0e10cSrcweir
66cdf0e10cSrcweirimport sys, smtplib, imaplib, poplib
67cdf0e10cSrcweir
68cdf0e10cSrcweirdbg = False
69bc42e70bSAriel Constenla-Haileout = sys.stderr
70cdf0e10cSrcweir
71cdf0e10cSrcweirclass PyMailSMTPService(unohelper.Base, XSmtpService):
72cdf0e10cSrcweir    def __init__( self, ctx ):
73cdf0e10cSrcweir        self.ctx = ctx
74cdf0e10cSrcweir        self.listeners = []
75cdf0e10cSrcweir        self.supportedtypes = ('Insecure', 'Ssl')
76cdf0e10cSrcweir        self.server = None
77cdf0e10cSrcweir        self.connectioncontext = None
78bb7facceSAriel Constenla-Haile        self.notify = EventObject(self)
79cdf0e10cSrcweir        if dbg:
80a5dab537Smseidel            out.write("PyMailSMTPService init\n")
81cdf0e10cSrcweir    def addConnectionListener(self, xListener):
82cdf0e10cSrcweir        if dbg:
83a5dab537Smseidel            out.write("PyMailSMTPService addConnectionListener\n")
84cdf0e10cSrcweir        self.listeners.append(xListener)
85cdf0e10cSrcweir    def removeConnectionListener(self, xListener):
86cdf0e10cSrcweir        if dbg:
87a5dab537Smseidel            out.write("PyMailSMTPService removeConnectionListener\n")
88cdf0e10cSrcweir        self.listeners.remove(xListener)
89cdf0e10cSrcweir    def getSupportedConnectionTypes(self):
90cdf0e10cSrcweir        if dbg:
91a5dab537Smseidel            out.write("PyMailSMTPService getSupportedConnectionTypes\n")
92cdf0e10cSrcweir        return self.supportedtypes
93cdf0e10cSrcweir    def connect(self, xConnectionContext, xAuthenticator):
94cdf0e10cSrcweir        self.connectioncontext = xConnectionContext
95cdf0e10cSrcweir        if dbg:
96a5dab537Smseidel            out.write("PyMailSMTPService connect\n")
97d3d1f4e0SAriel Constenla-Haile
98cdf0e10cSrcweir        server = xConnectionContext.getValueByName("ServerName")
99cdf0e10cSrcweir        if dbg:
100bc42e70bSAriel Constenla-Haile            out.write("ServerName: %s\n" % server)
101d3d1f4e0SAriel Constenla-Haile
102cdf0e10cSrcweir        port = xConnectionContext.getValueByName("Port")
103cdf0e10cSrcweir        if dbg:
104bc42e70bSAriel Constenla-Haile            out.write("Port: %d\n" % port)
105d3d1f4e0SAriel Constenla-Haile
106d3d1f4e0SAriel Constenla-Haile        tout = xConnectionContext.getValueByName("Timeout")
107d3d1f4e0SAriel Constenla-Haile        if dbg:
108bc42e70bSAriel Constenla-Haile            out.write("Timeout is instance of int? %s\n" % isinstance(tout,int))
109d3d1f4e0SAriel Constenla-Haile        if not isinstance(tout,int):
110d3d1f4e0SAriel Constenla-Haile            tout = _GLOBAL_DEFAULT_TIMEOUT
111d3d1f4e0SAriel Constenla-Haile        if dbg:
112bc42e70bSAriel Constenla-Haile            out.write("Timeout: %s\n" % str(tout))
113d3d1f4e0SAriel Constenla-Haile
114*6e1ccbb0SBidouille        connectiontype = xConnectionContext.getValueByName("ConnectionType")
115*6e1ccbb0SBidouille        if connectiontype.upper() == "SSL":
116*6e1ccbb0SBidouille            if not hasattr(smtplib, "SMTP_SSL"):
117*6e1ccbb0SBidouille                raise IllegalArgumentException("Connection type is not supported: " + connectiontype, self, 1)
118*6e1ccbb0SBidouille            self.server = smtplib.SMTP_SSL(server, port, timeout=tout)
119*6e1ccbb0SBidouille        else:
120d3d1f4e0SAriel Constenla-Haile            self.server = smtplib.SMTP(server, port,timeout=tout)
121cdf0e10cSrcweir        if dbg:
122cdf0e10cSrcweir            self.server.set_debuglevel(1)
123d3d1f4e0SAriel Constenla-Haile
124cdf0e10cSrcweir        if dbg:
125bc42e70bSAriel Constenla-Haile            out.write("ConnectionType: %s\n" % str(connectiontype))
126d3d1f4e0SAriel Constenla-Haile
127*6e1ccbb0SBidouille        if connectiontype.upper() == 'INSECURE':
128cdf0e10cSrcweir            self.server.ehlo()
129cdf0e10cSrcweir            self.server.starttls()
130cdf0e10cSrcweir            self.server.ehlo()
131cdf0e10cSrcweir
132cdf0e10cSrcweir        user = xAuthenticator.getUserName().encode('ascii')
133cdf0e10cSrcweir        password = xAuthenticator.getPassword().encode('ascii')
134cdf0e10cSrcweir        if user != '':
135cdf0e10cSrcweir            if dbg:
136bc42e70bSAriel Constenla-Haile                out.write('Logging in, username of %s\n' % user)
137cdf0e10cSrcweir            self.server.login(user, password)
138cdf0e10cSrcweir
139cdf0e10cSrcweir        for listener in self.listeners:
140cdf0e10cSrcweir            listener.connected(self.notify)
141cdf0e10cSrcweir    def disconnect(self):
142cdf0e10cSrcweir        if dbg:
143a5dab537Smseidel            out.write("PyMailSMTPService disconnect\n")
144cdf0e10cSrcweir        if self.server:
145cdf0e10cSrcweir            self.server.quit()
146cdf0e10cSrcweir            self.server = None
147cdf0e10cSrcweir        for listener in self.listeners:
148cdf0e10cSrcweir            listener.disconnected(self.notify)
149cdf0e10cSrcweir    def isConnected(self):
150cdf0e10cSrcweir        if dbg:
151a5dab537Smseidel            out.write("PyMailSMTPService isConnected\n")
152cdf0e10cSrcweir        return self.server != None
153cdf0e10cSrcweir    def getCurrentConnectionContext(self):
154cdf0e10cSrcweir        if dbg:
155a5dab537Smseidel            out.write("PyMailSMTPService getCurrentConnectionContext\n")
156cdf0e10cSrcweir        return self.connectioncontext
157cdf0e10cSrcweir    def sendMailMessage(self, xMailMessage):
158cdf0e10cSrcweir        COMMASPACE = ', '
159cdf0e10cSrcweir
160cdf0e10cSrcweir        if dbg:
161a5dab537Smseidel            out.write("PyMailSMTPService sendMailMessage\n")
162cdf0e10cSrcweir        recipients = xMailMessage.getRecipients()
163cdf0e10cSrcweir        sendermail = xMailMessage.SenderAddress
164cdf0e10cSrcweir        sendername = xMailMessage.SenderName
165cdf0e10cSrcweir        subject = xMailMessage.Subject
166cdf0e10cSrcweir        ccrecipients = xMailMessage.getCcRecipients()
167cdf0e10cSrcweir        bccrecipients = xMailMessage.getBccRecipients()
168cdf0e10cSrcweir        if dbg:
169a5dab537Smseidel            out.write("PyMailSMTPService subject %s\n" % subject)
170a5dab537Smseidel            out.write("PyMailSMTPService from %s\n" % sendername.encode('utf-8'))
171bc42e70bSAriel Constenla-Haile            out.write("PyMailSMTPService from %s\n" % sendermail)
172a5dab537Smseidel            out.write("PyMailSMTPService send to %s\n" % str(recipients))
173cdf0e10cSrcweir
174cdf0e10cSrcweir        attachments = xMailMessage.getAttachments()
175cdf0e10cSrcweir
176cdf0e10cSrcweir        textmsg = Message()
177cdf0e10cSrcweir
178cdf0e10cSrcweir        content = xMailMessage.Body
179cdf0e10cSrcweir        flavors = content.getTransferDataFlavors()
180cdf0e10cSrcweir        if dbg:
181a5dab537Smseidel            out.write("PyMailSMTPService flavors len %d\n" % len(flavors))
182cdf0e10cSrcweir
183cdf0e10cSrcweir        #Use first flavor that's sane for an email body
184cdf0e10cSrcweir        for flavor in flavors:
185cdf0e10cSrcweir            if flavor.MimeType.find('text/html') != -1 or flavor.MimeType.find('text/plain') != -1:
186cdf0e10cSrcweir                if dbg:
187a5dab537Smseidel                    out.write("PyMailSMTPService mimetype is %s\n" % flavor.MimeType)
188cdf0e10cSrcweir                textbody = content.getTransferData(flavor)
189cdf0e10cSrcweir                try:
190cdf0e10cSrcweir                    textbody = textbody.value
191cdf0e10cSrcweir                except:
192cdf0e10cSrcweir                    pass
193cdf0e10cSrcweir                textbody = textbody.encode('utf-8')
194cdf0e10cSrcweir
195cdf0e10cSrcweir                if len(textbody):
196cdf0e10cSrcweir                    mimeEncoding = re.sub("charset=.*", "charset=UTF-8", flavor.MimeType)
197cdf0e10cSrcweir                    if mimeEncoding.find('charset=UTF-8') == -1:
198cdf0e10cSrcweir                        mimeEncoding = mimeEncoding + "; charset=UTF-8"
199cdf0e10cSrcweir                    textmsg['Content-Type'] = mimeEncoding
200cdf0e10cSrcweir                    textmsg['MIME-Version'] = '1.0'
201cdf0e10cSrcweir                    textmsg.set_payload(textbody)
202cdf0e10cSrcweir
203cdf0e10cSrcweir                break
204cdf0e10cSrcweir
205cdf0e10cSrcweir        if (len(attachments)):
206cdf0e10cSrcweir            msg = MIMEMultipart()
207cdf0e10cSrcweir            msg.epilogue = ''
208cdf0e10cSrcweir            msg.attach(textmsg)
209cdf0e10cSrcweir        else:
210cdf0e10cSrcweir            msg = textmsg
211cdf0e10cSrcweir
212cdf0e10cSrcweir        hdr = Header(sendername, 'utf-8')
213cdf0e10cSrcweir        hdr.append('<'+sendermail+'>','us-ascii')
214cdf0e10cSrcweir        msg['Subject'] = subject
215cdf0e10cSrcweir        msg['From'] = hdr
216cdf0e10cSrcweir        msg['To'] = COMMASPACE.join(recipients)
217cdf0e10cSrcweir        if len(ccrecipients):
218cdf0e10cSrcweir            msg['Cc'] = COMMASPACE.join(ccrecipients)
219cdf0e10cSrcweir        if xMailMessage.ReplyToAddress != '':
220cdf0e10cSrcweir            msg['Reply-To'] = xMailMessage.ReplyToAddress
221cdf0e10cSrcweir
222a5dab537Smseidel        mailerstring = "OpenOffice via Caolan's mailmerge component"
223cdf0e10cSrcweir        try:
224cdf0e10cSrcweir            ctx = uno.getComponentContext()
225cdf0e10cSrcweir            aConfigProvider = ctx.ServiceManager.createInstance("com.sun.star.configuration.ConfigurationProvider")
226cdf0e10cSrcweir            prop = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
227cdf0e10cSrcweir            prop.Name = "nodepath"
228cdf0e10cSrcweir            prop.Value = "/org.openoffice.Setup/Product"
229cdf0e10cSrcweir            aSettings = aConfigProvider.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess",
230cdf0e10cSrcweir                    (prop,))
231cdf0e10cSrcweir            mailerstring = aSettings.getByName("ooName") + " " + \
232cdf0e10cSrcweir                    aSettings.getByName("ooSetupVersion") + " via Caolan's mailmerge component"
233cdf0e10cSrcweir        except:
234cdf0e10cSrcweir            pass
235cdf0e10cSrcweir
236cdf0e10cSrcweir        msg['X-Mailer'] = mailerstring
237cdf0e10cSrcweir        msg['Date'] = formatdate(localtime=True)
238cdf0e10cSrcweir
239cdf0e10cSrcweir        for attachment in attachments:
240cdf0e10cSrcweir            content = attachment.Data
241cdf0e10cSrcweir            flavors = content.getTransferDataFlavors()
242cdf0e10cSrcweir            flavor = flavors[0]
243cdf0e10cSrcweir            ctype = flavor.MimeType
244cdf0e10cSrcweir            maintype, subtype = ctype.split('/', 1)
245cdf0e10cSrcweir            msgattachment = MIMEBase(maintype, subtype)
246cdf0e10cSrcweir            data = content.getTransferData(flavor)
247cdf0e10cSrcweir            msgattachment.set_payload(data)
248cdf0e10cSrcweir            Encoders.encode_base64(msgattachment)
249a0292563SAriel Constenla-Haile            fname = attachment.ReadableName
250a0292563SAriel Constenla-Haile            try:
251a0292563SAriel Constenla-Haile                fname.encode('ascii')
252a0292563SAriel Constenla-Haile            except:
253a0292563SAriel Constenla-Haile                fname = ('utf-8','',fname.encode('utf-8'))
254cdf0e10cSrcweir            msgattachment.add_header('Content-Disposition', 'attachment', \
255a0292563SAriel Constenla-Haile                    filename=fname)
256cdf0e10cSrcweir            msg.attach(msgattachment)
257cdf0e10cSrcweir
258cdf0e10cSrcweir        uniquer = {}
259cdf0e10cSrcweir        for key in recipients:
260cdf0e10cSrcweir            uniquer[key] = True
261cdf0e10cSrcweir        if len(ccrecipients):
262cdf0e10cSrcweir            for key in ccrecipients:
263cdf0e10cSrcweir                uniquer[key] = True
264cdf0e10cSrcweir        if len(bccrecipients):
265cdf0e10cSrcweir            for key in bccrecipients:
266cdf0e10cSrcweir                uniquer[key] = True
2677d9fa7c3SPedro Giffuni        truerecipients = list(uniquer.keys())
268cdf0e10cSrcweir
269cdf0e10cSrcweir        if dbg:
270a5dab537Smseidel            out.write("PyMailSMTPService recipients are %s\n" % str(truerecipients))
271cdf0e10cSrcweir
272cdf0e10cSrcweir        self.server.sendmail(sendermail, truerecipients, msg.as_string())
273cdf0e10cSrcweir
274cdf0e10cSrcweirclass PyMailIMAPService(unohelper.Base, XMailService):
275cdf0e10cSrcweir    def __init__( self, ctx ):
276cdf0e10cSrcweir        self.ctx = ctx
277cdf0e10cSrcweir        self.listeners = []
278cdf0e10cSrcweir        self.supportedtypes = ('Insecure', 'Ssl')
279cdf0e10cSrcweir        self.server = None
280cdf0e10cSrcweir        self.connectioncontext = None
281bb7facceSAriel Constenla-Haile        self.notify = EventObject(self)
282cdf0e10cSrcweir        if dbg:
283bc42e70bSAriel Constenla-Haile            out.write("PyMailIMAPService init\n")
284cdf0e10cSrcweir    def addConnectionListener(self, xListener):
285cdf0e10cSrcweir        if dbg:
286bc42e70bSAriel Constenla-Haile            out.write("PyMailIMAPService addConnectionListener\n")
287cdf0e10cSrcweir        self.listeners.append(xListener)
288cdf0e10cSrcweir    def removeConnectionListener(self, xListener):
289cdf0e10cSrcweir        if dbg:
290bc42e70bSAriel Constenla-Haile            out.write("PyMailIMAPService removeConnectionListener\n")
291cdf0e10cSrcweir        self.listeners.remove(xListener)
292cdf0e10cSrcweir    def getSupportedConnectionTypes(self):
293cdf0e10cSrcweir        if dbg:
294bc42e70bSAriel Constenla-Haile            out.write("PyMailIMAPService getSupportedConnectionTypes\n")
295cdf0e10cSrcweir        return self.supportedtypes
296cdf0e10cSrcweir    def connect(self, xConnectionContext, xAuthenticator):
297cdf0e10cSrcweir        if dbg:
298bc42e70bSAriel Constenla-Haile            out.write("PyMailIMAPService connect\n")
299cdf0e10cSrcweir
300cdf0e10cSrcweir        self.connectioncontext = xConnectionContext
301cdf0e10cSrcweir        server = xConnectionContext.getValueByName("ServerName")
302cdf0e10cSrcweir        if dbg:
303bc42e70bSAriel Constenla-Haile            out.write("Server: %s\n" % server)
304cdf0e10cSrcweir        port = xConnectionContext.getValueByName("Port")
305cdf0e10cSrcweir        if dbg:
306bc42e70bSAriel Constenla-Haile            out.write("Port: %d\n" % port)
307cdf0e10cSrcweir        connectiontype = xConnectionContext.getValueByName("ConnectionType")
308cdf0e10cSrcweir        if dbg:
309bc42e70bSAriel Constenla-Haile            out.write("Connection type: %s\n" % connectiontype)
310bc42e70bSAriel Constenla-Haile        out.write("BEFORE\n")
311bb7facceSAriel Constenla-Haile        if connectiontype.upper() == 'SSL':
312cdf0e10cSrcweir            self.server = imaplib.IMAP4_SSL(server, port)
313cdf0e10cSrcweir        else:
314cdf0e10cSrcweir            self.server = imaplib.IMAP4(server, port)
315bc42e70bSAriel Constenla-Haile        out.write("AFTER\n")
316cdf0e10cSrcweir
317cdf0e10cSrcweir        user = xAuthenticator.getUserName().encode('ascii')
318cdf0e10cSrcweir        password = xAuthenticator.getPassword().encode('ascii')
319cdf0e10cSrcweir        if user != '':
320cdf0e10cSrcweir            if dbg:
321bc42e70bSAriel Constenla-Haile                out.write('Logging in, username of %s\n' % user)
322cdf0e10cSrcweir            self.server.login(user, password)
323cdf0e10cSrcweir
324cdf0e10cSrcweir        for listener in self.listeners:
325cdf0e10cSrcweir            listener.connected(self.notify)
326cdf0e10cSrcweir    def disconnect(self):
327cdf0e10cSrcweir        if dbg:
328bc42e70bSAriel Constenla-Haile            out.write("PyMailIMAPService disconnect\n")
329cdf0e10cSrcweir        if self.server:
330cdf0e10cSrcweir            self.server.logout()
331cdf0e10cSrcweir            self.server = None
332cdf0e10cSrcweir        for listener in self.listeners:
333cdf0e10cSrcweir            listener.disconnected(self.notify)
334cdf0e10cSrcweir    def isConnected(self):
335cdf0e10cSrcweir        if dbg:
336bc42e70bSAriel Constenla-Haile            out.write("PyMailIMAPService isConnected\n")
337cdf0e10cSrcweir        return self.server != None
338cdf0e10cSrcweir    def getCurrentConnectionContext(self):
339cdf0e10cSrcweir        if dbg:
340bc42e70bSAriel Constenla-Haile            out.write("PyMailIMAPService getCurrentConnectionContext\n")
341cdf0e10cSrcweir        return self.connectioncontext
342cdf0e10cSrcweir
343cdf0e10cSrcweirclass PyMailPOP3Service(unohelper.Base, XMailService):
344cdf0e10cSrcweir    def __init__( self, ctx ):
345cdf0e10cSrcweir        self.ctx = ctx
346cdf0e10cSrcweir        self.listeners = []
347cdf0e10cSrcweir        self.supportedtypes = ('Insecure', 'Ssl')
348cdf0e10cSrcweir        self.server = None
349cdf0e10cSrcweir        self.connectioncontext = None
350bb7facceSAriel Constenla-Haile        self.notify = EventObject(self)
351cdf0e10cSrcweir        if dbg:
352bc42e70bSAriel Constenla-Haile            out.write("PyMailPOP3Service init\n")
353cdf0e10cSrcweir    def addConnectionListener(self, xListener):
354cdf0e10cSrcweir        if dbg:
355bc42e70bSAriel Constenla-Haile            out.write("PyMailPOP3Service addConnectionListener\n")
356cdf0e10cSrcweir        self.listeners.append(xListener)
357cdf0e10cSrcweir    def removeConnectionListener(self, xListener):
358cdf0e10cSrcweir        if dbg:
359bc42e70bSAriel Constenla-Haile            out.write("PyMailPOP3Service removeConnectionListener\n")
360cdf0e10cSrcweir        self.listeners.remove(xListener)
361cdf0e10cSrcweir    def getSupportedConnectionTypes(self):
362cdf0e10cSrcweir        if dbg:
363bc42e70bSAriel Constenla-Haile            out.write("PyMailPOP3Service getSupportedConnectionTypes\n")
364cdf0e10cSrcweir        return self.supportedtypes
365cdf0e10cSrcweir    def connect(self, xConnectionContext, xAuthenticator):
366cdf0e10cSrcweir        if dbg:
367bc42e70bSAriel Constenla-Haile            out.write("PyMailPOP3Service connect\n")
368cdf0e10cSrcweir
369cdf0e10cSrcweir        self.connectioncontext = xConnectionContext
370cdf0e10cSrcweir        server = xConnectionContext.getValueByName("ServerName")
371cdf0e10cSrcweir        if dbg:
372bc42e70bSAriel Constenla-Haile            out.write("Server: %s\n" % server)
373cdf0e10cSrcweir        port = xConnectionContext.getValueByName("Port")
374cdf0e10cSrcweir        if dbg:
375bc42e70bSAriel Constenla-Haile            out.write("Port: %s\n" % port)
376cdf0e10cSrcweir        connectiontype = xConnectionContext.getValueByName("ConnectionType")
377cdf0e10cSrcweir        if dbg:
378bc42e70bSAriel Constenla-Haile            out.write("Connection type: %s\n" % str(connectiontype))
379bc42e70bSAriel Constenla-Haile        out.write("BEFORE\n")
380bb7facceSAriel Constenla-Haile        if connectiontype.upper() == 'SSL':
381cdf0e10cSrcweir            self.server = poplib.POP3_SSL(server, port)
382cdf0e10cSrcweir        else:
383d3d1f4e0SAriel Constenla-Haile            tout = xConnectionContext.getValueByName("Timeout")
384d3d1f4e0SAriel Constenla-Haile            if dbg:
385bc42e70bSAriel Constenla-Haile                out.write("Timeout is instance of int? %s\n" % isinstance(tout,int))
386d3d1f4e0SAriel Constenla-Haile            if not isinstance(tout,int):
387d3d1f4e0SAriel Constenla-Haile                tout = _GLOBAL_DEFAULT_TIMEOUT
388d3d1f4e0SAriel Constenla-Haile            if dbg:
389bc42e70bSAriel Constenla-Haile                out.write("Timeout: %s\n" % str(tout))
390d3d1f4e0SAriel Constenla-Haile            self.server = poplib.POP3(server, port, timeout=tout)
391bc42e70bSAriel Constenla-Haile        out.write("AFTER\n")
392cdf0e10cSrcweir
393cdf0e10cSrcweir        user = xAuthenticator.getUserName().encode('ascii')
394cdf0e10cSrcweir        password = xAuthenticator.getPassword().encode('ascii')
395cdf0e10cSrcweir        if dbg:
396bc42e70bSAriel Constenla-Haile            out.write('Logging in, username of %s\n' % user)
397cdf0e10cSrcweir        self.server.user(user)
398bb7facceSAriel Constenla-Haile        self.server.pass_(password)
399cdf0e10cSrcweir
400cdf0e10cSrcweir        for listener in self.listeners:
401cdf0e10cSrcweir            listener.connected(self.notify)
402cdf0e10cSrcweir    def disconnect(self):
403cdf0e10cSrcweir        if dbg:
404bc42e70bSAriel Constenla-Haile            out.write("PyMailPOP3Service disconnect\n")
405cdf0e10cSrcweir        if self.server:
406cdf0e10cSrcweir            self.server.quit()
407cdf0e10cSrcweir            self.server = None
408cdf0e10cSrcweir        for listener in self.listeners:
409cdf0e10cSrcweir            listener.disconnected(self.notify)
410cdf0e10cSrcweir    def isConnected(self):
411cdf0e10cSrcweir        if dbg:
412bc42e70bSAriel Constenla-Haile            out.write("PyMailPOP3Service isConnected\n")
413cdf0e10cSrcweir        return self.server != None
414cdf0e10cSrcweir    def getCurrentConnectionContext(self):
415cdf0e10cSrcweir        if dbg:
416bc42e70bSAriel Constenla-Haile            out.write("PyMailPOP3Service getCurrentConnectionContext\n")
417cdf0e10cSrcweir        return self.connectioncontext
418cdf0e10cSrcweir
419cdf0e10cSrcweirclass PyMailServiceProvider(unohelper.Base, XMailServiceProvider):
420cdf0e10cSrcweir    def __init__( self, ctx ):
421cdf0e10cSrcweir        if dbg:
422bc42e70bSAriel Constenla-Haile            out.write("PyMailServiceProvider init\n")
423cdf0e10cSrcweir        self.ctx = ctx
424cdf0e10cSrcweir    def create(self, aType):
425cdf0e10cSrcweir        if dbg:
426bc42e70bSAriel Constenla-Haile            out.write("PyMailServiceProvider create with %s\n" % aType)
427cdf0e10cSrcweir        if aType == SMTP:
428cdf0e10cSrcweir            return PyMailSMTPService(self.ctx);
429cdf0e10cSrcweir        elif aType == POP3:
430cdf0e10cSrcweir            return PyMailPOP3Service(self.ctx);
431cdf0e10cSrcweir        elif aType == IMAP:
432cdf0e10cSrcweir            return PyMailIMAPService(self.ctx);
433cdf0e10cSrcweir        else:
434bc42e70bSAriel Constenla-Haile            out.write("PyMailServiceProvider, unknown TYPE %s\n" % aType)
435cdf0e10cSrcweir
436cdf0e10cSrcweirclass PyMailMessage(unohelper.Base, XMailMessage):
437cdf0e10cSrcweir    def __init__( self, ctx, sTo='', sFrom='', Subject='', Body=None, aMailAttachment=None ):
438cdf0e10cSrcweir        if dbg:
439bc42e70bSAriel Constenla-Haile            out.write("PyMailMessage init\n")
440cdf0e10cSrcweir        self.ctx = ctx
441cdf0e10cSrcweir
442bb7facceSAriel Constenla-Haile        self.recipients = [sTo]
443bb7facceSAriel Constenla-Haile        self.ccrecipients = []
444bb7facceSAriel Constenla-Haile        self.bccrecipients = []
445bb7facceSAriel Constenla-Haile        self.aMailAttachments = []
446cdf0e10cSrcweir        if aMailAttachment != None:
447bb7facceSAriel Constenla-Haile            self.aMailAttachments.append(aMailAttachment)
448cdf0e10cSrcweir
449cdf0e10cSrcweir        self.SenderName, self.SenderAddress = parseaddr(sFrom)
450cdf0e10cSrcweir        self.ReplyToAddress = sFrom
451cdf0e10cSrcweir        self.Subject = Subject
452cdf0e10cSrcweir        self.Body = Body
453cdf0e10cSrcweir        if dbg:
454bc42e70bSAriel Constenla-Haile            out.write("post PyMailMessage init\n")
455cdf0e10cSrcweir    def addRecipient( self, recipient ):
456cdf0e10cSrcweir        if dbg:
457bc42e70bSAriel Constenla-Haile            out.write("PyMailMessage.addRecipient%s\n" % recipient)
458bb7facceSAriel Constenla-Haile        self.recipients.append(recipient)
459cdf0e10cSrcweir    def addCcRecipient( self, ccrecipient ):
460cdf0e10cSrcweir        if dbg:
461bc42e70bSAriel Constenla-Haile            out.write("PyMailMessage.addCcRecipient%s\n" % ccrecipient)
462bb7facceSAriel Constenla-Haile        self.ccrecipients.append(ccrecipient)
463cdf0e10cSrcweir    def addBccRecipient( self, bccrecipient ):
464cdf0e10cSrcweir        if dbg:
465bc42e70bSAriel Constenla-Haile            out.write("PyMailMessage.addBccRecipient%s\n" % bccrecipient)
466bb7facceSAriel Constenla-Haile        self.bccrecipients.append(bccrecipient)
467cdf0e10cSrcweir    def getRecipients( self ):
468cdf0e10cSrcweir        if dbg:
469bc42e70bSAriel Constenla-Haile            out.write("PyMailMessage.getRecipients%s\n" % self.recipients)
470bb7facceSAriel Constenla-Haile        return tuple(self.recipients)
471cdf0e10cSrcweir    def getCcRecipients( self ):
472cdf0e10cSrcweir        if dbg:
473bc42e70bSAriel Constenla-Haile            out.write("PyMailMessage.getCcRecipients%s\n" % self.ccrecipients)
474bb7facceSAriel Constenla-Haile        return tuple(self.ccrecipients)
475cdf0e10cSrcweir    def getBccRecipients( self ):
476cdf0e10cSrcweir        if dbg:
477bc42e70bSAriel Constenla-Haile            out.write("PyMailMessage.getBccRecipients%s\n" % self.bccrecipients)
478bb7facceSAriel Constenla-Haile        return tuple(self.bccrecipients)
479cdf0e10cSrcweir    def addAttachment( self, aMailAttachment ):
480cdf0e10cSrcweir        if dbg:
481bc42e70bSAriel Constenla-Haile            out.write("PyMailMessage.addAttachment\n")
482bb7facceSAriel Constenla-Haile        self.aMailAttachments.append(aMailAttachment)
483cdf0e10cSrcweir    def getAttachments( self ):
484cdf0e10cSrcweir        if dbg:
485bc42e70bSAriel Constenla-Haile            out.write("PyMailMessage.getAttachments\n")
486bb7facceSAriel Constenla-Haile        return tuple(self.aMailAttachments)
487bb7facceSAriel Constenla-Haile
488cdf0e10cSrcweir
489cdf0e10cSrcweir# pythonloader looks for a static g_ImplementationHelper variable
490cdf0e10cSrcweirg_ImplementationHelper = unohelper.ImplementationHelper()
491cdf0e10cSrcweirg_ImplementationHelper.addImplementation( \
492cdf0e10cSrcweir        PyMailServiceProvider, "org.openoffice.pyuno.MailServiceProvider",
493cdf0e10cSrcweir                ("com.sun.star.mail.MailServiceProvider",),)
494cdf0e10cSrcweirg_ImplementationHelper.addImplementation( \
495cdf0e10cSrcweir        PyMailMessage, "org.openoffice.pyuno.MailMessage",
496cdf0e10cSrcweir                ("com.sun.star.mail.MailMessage",),)
497