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