/usr/lib/libreoffice/program/mailmerge.py is in libreoffice-emailmerge 1:3.5.2-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 | # Caolan McNamara caolanm@redhat.com
# a simple email mailmerge component
# manual installation for hackers, not necessary for users
# cp mailmerge.py /usr/lib/libreoffice/program
# cd /usr/lib/libreoffice/program
# ./unopkg add --shared mailmerge.py
# edit ~/.openoffice.org2/user/registry/data/org/openoffice/Office/Writer.xcu
# and change EMailSupported to as follows...
# <prop oor:name="EMailSupported" oor:type="xs:boolean">
# <value>true</value>
# </prop>
import unohelper
import uno
import re
import os
#to implement com::sun::star::mail::XMailServiceProvider
#and
#to implement com.sun.star.mail.XMailMessage
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
from email.Utils import parseaddr
import sys, smtplib, imaplib, poplib
dbg = False
#no stderr under windows, output to pymailmerge.log
#with no buffering
if dbg and os.name == 'nt':
dbgout = open('pymailmerge.log', 'w', 0)
else:
dbgout = sys.stderr
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 >> dbgout, "PyMailSMPTService init"
def addConnectionListener(self, xListener):
if dbg:
print >> dbgout, "PyMailSMPTService addConnectionListener"
self.listeners.append(xListener)
def removeConnectionListener(self, xListener):
if dbg:
print >> dbgout, "PyMailSMPTService removeConnectionListener"
self.listeners.remove(xListener)
def getSupportedConnectionTypes(self):
if dbg:
print >> dbgout, "PyMailSMPTService getSupportedConnectionTypes"
return self.supportedtypes
def connect(self, xConnectionContext, xAuthenticator):
self.connectioncontext = xConnectionContext
if dbg:
print >> dbgout, "PyMailSMPTService connect"
server = xConnectionContext.getValueByName("ServerName")
if dbg:
print >> dbgout, server
port = int(xConnectionContext.getValueByName("Port"))
if dbg:
print >> dbgout, port
self.server = smtplib.SMTP(server, port)
#stderr not available for us under windows, but
#set_debuglevel outputs there, and so throw
#an exception under windows on debugging mode
#with this enabled
if dbg and os.name != 'nt':
self.server.set_debuglevel(1)
connectiontype = xConnectionContext.getValueByName("ConnectionType")
if dbg:
print >> dbgout, connectiontype
if connectiontype == 'Ssl':
self.server.ehlo()
self.server.starttls()
self.server.ehlo()
user = xAuthenticator.getUserName().encode('ascii')
password = xAuthenticator.getPassword().encode('ascii')
if user != '':
if dbg:
print >> dbgout, 'Logging in, username of', user
self.server.login(user, password)
for listener in self.listeners:
listener.connected(self.notify)
def disconnect(self):
if dbg:
print >> dbgout, "PyMailSMPTService disconnect"
if self.server:
self.server.quit()
self.server = None
for listener in self.listeners:
listener.disconnected(self.notify)
def isConnected(self):
if dbg:
print >> dbgout, "PyMailSMPTService isConnected"
return self.server != None
def getCurrentConnectionContext(self):
if dbg:
print >> dbgout, "PyMailSMPTService getCurrentConnectionContext"
return self.connectioncontext
def sendMailMessage(self, xMailMessage):
COMMASPACE = ', '
if dbg:
print >> dbgout, "PyMailSMPTService sendMailMessage"
recipients = xMailMessage.getRecipients()
sendermail = xMailMessage.SenderAddress
sendername = xMailMessage.SenderName
subject = xMailMessage.Subject
ccrecipients = xMailMessage.getCcRecipients()
bccrecipients = xMailMessage.getBccRecipients()
if dbg:
print >> dbgout, "PyMailSMPTService subject", subject
print >> dbgout, "PyMailSMPTService from", sendername.encode('utf-8')
print >> dbgout, "PyMailSMTPService from", sendermail
print >> dbgout, "PyMailSMPTService send to", recipients
attachments = xMailMessage.getAttachments()
textmsg = Message()
content = xMailMessage.Body
flavors = content.getTransferDataFlavors()
if dbg:
print >> dbgout, "PyMailSMPTService flavors len", len(flavors)
#Use first flavor that's sane for an email body
for flavor in flavors:
if flavor.MimeType.find('text/html') != -1 or flavor.MimeType.find('text/plain') != -1:
if dbg:
print >> dbgout, "PyMailSMPTService mimetype is", flavor.MimeType
textbody = content.getTransferData(flavor)
try:
textbody = textbody.value
except:
pass
textbody = textbody.encode('utf-8')
if len(textbody):
mimeEncoding = re.sub("charset=.*", "charset=UTF-8", flavor.MimeType)
if mimeEncoding.find('charset=UTF-8') == -1:
mimeEncoding = mimeEncoding + "; charset=UTF-8"
textmsg['Content-Type'] = mimeEncoding
textmsg['MIME-Version'] = '1.0'
textmsg.set_payload(textbody)
break
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
mailerstring = "LibreOffice via Caolan's mailmerge component"
try:
ctx = uno.getComponentContext()
aConfigProvider = ctx.ServiceManager.createInstance("com.sun.star.configuration.ConfigurationProvider")
prop = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
prop.Name = "nodepath"
prop.Value = "/org.openoffice.Setup/Product"
aSettings = aConfigProvider.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess",
(prop,))
mailerstring = aSettings.getByName("ooName") + " " + \
aSettings.getByName("ooSetupVersion") + " via Caolan's mailmerge component"
except:
pass
msg['X-Mailer'] = mailerstring
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 >> dbgout, "PyMailSMPTService recipients are", truerecipients
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
self.notify = EventObject()
if dbg:
print >> dbgout, "PyMailIMAPService init"
def addConnectionListener(self, xListener):
if dbg:
print >> dbgout, "PyMailIMAPService addConnectionListener"
self.listeners.append(xListener)
def removeConnectionListener(self, xListener):
if dbg:
print >> dbgout, "PyMailIMAPService removeConnectionListener"
self.listeners.remove(xListener)
def getSupportedConnectionTypes(self):
if dbg:
print >> dbgout, "PyMailIMAPService getSupportedConnectionTypes"
return self.supportedtypes
def connect(self, xConnectionContext, xAuthenticator):
if dbg:
print >> dbgout, "PyMailIMAPService connect"
self.connectioncontext = xConnectionContext
server = xConnectionContext.getValueByName("ServerName")
if dbg:
print >> dbgout, server
port = int(xConnectionContext.getValueByName("Port"))
if dbg:
print >> dbgout, port
connectiontype = xConnectionContext.getValueByName("ConnectionType")
if dbg:
print >> dbgout, connectiontype
print >> dbgout, "BEFORE"
if connectiontype == 'Ssl':
self.server = imaplib.IMAP4_SSL(server, port)
else:
self.server = imaplib.IMAP4(server, port)
print >> dbgout, "AFTER"
user = xAuthenticator.getUserName().encode('ascii')
password = xAuthenticator.getPassword().encode('ascii')
if user != '':
if dbg:
print >> dbgout, 'Logging in, username of', user
self.server.login(user, password)
for listener in self.listeners:
listener.connected(self.notify)
def disconnect(self):
if dbg:
print >> dbgout, "PyMailIMAPService disconnect"
if self.server:
self.server.logout()
self.server = None
for listener in self.listeners:
listener.disconnected(self.notify)
def isConnected(self):
if dbg:
print >> dbgout, "PyMailIMAPService isConnected"
return self.server != None
def getCurrentConnectionContext(self):
if dbg:
print >> dbgout, "PyMailIMAPService getCurrentConnectionContext"
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
self.notify = EventObject()
if dbg:
print >> dbgout, "PyMailPOP3Service init"
def addConnectionListener(self, xListener):
if dbg:
print >> dbgout, "PyMailPOP3Service addConnectionListener"
self.listeners.append(xListener)
def removeConnectionListener(self, xListener):
if dbg:
print >> dbgout, "PyMailPOP3Service removeConnectionListener"
self.listeners.remove(xListener)
def getSupportedConnectionTypes(self):
if dbg:
print >> dbgout, "PyMailPOP3Service getSupportedConnectionTypes"
return self.supportedtypes
def connect(self, xConnectionContext, xAuthenticator):
if dbg:
print >> dbgout, "PyMailPOP3Service connect"
self.connectioncontext = xConnectionContext
server = xConnectionContext.getValueByName("ServerName")
if dbg:
print >> dbgout, server
port = int(xConnectionContext.getValueByName("Port"))
if dbg:
print >> dbgout, port
connectiontype = xConnectionContext.getValueByName("ConnectionType")
if dbg:
print >> dbgout, connectiontype
print >> dbgout, "BEFORE"
if connectiontype == 'Ssl':
self.server = poplib.POP3_SSL(server, port)
else:
self.server = poplib.POP3(server, port)
print >> dbgout, "AFTER"
user = xAuthenticator.getUserName().encode('ascii')
password = xAuthenticator.getPassword().encode('ascii')
if dbg:
print >> dbgout, 'Logging in, username of', user
self.server.user(user)
self.server.pass_(user, password)
for listener in self.listeners:
listener.connected(self.notify)
def disconnect(self):
if dbg:
print >> dbgout, "PyMailPOP3Service disconnect"
if self.server:
self.server.quit()
self.server = None
for listener in self.listeners:
listener.disconnected(self.notify)
def isConnected(self):
if dbg:
print >> dbgout, "PyMailPOP3Service isConnected"
return self.server != None
def getCurrentConnectionContext(self):
if dbg:
print >> dbgout, "PyMailPOP3Service getCurrentConnectionContext"
return self.connectioncontext
class PyMailServiceProvider(unohelper.Base, XMailServiceProvider):
def __init__( self, ctx ):
if dbg:
print >> dbgout, "PyMailServiceProvider init"
self.ctx = ctx
def create(self, aType):
if dbg:
print >> dbgout, "PyMailServiceProvider create with", aType
if aType == SMTP:
return PyMailSMTPService(self.ctx);
elif aType == POP3:
return PyMailPOP3Service(self.ctx);
elif aType == IMAP:
return PyMailIMAPService(self.ctx);
else:
print >> dbgout, "PyMailServiceProvider, unknown TYPE", aType
class PyMailMessage(unohelper.Base, XMailMessage):
def __init__( self, ctx, sTo='', sFrom='', Subject='', Body=None, aMailAttachment=None ):
if dbg:
print >> dbgout, "PyMailMessage init"
self.ctx = ctx
self.recipients = sTo,
self.ccrecipients = ()
self.bccrecipients = ()
self.aMailAttachments = ()
if aMailAttachment != None:
self.aMailAttachments = aMailAttachment,
self.SenderName, self.SenderAddress = parseaddr(sFrom)
self.ReplyToAddress = sFrom
self.Subject = Subject
self.Body = Body
if dbg:
print >> dbgout, "post PyMailMessage init"
def addRecipient( self, recipient ):
if dbg:
print >> dbgout, "PyMailMessage.addRecipient", recipient
self.recipients = self.recipients, recipient
def addCcRecipient( self, ccrecipient ):
if dbg:
print >> dbgout, "PyMailMessage.addCcRecipient", ccrecipient
self.ccrecipients = self.ccrecipients, ccrecipient
def addBccRecipient( self, bccrecipient ):
if dbg:
print >> dbgout, "PyMailMessage.addBccRecipient", bccrecipient
self.bccrecipients = self.bccrecipients, bccrecipient
def getRecipients( self ):
if dbg:
print >> dbgout, "PyMailMessage.getRecipients", self.recipients
return self.recipients
def getCcRecipients( self ):
if dbg:
print >> dbgout, "PyMailMessage.getCcRecipients", self.ccrecipients
return self.ccrecipients
def getBccRecipients( self ):
if dbg:
print >> dbgout, "PyMailMessage.getBccRecipients", self.bccrecipients
return self.bccrecipients
def addAttachment( self, aMailAttachment ):
if dbg:
print >> dbgout, "PyMailMessage.addAttachment"
self.aMailAttachments = self.aMailAttachments, aMailAttachment
def getAttachments( self ):
if dbg:
print >> dbgout, "PyMailMessage.getAttachments"
return self.aMailAttachments
# 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",),)
g_ImplementationHelper.addImplementation( \
PyMailMessage, "org.openoffice.pyuno.MailMessage",
("com.sun.star.mail.MailMessage",),)
|