xref: /AOO41X/main/scripting/source/pyprov/mailmerge.py (revision 7d9fa7c3b3b11fd5ae76034517c71005d0e61826)
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
69cdf0e10cSrcweir
70cdf0e10cSrcweirclass PyMailSMTPService(unohelper.Base, XSmtpService):
71cdf0e10cSrcweir    def __init__( self, ctx ):
72cdf0e10cSrcweir        self.ctx = ctx
73cdf0e10cSrcweir        self.listeners = []
74cdf0e10cSrcweir        self.supportedtypes = ('Insecure', 'Ssl')
75cdf0e10cSrcweir        self.server = None
76cdf0e10cSrcweir        self.connectioncontext = None
77bb7facceSAriel Constenla-Haile        self.notify = EventObject(self)
78cdf0e10cSrcweir        if dbg:
79*7d9fa7c3SPedro Giffuni            print("PyMailSMPTService init", file=sys.stderr)
80cdf0e10cSrcweir    def addConnectionListener(self, xListener):
81cdf0e10cSrcweir        if dbg:
82*7d9fa7c3SPedro Giffuni            print("PyMailSMPTService addConnectionListener", file=sys.stderr)
83cdf0e10cSrcweir        self.listeners.append(xListener)
84cdf0e10cSrcweir    def removeConnectionListener(self, xListener):
85cdf0e10cSrcweir        if dbg:
86*7d9fa7c3SPedro Giffuni            print("PyMailSMPTService removeConnectionListener", file=sys.stderr)
87cdf0e10cSrcweir        self.listeners.remove(xListener)
88cdf0e10cSrcweir    def getSupportedConnectionTypes(self):
89cdf0e10cSrcweir        if dbg:
90*7d9fa7c3SPedro Giffuni            print("PyMailSMPTService getSupportedConnectionTypes", file=sys.stderr)
91cdf0e10cSrcweir        return self.supportedtypes
92cdf0e10cSrcweir    def connect(self, xConnectionContext, xAuthenticator):
93cdf0e10cSrcweir        self.connectioncontext = xConnectionContext
94cdf0e10cSrcweir        if dbg:
95*7d9fa7c3SPedro Giffuni            print("PyMailSMPTService connect", file=sys.stderr)
96d3d1f4e0SAriel Constenla-Haile
97cdf0e10cSrcweir        server = xConnectionContext.getValueByName("ServerName")
98cdf0e10cSrcweir        if dbg:
99*7d9fa7c3SPedro Giffuni            print("ServerName: %s" % server, file=sys.stderr)
100d3d1f4e0SAriel Constenla-Haile
101cdf0e10cSrcweir        port = xConnectionContext.getValueByName("Port")
102cdf0e10cSrcweir        if dbg:
103*7d9fa7c3SPedro Giffuni            print("Port: %d" % port, file=sys.stderr)
104d3d1f4e0SAriel Constenla-Haile
105d3d1f4e0SAriel Constenla-Haile        tout = xConnectionContext.getValueByName("Timeout")
106d3d1f4e0SAriel Constenla-Haile        if dbg:
107*7d9fa7c3SPedro Giffuni            print(isinstance(tout,int), file=sys.stderr)
108d3d1f4e0SAriel Constenla-Haile        if not isinstance(tout,int):
109d3d1f4e0SAriel Constenla-Haile            tout = _GLOBAL_DEFAULT_TIMEOUT
110d3d1f4e0SAriel Constenla-Haile        if dbg:
111*7d9fa7c3SPedro Giffuni            print("Timeout: %s" % str(tout), file=sys.stderr)
112d3d1f4e0SAriel Constenla-Haile
113d3d1f4e0SAriel Constenla-Haile        self.server = smtplib.SMTP(server, port,timeout=tout)
114cdf0e10cSrcweir        if dbg:
115cdf0e10cSrcweir            self.server.set_debuglevel(1)
116d3d1f4e0SAriel Constenla-Haile
117cdf0e10cSrcweir        connectiontype = xConnectionContext.getValueByName("ConnectionType")
118cdf0e10cSrcweir        if dbg:
119*7d9fa7c3SPedro Giffuni            print("ConnectionType: %s" % connectiontype, file=sys.stderr)
120d3d1f4e0SAriel Constenla-Haile
121bb7facceSAriel Constenla-Haile        if connectiontype.upper() == 'SSL':
122cdf0e10cSrcweir            self.server.ehlo()
123cdf0e10cSrcweir            self.server.starttls()
124cdf0e10cSrcweir            self.server.ehlo()
125cdf0e10cSrcweir
126cdf0e10cSrcweir        user = xAuthenticator.getUserName().encode('ascii')
127cdf0e10cSrcweir        password = xAuthenticator.getPassword().encode('ascii')
128cdf0e10cSrcweir        if user != '':
129cdf0e10cSrcweir            if dbg:
130*7d9fa7c3SPedro Giffuni                print('Logging in, username of', user, file=sys.stderr)
131cdf0e10cSrcweir            self.server.login(user, password)
132cdf0e10cSrcweir
133cdf0e10cSrcweir        for listener in self.listeners:
134cdf0e10cSrcweir            listener.connected(self.notify)
135cdf0e10cSrcweir    def disconnect(self):
136cdf0e10cSrcweir        if dbg:
137*7d9fa7c3SPedro Giffuni            print("PyMailSMPTService disconnect", file=sys.stderr)
138cdf0e10cSrcweir        if self.server:
139cdf0e10cSrcweir            self.server.quit()
140cdf0e10cSrcweir            self.server = None
141cdf0e10cSrcweir        for listener in self.listeners:
142cdf0e10cSrcweir            listener.disconnected(self.notify)
143cdf0e10cSrcweir    def isConnected(self):
144cdf0e10cSrcweir        if dbg:
145*7d9fa7c3SPedro Giffuni            print("PyMailSMPTService isConnected", file=sys.stderr)
146cdf0e10cSrcweir        return self.server != None
147cdf0e10cSrcweir    def getCurrentConnectionContext(self):
148cdf0e10cSrcweir        if dbg:
149*7d9fa7c3SPedro Giffuni            print("PyMailSMPTService getCurrentConnectionContext", file=sys.stderr)
150cdf0e10cSrcweir        return self.connectioncontext
151cdf0e10cSrcweir    def sendMailMessage(self, xMailMessage):
152cdf0e10cSrcweir        COMMASPACE = ', '
153cdf0e10cSrcweir
154cdf0e10cSrcweir        if dbg:
155*7d9fa7c3SPedro Giffuni            print("PyMailSMPTService sendMailMessage", file=sys.stderr)
156cdf0e10cSrcweir        recipients = xMailMessage.getRecipients()
157cdf0e10cSrcweir        sendermail = xMailMessage.SenderAddress
158cdf0e10cSrcweir        sendername = xMailMessage.SenderName
159cdf0e10cSrcweir        subject = xMailMessage.Subject
160cdf0e10cSrcweir        ccrecipients = xMailMessage.getCcRecipients()
161cdf0e10cSrcweir        bccrecipients = xMailMessage.getBccRecipients()
162cdf0e10cSrcweir        if dbg:
163*7d9fa7c3SPedro Giffuni            print("PyMailSMPTService subject", subject, file=sys.stderr)
164*7d9fa7c3SPedro Giffuni            print("PyMailSMPTService from", sendername.encode('utf-8'), file=sys.stderr)
165*7d9fa7c3SPedro Giffuni            print("PyMailSMTPService from", sendermail, file=sys.stderr)
166*7d9fa7c3SPedro Giffuni            print("PyMailSMPTService send to", recipients, file=sys.stderr)
167cdf0e10cSrcweir
168cdf0e10cSrcweir        attachments = xMailMessage.getAttachments()
169cdf0e10cSrcweir
170cdf0e10cSrcweir        textmsg = Message()
171cdf0e10cSrcweir
172cdf0e10cSrcweir        content = xMailMessage.Body
173cdf0e10cSrcweir        flavors = content.getTransferDataFlavors()
174cdf0e10cSrcweir        if dbg:
175*7d9fa7c3SPedro Giffuni            print("PyMailSMPTService flavors len", len(flavors), file=sys.stderr)
176cdf0e10cSrcweir
177cdf0e10cSrcweir        #Use first flavor that's sane for an email body
178cdf0e10cSrcweir        for flavor in flavors:
179cdf0e10cSrcweir            if flavor.MimeType.find('text/html') != -1 or flavor.MimeType.find('text/plain') != -1:
180cdf0e10cSrcweir                if dbg:
181*7d9fa7c3SPedro Giffuni                    print("PyMailSMPTService mimetype is", flavor.MimeType, file=sys.stderr)
182cdf0e10cSrcweir                textbody = content.getTransferData(flavor)
183cdf0e10cSrcweir                try:
184cdf0e10cSrcweir                    textbody = textbody.value
185cdf0e10cSrcweir                except:
186cdf0e10cSrcweir                    pass
187cdf0e10cSrcweir                textbody = textbody.encode('utf-8')
188cdf0e10cSrcweir
189cdf0e10cSrcweir                if len(textbody):
190cdf0e10cSrcweir                    mimeEncoding = re.sub("charset=.*", "charset=UTF-8", flavor.MimeType)
191cdf0e10cSrcweir                    if mimeEncoding.find('charset=UTF-8') == -1:
192cdf0e10cSrcweir                        mimeEncoding = mimeEncoding + "; charset=UTF-8"
193cdf0e10cSrcweir                    textmsg['Content-Type'] = mimeEncoding
194cdf0e10cSrcweir                    textmsg['MIME-Version'] = '1.0'
195cdf0e10cSrcweir                    textmsg.set_payload(textbody)
196cdf0e10cSrcweir
197cdf0e10cSrcweir                break
198cdf0e10cSrcweir
199cdf0e10cSrcweir        if (len(attachments)):
200cdf0e10cSrcweir            msg = MIMEMultipart()
201cdf0e10cSrcweir            msg.epilogue = ''
202cdf0e10cSrcweir            msg.attach(textmsg)
203cdf0e10cSrcweir        else:
204cdf0e10cSrcweir            msg = textmsg
205cdf0e10cSrcweir
206cdf0e10cSrcweir        hdr = Header(sendername, 'utf-8')
207cdf0e10cSrcweir        hdr.append('<'+sendermail+'>','us-ascii')
208cdf0e10cSrcweir        msg['Subject'] = subject
209cdf0e10cSrcweir        msg['From'] = hdr
210cdf0e10cSrcweir        msg['To'] = COMMASPACE.join(recipients)
211cdf0e10cSrcweir        if len(ccrecipients):
212cdf0e10cSrcweir            msg['Cc'] = COMMASPACE.join(ccrecipients)
213cdf0e10cSrcweir        if xMailMessage.ReplyToAddress != '':
214cdf0e10cSrcweir            msg['Reply-To'] = xMailMessage.ReplyToAddress
215cdf0e10cSrcweir
216cdf0e10cSrcweir        mailerstring = "OpenOffice.org 2.0 via Caolan's mailmerge component"
217cdf0e10cSrcweir        try:
218cdf0e10cSrcweir            ctx = uno.getComponentContext()
219cdf0e10cSrcweir            aConfigProvider = ctx.ServiceManager.createInstance("com.sun.star.configuration.ConfigurationProvider")
220cdf0e10cSrcweir            prop = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
221cdf0e10cSrcweir            prop.Name = "nodepath"
222cdf0e10cSrcweir            prop.Value = "/org.openoffice.Setup/Product"
223cdf0e10cSrcweir            aSettings = aConfigProvider.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess",
224cdf0e10cSrcweir                    (prop,))
225cdf0e10cSrcweir            mailerstring = aSettings.getByName("ooName") + " " + \
226cdf0e10cSrcweir                    aSettings.getByName("ooSetupVersion") + " via Caolan's mailmerge component"
227cdf0e10cSrcweir        except:
228cdf0e10cSrcweir            pass
229cdf0e10cSrcweir
230cdf0e10cSrcweir        msg['X-Mailer'] = mailerstring
231cdf0e10cSrcweir        msg['Date'] = formatdate(localtime=True)
232cdf0e10cSrcweir
233cdf0e10cSrcweir        for attachment in attachments:
234cdf0e10cSrcweir            content = attachment.Data
235cdf0e10cSrcweir            flavors = content.getTransferDataFlavors()
236cdf0e10cSrcweir            flavor = flavors[0]
237cdf0e10cSrcweir            ctype = flavor.MimeType
238cdf0e10cSrcweir            maintype, subtype = ctype.split('/', 1)
239cdf0e10cSrcweir            msgattachment = MIMEBase(maintype, subtype)
240cdf0e10cSrcweir            data = content.getTransferData(flavor)
241cdf0e10cSrcweir            msgattachment.set_payload(data)
242cdf0e10cSrcweir            Encoders.encode_base64(msgattachment)
243a0292563SAriel Constenla-Haile            fname = attachment.ReadableName
244a0292563SAriel Constenla-Haile            try:
245a0292563SAriel Constenla-Haile                fname.encode('ascii')
246a0292563SAriel Constenla-Haile            except:
247a0292563SAriel Constenla-Haile                fname = ('utf-8','',fname.encode('utf-8'))
248cdf0e10cSrcweir            msgattachment.add_header('Content-Disposition', 'attachment', \
249a0292563SAriel Constenla-Haile                    filename=fname)
250cdf0e10cSrcweir            msg.attach(msgattachment)
251cdf0e10cSrcweir
252cdf0e10cSrcweir        uniquer = {}
253cdf0e10cSrcweir        for key in recipients:
254cdf0e10cSrcweir            uniquer[key] = True
255cdf0e10cSrcweir        if len(ccrecipients):
256cdf0e10cSrcweir            for key in ccrecipients:
257cdf0e10cSrcweir                uniquer[key] = True
258cdf0e10cSrcweir        if len(bccrecipients):
259cdf0e10cSrcweir            for key in bccrecipients:
260cdf0e10cSrcweir                uniquer[key] = True
261*7d9fa7c3SPedro Giffuni        truerecipients = list(uniquer.keys())
262cdf0e10cSrcweir
263cdf0e10cSrcweir        if dbg:
264*7d9fa7c3SPedro Giffuni            print("PyMailSMPTService recipients are", truerecipients, file=sys.stderr)
265cdf0e10cSrcweir
266cdf0e10cSrcweir        self.server.sendmail(sendermail, truerecipients, msg.as_string())
267cdf0e10cSrcweir
268cdf0e10cSrcweirclass PyMailIMAPService(unohelper.Base, XMailService):
269cdf0e10cSrcweir    def __init__( self, ctx ):
270cdf0e10cSrcweir        self.ctx = ctx
271cdf0e10cSrcweir        self.listeners = []
272cdf0e10cSrcweir        self.supportedtypes = ('Insecure', 'Ssl')
273cdf0e10cSrcweir        self.server = None
274cdf0e10cSrcweir        self.connectioncontext = None
275bb7facceSAriel Constenla-Haile        self.notify = EventObject(self)
276cdf0e10cSrcweir        if dbg:
277*7d9fa7c3SPedro Giffuni            print("PyMailIMAPService init", file=sys.stderr)
278cdf0e10cSrcweir    def addConnectionListener(self, xListener):
279cdf0e10cSrcweir        if dbg:
280*7d9fa7c3SPedro Giffuni            print("PyMailIMAPService addConnectionListener", file=sys.stderr)
281cdf0e10cSrcweir        self.listeners.append(xListener)
282cdf0e10cSrcweir    def removeConnectionListener(self, xListener):
283cdf0e10cSrcweir        if dbg:
284*7d9fa7c3SPedro Giffuni            print("PyMailIMAPService removeConnectionListener", file=sys.stderr)
285cdf0e10cSrcweir        self.listeners.remove(xListener)
286cdf0e10cSrcweir    def getSupportedConnectionTypes(self):
287cdf0e10cSrcweir        if dbg:
288*7d9fa7c3SPedro Giffuni            print("PyMailIMAPService getSupportedConnectionTypes", file=sys.stderr)
289cdf0e10cSrcweir        return self.supportedtypes
290cdf0e10cSrcweir    def connect(self, xConnectionContext, xAuthenticator):
291cdf0e10cSrcweir        if dbg:
292*7d9fa7c3SPedro Giffuni            print("PyMailIMAPService connect", file=sys.stderr)
293cdf0e10cSrcweir
294cdf0e10cSrcweir        self.connectioncontext = xConnectionContext
295cdf0e10cSrcweir        server = xConnectionContext.getValueByName("ServerName")
296cdf0e10cSrcweir        if dbg:
297*7d9fa7c3SPedro Giffuni            print(server, file=sys.stderr)
298cdf0e10cSrcweir        port = xConnectionContext.getValueByName("Port")
299cdf0e10cSrcweir        if dbg:
300*7d9fa7c3SPedro Giffuni            print(port, file=sys.stderr)
301cdf0e10cSrcweir        connectiontype = xConnectionContext.getValueByName("ConnectionType")
302cdf0e10cSrcweir        if dbg:
303*7d9fa7c3SPedro Giffuni            print(connectiontype, file=sys.stderr)
304*7d9fa7c3SPedro Giffuni        print("BEFORE", file=sys.stderr)
305bb7facceSAriel Constenla-Haile        if connectiontype.upper() == 'SSL':
306cdf0e10cSrcweir            self.server = imaplib.IMAP4_SSL(server, port)
307cdf0e10cSrcweir        else:
308cdf0e10cSrcweir            self.server = imaplib.IMAP4(server, port)
309*7d9fa7c3SPedro Giffuni        print("AFTER", file=sys.stderr)
310cdf0e10cSrcweir
311cdf0e10cSrcweir        user = xAuthenticator.getUserName().encode('ascii')
312cdf0e10cSrcweir        password = xAuthenticator.getPassword().encode('ascii')
313cdf0e10cSrcweir        if user != '':
314cdf0e10cSrcweir            if dbg:
315*7d9fa7c3SPedro Giffuni                print('Logging in, username of', user, file=sys.stderr)
316cdf0e10cSrcweir            self.server.login(user, password)
317cdf0e10cSrcweir
318cdf0e10cSrcweir        for listener in self.listeners:
319cdf0e10cSrcweir            listener.connected(self.notify)
320cdf0e10cSrcweir    def disconnect(self):
321cdf0e10cSrcweir        if dbg:
322*7d9fa7c3SPedro Giffuni            print("PyMailIMAPService disconnect", file=sys.stderr)
323cdf0e10cSrcweir        if self.server:
324cdf0e10cSrcweir            self.server.logout()
325cdf0e10cSrcweir            self.server = None
326cdf0e10cSrcweir        for listener in self.listeners:
327cdf0e10cSrcweir            listener.disconnected(self.notify)
328cdf0e10cSrcweir    def isConnected(self):
329cdf0e10cSrcweir        if dbg:
330*7d9fa7c3SPedro Giffuni            print("PyMailIMAPService isConnected", file=sys.stderr)
331cdf0e10cSrcweir        return self.server != None
332cdf0e10cSrcweir    def getCurrentConnectionContext(self):
333cdf0e10cSrcweir        if dbg:
334*7d9fa7c3SPedro Giffuni            print("PyMailIMAPService getCurrentConnectionContext", file=sys.stderr)
335cdf0e10cSrcweir        return self.connectioncontext
336cdf0e10cSrcweir
337cdf0e10cSrcweirclass PyMailPOP3Service(unohelper.Base, XMailService):
338cdf0e10cSrcweir    def __init__( self, ctx ):
339cdf0e10cSrcweir        self.ctx = ctx
340cdf0e10cSrcweir        self.listeners = []
341cdf0e10cSrcweir        self.supportedtypes = ('Insecure', 'Ssl')
342cdf0e10cSrcweir        self.server = None
343cdf0e10cSrcweir        self.connectioncontext = None
344bb7facceSAriel Constenla-Haile        self.notify = EventObject(self)
345cdf0e10cSrcweir        if dbg:
346*7d9fa7c3SPedro Giffuni            print("PyMailPOP3Service init", file=sys.stderr)
347cdf0e10cSrcweir    def addConnectionListener(self, xListener):
348cdf0e10cSrcweir        if dbg:
349*7d9fa7c3SPedro Giffuni            print("PyMailPOP3Service addConnectionListener", file=sys.stderr)
350cdf0e10cSrcweir        self.listeners.append(xListener)
351cdf0e10cSrcweir    def removeConnectionListener(self, xListener):
352cdf0e10cSrcweir        if dbg:
353*7d9fa7c3SPedro Giffuni            print("PyMailPOP3Service removeConnectionListener", file=sys.stderr)
354cdf0e10cSrcweir        self.listeners.remove(xListener)
355cdf0e10cSrcweir    def getSupportedConnectionTypes(self):
356cdf0e10cSrcweir        if dbg:
357*7d9fa7c3SPedro Giffuni            print("PyMailPOP3Service getSupportedConnectionTypes", file=sys.stderr)
358cdf0e10cSrcweir        return self.supportedtypes
359cdf0e10cSrcweir    def connect(self, xConnectionContext, xAuthenticator):
360cdf0e10cSrcweir        if dbg:
361*7d9fa7c3SPedro Giffuni            print("PyMailPOP3Service connect", file=sys.stderr)
362cdf0e10cSrcweir
363cdf0e10cSrcweir        self.connectioncontext = xConnectionContext
364cdf0e10cSrcweir        server = xConnectionContext.getValueByName("ServerName")
365cdf0e10cSrcweir        if dbg:
366*7d9fa7c3SPedro Giffuni            print(server, file=sys.stderr)
367cdf0e10cSrcweir        port = xConnectionContext.getValueByName("Port")
368cdf0e10cSrcweir        if dbg:
369*7d9fa7c3SPedro Giffuni            print(port, file=sys.stderr)
370cdf0e10cSrcweir        connectiontype = xConnectionContext.getValueByName("ConnectionType")
371cdf0e10cSrcweir        if dbg:
372*7d9fa7c3SPedro Giffuni            print(connectiontype, file=sys.stderr)
373*7d9fa7c3SPedro Giffuni        print("BEFORE", file=sys.stderr)
374bb7facceSAriel Constenla-Haile        if connectiontype.upper() == 'SSL':
375cdf0e10cSrcweir            self.server = poplib.POP3_SSL(server, port)
376cdf0e10cSrcweir        else:
377d3d1f4e0SAriel Constenla-Haile            tout = xConnectionContext.getValueByName("Timeout")
378d3d1f4e0SAriel Constenla-Haile            if dbg:
379*7d9fa7c3SPedro Giffuni                print(isinstance(tout,int), file=sys.stderr)
380d3d1f4e0SAriel Constenla-Haile            if not isinstance(tout,int):
381d3d1f4e0SAriel Constenla-Haile                tout = _GLOBAL_DEFAULT_TIMEOUT
382d3d1f4e0SAriel Constenla-Haile            if dbg:
383*7d9fa7c3SPedro Giffuni                print("Timeout: %s" % str(tout), file=sys.stderr)
384d3d1f4e0SAriel Constenla-Haile            self.server = poplib.POP3(server, port, timeout=tout)
385*7d9fa7c3SPedro Giffuni        print("AFTER", file=sys.stderr)
386cdf0e10cSrcweir
387cdf0e10cSrcweir        user = xAuthenticator.getUserName().encode('ascii')
388cdf0e10cSrcweir        password = xAuthenticator.getPassword().encode('ascii')
389cdf0e10cSrcweir        if dbg:
390*7d9fa7c3SPedro Giffuni            print('Logging in, username of', user, file=sys.stderr)
391cdf0e10cSrcweir        self.server.user(user)
392bb7facceSAriel Constenla-Haile        self.server.pass_(password)
393cdf0e10cSrcweir
394cdf0e10cSrcweir        for listener in self.listeners:
395cdf0e10cSrcweir            listener.connected(self.notify)
396cdf0e10cSrcweir    def disconnect(self):
397cdf0e10cSrcweir        if dbg:
398*7d9fa7c3SPedro Giffuni            print("PyMailPOP3Service disconnect", file=sys.stderr)
399cdf0e10cSrcweir        if self.server:
400cdf0e10cSrcweir            self.server.quit()
401cdf0e10cSrcweir            self.server = None
402cdf0e10cSrcweir        for listener in self.listeners:
403cdf0e10cSrcweir            listener.disconnected(self.notify)
404cdf0e10cSrcweir    def isConnected(self):
405cdf0e10cSrcweir        if dbg:
406*7d9fa7c3SPedro Giffuni            print("PyMailPOP3Service isConnected", file=sys.stderr)
407cdf0e10cSrcweir        return self.server != None
408cdf0e10cSrcweir    def getCurrentConnectionContext(self):
409cdf0e10cSrcweir        if dbg:
410*7d9fa7c3SPedro Giffuni            print("PyMailPOP3Service getCurrentConnectionContext", file=sys.stderr)
411cdf0e10cSrcweir        return self.connectioncontext
412cdf0e10cSrcweir
413cdf0e10cSrcweirclass PyMailServiceProvider(unohelper.Base, XMailServiceProvider):
414cdf0e10cSrcweir    def __init__( self, ctx ):
415cdf0e10cSrcweir        if dbg:
416*7d9fa7c3SPedro Giffuni            print("PyMailServiceProvider init", file=sys.stderr)
417cdf0e10cSrcweir        self.ctx = ctx
418cdf0e10cSrcweir    def create(self, aType):
419cdf0e10cSrcweir        if dbg:
420*7d9fa7c3SPedro Giffuni            print("PyMailServiceProvider create with", aType, file=sys.stderr)
421cdf0e10cSrcweir        if aType == SMTP:
422cdf0e10cSrcweir            return PyMailSMTPService(self.ctx);
423cdf0e10cSrcweir        elif aType == POP3:
424cdf0e10cSrcweir            return PyMailPOP3Service(self.ctx);
425cdf0e10cSrcweir        elif aType == IMAP:
426cdf0e10cSrcweir            return PyMailIMAPService(self.ctx);
427cdf0e10cSrcweir        else:
428*7d9fa7c3SPedro Giffuni            print("PyMailServiceProvider, unknown TYPE", aType, file=sys.stderr)
429cdf0e10cSrcweir
430cdf0e10cSrcweirclass PyMailMessage(unohelper.Base, XMailMessage):
431cdf0e10cSrcweir    def __init__( self, ctx, sTo='', sFrom='', Subject='', Body=None, aMailAttachment=None ):
432cdf0e10cSrcweir        if dbg:
433*7d9fa7c3SPedro Giffuni            print("PyMailMessage init", file=sys.stderr)
434cdf0e10cSrcweir        self.ctx = ctx
435cdf0e10cSrcweir
436bb7facceSAriel Constenla-Haile        self.recipients = [sTo]
437bb7facceSAriel Constenla-Haile        self.ccrecipients = []
438bb7facceSAriel Constenla-Haile        self.bccrecipients = []
439bb7facceSAriel Constenla-Haile        self.aMailAttachments = []
440cdf0e10cSrcweir        if aMailAttachment != None:
441bb7facceSAriel Constenla-Haile            self.aMailAttachments.append(aMailAttachment)
442cdf0e10cSrcweir
443cdf0e10cSrcweir        self.SenderName, self.SenderAddress = parseaddr(sFrom)
444cdf0e10cSrcweir        self.ReplyToAddress = sFrom
445cdf0e10cSrcweir        self.Subject = Subject
446cdf0e10cSrcweir        self.Body = Body
447cdf0e10cSrcweir        if dbg:
448*7d9fa7c3SPedro Giffuni            print("post PyMailMessage init", file=sys.stderr)
449cdf0e10cSrcweir    def addRecipient( self, recipient ):
450cdf0e10cSrcweir        if dbg:
451*7d9fa7c3SPedro Giffuni            print("PyMailMessage.addRecipient", recipient, file=sys.stderr)
452bb7facceSAriel Constenla-Haile        self.recipients.append(recipient)
453cdf0e10cSrcweir    def addCcRecipient( self, ccrecipient ):
454cdf0e10cSrcweir        if dbg:
455*7d9fa7c3SPedro Giffuni            print("PyMailMessage.addCcRecipient", ccrecipient, file=sys.stderr)
456bb7facceSAriel Constenla-Haile        self.ccrecipients.append(ccrecipient)
457cdf0e10cSrcweir    def addBccRecipient( self, bccrecipient ):
458cdf0e10cSrcweir        if dbg:
459*7d9fa7c3SPedro Giffuni            print("PyMailMessage.addBccRecipient", bccrecipient, file=sys.stderr)
460bb7facceSAriel Constenla-Haile        self.bccrecipients.append(bccrecipient)
461cdf0e10cSrcweir    def getRecipients( self ):
462cdf0e10cSrcweir        if dbg:
463*7d9fa7c3SPedro Giffuni            print("PyMailMessage.getRecipients", self.recipients, file=sys.stderr)
464bb7facceSAriel Constenla-Haile        return tuple(self.recipients)
465cdf0e10cSrcweir    def getCcRecipients( self ):
466cdf0e10cSrcweir        if dbg:
467*7d9fa7c3SPedro Giffuni            print("PyMailMessage.getCcRecipients", self.ccrecipients, file=sys.stderr)
468bb7facceSAriel Constenla-Haile        return tuple(self.ccrecipients)
469cdf0e10cSrcweir    def getBccRecipients( self ):
470cdf0e10cSrcweir        if dbg:
471*7d9fa7c3SPedro Giffuni            print("PyMailMessage.getBccRecipients", self.bccrecipients, file=sys.stderr)
472bb7facceSAriel Constenla-Haile        return tuple(self.bccrecipients)
473cdf0e10cSrcweir    def addAttachment( self, aMailAttachment ):
474cdf0e10cSrcweir        if dbg:
475*7d9fa7c3SPedro Giffuni            print("PyMailMessage.addAttachment", file=sys.stderr)
476bb7facceSAriel Constenla-Haile        self.aMailAttachments.append(aMailAttachment)
477cdf0e10cSrcweir    def getAttachments( self ):
478cdf0e10cSrcweir        if dbg:
479*7d9fa7c3SPedro Giffuni            print("PyMailMessage.getAttachments", file=sys.stderr)
480bb7facceSAriel Constenla-Haile        return tuple(self.aMailAttachments)
481bb7facceSAriel Constenla-Haile
482cdf0e10cSrcweir
483cdf0e10cSrcweir# pythonloader looks for a static g_ImplementationHelper variable
484cdf0e10cSrcweirg_ImplementationHelper = unohelper.ImplementationHelper()
485cdf0e10cSrcweirg_ImplementationHelper.addImplementation( \
486cdf0e10cSrcweir        PyMailServiceProvider, "org.openoffice.pyuno.MailServiceProvider",
487cdf0e10cSrcweir                ("com.sun.star.mail.MailServiceProvider",),)
488cdf0e10cSrcweirg_ImplementationHelper.addImplementation( \
489cdf0e10cSrcweir        PyMailMessage, "org.openoffice.pyuno.MailMessage",
490cdf0e10cSrcweir                ("com.sun.star.mail.MailMessage",),)
491