#!/bin/python # Caolan McNamara caolanm@redhat.com # a simple email mailmerge component # manual installation for hackers, not necessary for users # cp mailmerge.py /usr/lib/openoffice.org2.0/program # cd /usr/lib/openoffice.org2.0/program # ./unopkg add --shared mailmerge.py # edit ~/.openoffice.org2/user/registry/data/org/openoffice/Office/Writer.xcu # and change EMailSupported to as follows... # # true # import unohelper import uno import re #to implement com::sun::star::mail::XMailServiceProvider from com.sun.star.mail import XMailServiceProvider from com.sun.star.mail import XMailService from com.sun.star.mail import XSmtpService from com.sun.star.mail import XConnectionListener from com.sun.star.mail import XAuthenticator from com.sun.star.mail import XMailMessage from com.sun.star.mail.MailServiceType import SMTP from com.sun.star.mail.MailServiceType import POP3 from com.sun.star.mail.MailServiceType import IMAP from com.sun.star.uno import XCurrentContext from com.sun.star.lang import IllegalArgumentException from com.sun.star.lang import EventObject from com.sun.star.mail import SendMailMessageFailedException from email.MIMEBase import MIMEBase from email.Message import Message from email import Encoders from email.Header import Header from email.MIMEMultipart import MIMEMultipart from email.Utils import formatdate import sys, smtplib, imaplib, poplib outputlog = open("c:/mailmerge.log","w") print >> outputlog, "hello world" dbg = True class PyMailSMTPService(unohelper.Base, XSmtpService): def __init__( self, ctx ): self.ctx = ctx self.listeners = [] self.supportedtypes = ('Insecure', 'Ssl') self.server = None self.connectioncontext = None self.notify = EventObject() if dbg: print >> outputlog, "PyMailSMPTService init" outputlog.flush() def addConnectionListener(self, xListener): if dbg: print >> outputlog, "PyMailSMPTService addConnectionListener" outputlog.flush() self.listeners.append(xListener) def removeConnectionListener(self, xListener): if dbg: print >> outputlog, "PyMailSMPTService removeConnectionListener" outputlog.flush() self.listeners.remove(xListener) def getSupportedConnectionTypes(self): if dbg: print >> outputlog, "PyMailSMPTService getSupportedConnectionTypes" outputlog.flush() return self.supportedtypes def connect(self, xConnectionContext, xAuthenticator): self.connectioncontext = xConnectionContext if dbg: print >> outputlog, "PyMailSMPTService connect" outputlog.flush() server = xConnectionContext.getValueByName("ServerName") if dbg: print >> outputlog, server outputlog.flush() port = xConnectionContext.getValueByName("Port") if dbg: print >> outputlog, port outputlog.flush() self.server = smtplib.SMTP(server, port) if dbg: self.server.set_debuglevel(1) connectiontype = xConnectionContext.getValueByName("ConnectionType") if dbg: print >> outputlog, connectiontype outputlog.flush() if connectiontype == 'Ssl': self.server.starttls() user = xAuthenticator.getUserName() password = xAuthenticator.getPassword() if user != '': if dbg: print >> outputlog, 'Logging in, username of', user outputlog.flush() self.server.login(user, password) for listener in self.listeners: listener.connected(self.notify) def disconnect(self): if dbg: print >> outputlog, "PyMailSMPTService disconnect" outputlog.flush() if self.server: self.server.quit() self.server = None for listener in self.listeners: listener.disconnected(self.notify) def isConnected(self): if dbg: print >> outputlog, "PyMailSMPTService isConnected" outputlog.flush() return self.server != None def getCurrentConnectionContext(self): if dbg: print >> outputlog, "PyMailSMPTService getCurrentConnectionContext" outputlog.flush() return self.connectioncontext def sendMailMessage(self, xMailMessage): COMMASPACE = ', ' if dbg: print >> outputlog, "PyMailSMPTService sendMailMessage" outputlog.flush() recipients = xMailMessage.getRecipients() sendermail = xMailMessage.SenderAddress sendername = xMailMessage.SenderName subject = xMailMessage.Subject ccrecipients = xMailMessage.getCcRecipients() bccrecipients = xMailMessage.getBccRecipients() if dbg: print >> outputlog, "PyMailSMPTService subject", subject outputlog.flush() print >> outputlog, "PyMailSMPTService from", sendername.encode('utf-8') outputlog.flush() print >> outputlog, "PyMailSMTPService from", sendermail outputlog.flush() print >> outputlog, "PyMailSMPTService send to", recipients outputlog.flush() attachments = xMailMessage.getAttachments() content = xMailMessage.Body flavors = content.getTransferDataFlavors() flavor = flavors[0] if dbg: print >> outputlog, "PyMailSMPTService mimetype is", flavor.MimeType outputlog.flush() textbody = content.getTransferData(flavor) textmsg = Message() mimeEncoding = re.sub("charset=.*", "charset=UTF-8", flavor.MimeType) textmsg['Content-Type'] = mimeEncoding textmsg['MIME-Version'] = '1.0' textmsg.set_payload(textbody.encode('utf-8')) if (len(attachments)): msg = MIMEMultipart() msg.epilogue = '' msg.attach(textmsg) else: msg = textmsg hdr = Header(sendername, 'utf-8') hdr.append('<'+sendermail+'>','us-ascii') msg['Subject'] = subject msg['From'] = hdr msg['To'] = COMMASPACE.join(recipients) if len(ccrecipients): msg['Cc'] = COMMASPACE.join(ccrecipients) if xMailMessage.ReplyToAddress != '': msg['Reply-To'] = xMailMessage.ReplyToAddress msg['X-Mailer'] = "OpenOffice.org 2.0 via Caolan's mailmerge component" msg['Date'] = formatdate(localtime=True) for attachment in attachments: content = attachment.Data flavors = content.getTransferDataFlavors() flavor = flavors[0] ctype = flavor.MimeType maintype, subtype = ctype.split('/', 1) msgattachment = MIMEBase(maintype, subtype) data = content.getTransferData(flavor) msgattachment.set_payload(data) Encoders.encode_base64(msgattachment) msgattachment.add_header('Content-Disposition', 'attachment', \ filename=attachment.ReadableName) msg.attach(msgattachment) uniquer = {} for key in recipients: uniquer[key] = True if len(ccrecipients): for key in ccrecipients: uniquer[key] = True if len(bccrecipients): for key in bccrecipients: uniquer[key] = True truerecipients = uniquer.keys() if dbg: print >> outputlog, "PyMailSMPTService recipients are", truerecipients outputlog.flush() self.server.sendmail(sendermail, truerecipients, msg.as_string()) class PyMailIMAPService(unohelper.Base, XMailService): def __init__( self, ctx ): self.ctx = ctx self.listeners = [] self.supportedtypes = ('Insecure', 'Ssl') self.server = None self.connectioncontext = None if dbg: print >> outputlog, "PyMailIMAPService init" outputlog.flush() def addConnectionListener(self, xListener): if dbg: print >> outputlog, "PyMailIMAPService addConnectionListener" outputlog.flush() self.listeners.append(xListener) def removeConnectionListener(self, xListener): if dbg: print >> outputlog, "PyMailIMAPService removeConnectionListener" outputlog.flush() self.listeners.remove(xListener) def getSupportedConnectionTypes(self): if dbg: print >> outputlog, "PyMailIMAPService getSupportedConnectionTypes" outputlog.flush() return self.supportedtypes def connect(self, xConnectionContext, xAuthenticator): if dbg: print >> outputlog, "PyMailIMAPService connect" outputlog.flush() self.connectioncontext = xConnectionContext server = xConnectionContext.getValueByName("ServerName") if dbg: print >> outputlog, server outputlog.flush() port = xConnectionContext.getValueByName("Port") if dbg: print >> outputlog, port outputlog.flush() connectiontype = xConnectionContext.getValueByName("ConnectionType") if dbg: print >> outputlog, connectiontype outputlog.flush() print >> outputlog, "BEFORE" outputlog.flush() if connectiontype == 'Ssl': self.server = imaplib.IMAP4_SSL(server, port) else: self.server = imaplib.IMAP4(server, port) print >> outputlog, "AFTER" outputlog.flush() user = xAuthenticator.getUserName() password = xAuthenticator.getPassword() if user != '': if dbg: print >> outputlog, 'Logging in, username of', user outputlog.flush() self.server.login(user, password) for listener in self.listeners: listener.connected(self.notify) def disconnect(self): if dbg: print >> outputlog, "PyMailIMAPService disconnect" outputlog.flush() if self.server: self.server.logout() self.server = None for listener in self.listeners: listener.disconnected(self.notify) def isConnected(self): if dbg: print >> outputlog, "PyMailIMAPService isConnected" outputlog.flush() return self.server != None def getCurrentConnectionContext(self): if dbg: print >> outputlog, "PyMailIMAPService getCurrentConnectionContext" outputlog.flush() return self.connectioncontext class PyMailPOP3Service(unohelper.Base, XMailService): def __init__( self, ctx ): self.ctx = ctx self.listeners = [] self.supportedtypes = ('Insecure', 'Ssl') self.server = None self.connectioncontext = None if dbg: print >> outputlog, "PyMailPOP3Service init" outputlog.flush() def addConnectionListener(self, xListener): if dbg: print >> outputlog, "PyMailPOP3Service addConnectionListener" outputlog.flush() self.listeners.append(xListener) def removeConnectionListener(self, xListener): if dbg: print >> outputlog, "PyMailPOP3Service removeConnectionListener" outputlog.flush() self.listeners.remove(xListener) def getSupportedConnectionTypes(self): if dbg: print >> outputlog, "PyMailPOP3Service getSupportedConnectionTypes" outputlog.flush() return self.supportedtypes def connect(self, xConnectionContext, xAuthenticator): if dbg: print >> outputlog, "PyMailPOP3Service connect" outputlog.flush() self.connectioncontext = xConnectionContext server = xConnectionContext.getValueByName("ServerName") if dbg: print >> outputlog, server outputlog.flush() port = xConnectionContext.getValueByName("Port") if dbg: print >> outputlog, port outputlog.flush() connectiontype = xConnectionContext.getValueByName("ConnectionType") if dbg: print >> outputlog, connectiontype outputlog.flush() print >> outputlog, "BEFORE" outputlog.flush() if connectiontype == 'Ssl': self.server = poplib.POP3_SSL(server, port) else: self.server = poplib.POP3(server, port) print >> outputlog, "AFTER" outputlog.flush() user = xAuthenticator.getUserName() password = xAuthenticator.getPassword() if dbg: print >> outputlog, 'Logging in, username of', user outputlog.flush() self.server.user(user) self.server.pass_(user, password) for listener in self.listeners: listener.connected(self.notify) def disconnect(self): if dbg: print >> outputlog, "PyMailPOP3Service disconnect" outputlog.flush() if self.server: self.server.quit() self.server = None for listener in self.listeners: listener.disconnected(self.notify) def isConnected(self): if dbg: print >> outputlog, "PyMailPOP3Service isConnected" outputlog.flush() return self.server != None def getCurrentConnectionContext(self): if dbg: print >> outputlog, "PyMailPOP3Service getCurrentConnectionContext" outputlog.flush() return self.connectioncontext class PyMailServiceProvider(unohelper.Base, XMailServiceProvider): def __init__( self, ctx ): if dbg: print >> outputlog, "PyMailServiceProvider init" outputlog.flush() self.ctx = ctx def create(self, aType): if dbg: print >> outputlog, "PyMailServiceProvider create with", aType outputlog.flush() if aType == SMTP: return PyMailSMTPService(self.ctx); elif aType == POP3: return PyMailPOP3Service(self.ctx); elif aType == IMAP: return PyMailIMAPService(self.ctx); else: print >> outputlog, "PyMailServiceProvider, unknown TYPE", aType outputlog.flush() # pythonloader looks for a static g_ImplementationHelper variable g_ImplementationHelper = unohelper.ImplementationHelper() g_ImplementationHelper.addImplementation( \ PyMailServiceProvider, "org.openoffice.pyuno.MailServiceProvider", ("com.sun.star.mail.MailServiceProvider",),)