Files
libreoffice/scripting/source/pyprov/mailmerge.py

493 lines
16 KiB
Python
Raw Normal View History

# 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>
from __future__ import print_function
import unohelper
import uno
import re
2011-08-02 09:52:37 +01:00
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.mime.base import MIMEBase
from email.message import Message
from email.charset import Charset
from email.charset import QP
from email.encoders import encode_base64
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.utils import formatdate
from email.utils import parseaddr
from socket import _GLOBAL_DEFAULT_TIMEOUT
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):
2011-03-12 23:19:46 +01:00
def __init__( self, ctx ):
self.ctx = ctx
self.listeners = []
self.supportedtypes = ('Insecure', 'Ssl')
self.server = None
self.connectioncontext = None
self.notify = EventObject(self)
if dbg:
print("PyMailSMTPService init", file=dbgout)
def addConnectionListener(self, xListener):
if dbg:
print("PyMailSMTPService addConnectionListener", file=dbgout)
self.listeners.append(xListener)
def removeConnectionListener(self, xListener):
if dbg:
print("PyMailSMTPService removeConnectionListener", file=dbgout)
self.listeners.remove(xListener)
def getSupportedConnectionTypes(self):
if dbg:
print("PyMailSMTPService getSupportedConnectionTypes", file=dbgout)
return self.supportedtypes
def connect(self, xConnectionContext, xAuthenticator):
self.connectioncontext = xConnectionContext
if dbg:
print("PyMailSMTPService connect", file=dbgout)
server = xConnectionContext.getValueByName("ServerName")
if dbg:
print("ServerName: " + server, file=dbgout)
2011-07-26 12:24:49 +01:00
port = int(xConnectionContext.getValueByName("Port"))
if dbg:
print("Port: " + str(port), file=dbgout)
tout = xConnectionContext.getValueByName("Timeout")
if dbg:
print(isinstance(tout,int), file=dbgout)
if not isinstance(tout,int):
tout = _GLOBAL_DEFAULT_TIMEOUT
if dbg:
print("Timeout: " + str(tout), file=dbgout)
self.server = smtplib.SMTP(server, port,timeout=tout)
2011-08-02 09:52:37 +01:00
#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("ConnectionType: " + connectiontype, file=dbgout)
if connectiontype.upper() == 'SSL':
CWS-TOOLING: integrate CWS cmcfixes51 2008-12-08 10:12:55 +0100 cmc r264975 : #i96203# protect with ifdefs to avoid unused symbol on mac 2008-12-05 12:23:47 +0100 cmc r264898 : CWS-TOOLING: rebase CWS cmcfixes51 to trunk@264807 (milestone: DEV300:m37) 2008-12-01 14:45:17 +0100 cmc r264606 : #i76655# ehlos apparently required 2008-11-28 17:49:30 +0100 cmc r264567 : #i96655# remove newly unused method 2008-11-28 10:41:28 +0100 cmc r264531 : #i96647# better ppc-bridges flushCode impl 2008-11-27 12:58:40 +0100 cmc r264478 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 12:32:49 +0100 cmc r264476 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 12:26:02 +0100 cmc r264475 : #i96655# redundant old table export helpers 2008-11-27 11:49:06 +0100 cmc r264473 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 11:38:35 +0100 cmc r264471 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 11:14:21 +0100 cmc r264467 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 11:06:22 +0100 cmc r264464 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 10:58:18 +0100 cmc r264462 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 10:41:44 +0100 cmc r264461 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 10:19:24 +0100 cmc r264460 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 10:13:39 +0100 cmc r264459 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 10:06:14 +0100 cmc r264458 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:59:54 +0100 cmc r264457 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:52:51 +0100 cmc r264456 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:48:26 +0100 cmc r264454 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:40:20 +0100 cmc r264452 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:35:26 +0100 cmc r264451 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:31:00 +0100 cmc r264450 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:24:08 +0100 cmc r264449 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 00:26:15 +0100 cmc r264443 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 00:21:01 +0100 cmc r264442 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 00:09:40 +0100 cmc r264441 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 23:51:56 +0100 cmc r264440 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 23:49:09 +0100 cmc r264439 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 18:09:54 +0100 cmc r264432 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 18:07:40 +0100 cmc r264431 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 17:28:02 +0100 cmc r264429 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 17:27:39 +0100 cmc r264428 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 17:18:36 +0100 cmc r264426 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 16:22:16 +0100 cmc r264415 : #i96624# make implicit braces and brackets explicit to avoid warnings 2008-11-26 16:00:23 +0100 cmc r264409 : #i90426# remove warnings from svtools 2008-11-26 15:59:17 +0100 cmc r264408 : #i90426# remove warnings 2008-11-26 15:47:32 +0100 cmc r264404 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 14:46:57 +0100 cmc r264394 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 14:19:50 +0100 cmc r264387 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 14:15:26 +0100 cmc r264386 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 14:11:26 +0100 cmc r264384 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 13:44:23 +0100 cmc r264380 : #i96084# comfirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 13:12:24 +0100 cmc r264372 : #i96604# silence new warnings 2008-11-26 12:35:02 +0100 cmc r264369 : #i96203# make qstarter work in 3-layer land 2008-11-26 12:33:04 +0100 cmc r264368 : #i96170# ensure gtypes are up and running
2008-12-11 07:05:03 +00:00
self.server.ehlo()
self.server.starttls()
CWS-TOOLING: integrate CWS cmcfixes51 2008-12-08 10:12:55 +0100 cmc r264975 : #i96203# protect with ifdefs to avoid unused symbol on mac 2008-12-05 12:23:47 +0100 cmc r264898 : CWS-TOOLING: rebase CWS cmcfixes51 to trunk@264807 (milestone: DEV300:m37) 2008-12-01 14:45:17 +0100 cmc r264606 : #i76655# ehlos apparently required 2008-11-28 17:49:30 +0100 cmc r264567 : #i96655# remove newly unused method 2008-11-28 10:41:28 +0100 cmc r264531 : #i96647# better ppc-bridges flushCode impl 2008-11-27 12:58:40 +0100 cmc r264478 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 12:32:49 +0100 cmc r264476 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 12:26:02 +0100 cmc r264475 : #i96655# redundant old table export helpers 2008-11-27 11:49:06 +0100 cmc r264473 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 11:38:35 +0100 cmc r264471 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 11:14:21 +0100 cmc r264467 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 11:06:22 +0100 cmc r264464 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 10:58:18 +0100 cmc r264462 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 10:41:44 +0100 cmc r264461 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 10:19:24 +0100 cmc r264460 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 10:13:39 +0100 cmc r264459 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 10:06:14 +0100 cmc r264458 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:59:54 +0100 cmc r264457 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:52:51 +0100 cmc r264456 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:48:26 +0100 cmc r264454 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:40:20 +0100 cmc r264452 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:35:26 +0100 cmc r264451 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:31:00 +0100 cmc r264450 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 09:24:08 +0100 cmc r264449 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 00:26:15 +0100 cmc r264443 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 00:21:01 +0100 cmc r264442 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-27 00:09:40 +0100 cmc r264441 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 23:51:56 +0100 cmc r264440 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 23:49:09 +0100 cmc r264439 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 18:09:54 +0100 cmc r264432 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 18:07:40 +0100 cmc r264431 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 17:28:02 +0100 cmc r264429 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 17:27:39 +0100 cmc r264428 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 17:18:36 +0100 cmc r264426 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 16:22:16 +0100 cmc r264415 : #i96624# make implicit braces and brackets explicit to avoid warnings 2008-11-26 16:00:23 +0100 cmc r264409 : #i90426# remove warnings from svtools 2008-11-26 15:59:17 +0100 cmc r264408 : #i90426# remove warnings 2008-11-26 15:47:32 +0100 cmc r264404 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 14:46:57 +0100 cmc r264394 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 14:19:50 +0100 cmc r264387 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 14:15:26 +0100 cmc r264386 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 14:11:26 +0100 cmc r264384 : #i96084# confirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 13:44:23 +0100 cmc r264380 : #i96084# comfirm existing logic with explicit brackets to remove new gcc warnings 2008-11-26 13:12:24 +0100 cmc r264372 : #i96604# silence new warnings 2008-11-26 12:35:02 +0100 cmc r264369 : #i96203# make qstarter work in 3-layer land 2008-11-26 12:33:04 +0100 cmc r264368 : #i96170# ensure gtypes are up and running
2008-12-11 07:05:03 +00:00
self.server.ehlo()
user = xAuthenticator.getUserName()
password = xAuthenticator.getPassword()
if user != '':
if sys.version < '3': # fdo#59249 i#105669 Python 2 needs "ascii"
user = user.encode('ascii')
password = password.encode('ascii')
if dbg:
print("Logging in, username of: " + user, file=dbgout)
self.server.login(user, password)
for listener in self.listeners:
listener.connected(self.notify)
def disconnect(self):
if dbg:
print("PyMailSMTPService disconnect", file=dbgout)
if self.server:
self.server.quit()
self.server = None
for listener in self.listeners:
listener.disconnected(self.notify)
def isConnected(self):
if dbg:
print("PyMailSMTPService isConnected", file=dbgout)
return self.server != None
def getCurrentConnectionContext(self):
if dbg:
print("PyMailSMTPService getCurrentConnectionContext", file=dbgout)
return self.connectioncontext
def sendMailMessage(self, xMailMessage):
COMMASPACE = ', '
if dbg:
print("PyMailSMTPService sendMailMessage", file=dbgout)
recipients = xMailMessage.getRecipients()
sendermail = xMailMessage.SenderAddress
sendername = xMailMessage.SenderName
subject = xMailMessage.Subject
ccrecipients = xMailMessage.getCcRecipients()
bccrecipients = xMailMessage.getBccRecipients()
if dbg:
print("PyMailSMTPService subject: " + subject, file=dbgout)
print("PyMailSMTPService from: " + sendername, file=dbgout)
print("PyMailSMTPService from: " + sendermail, file=dbgout)
print("PyMailSMTPService send to: %s" % (recipients,), file=dbgout)
attachments = xMailMessage.getAttachments()
textmsg = Message()
content = xMailMessage.Body
flavors = content.getTransferDataFlavors()
if dbg:
print("PyMailSMTPService flavors len: %d" % (len(flavors),), file=dbgout)
#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("PyMailSMTPService mimetype is: " + flavor.MimeType, file=dbgout)
textbody = content.getTransferData(flavor)
if len(textbody):
mimeEncoding = re.sub("charset=.*", "charset=UTF-8", flavor.MimeType)
2010-07-14 14:17:11 +01:00
if mimeEncoding.find('charset=UTF-8') == -1:
mimeEncoding = mimeEncoding + "; charset=UTF-8"
textmsg['Content-Type'] = mimeEncoding
textmsg['MIME-Version'] = '1.0'
textbody = textbody.encode('utf-8')
if sys.version >= '3':
#http://stackoverflow.com/questions/9403265/how-do-i-use-python-3-2-email-module-to-send-unicode-messages-encoded-in-utf-8-w
textbody = textbody.decode('iso8859-1')
c = Charset('utf-8')
c.body_encoding = QP
textmsg.set_payload(textbody, c)
else:
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.value)
encode_base64(msgattachment)
fname = attachment.ReadableName
try:
fname.encode('ascii')
except:
fname = ('utf-8','',fname.encode('utf-8'))
msgattachment.add_header('Content-Disposition', 'attachment', \
filename=fname)
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(("PyMailSMTPService recipients are: ", truerecipients), file=dbgout)
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(self)
if dbg:
print("PyMailIMAPService init", file=dbgout)
def addConnectionListener(self, xListener):
if dbg:
print("PyMailIMAPService addConnectionListener", file=dbgout)
self.listeners.append(xListener)
def removeConnectionListener(self, xListener):
if dbg:
print("PyMailIMAPService removeConnectionListener", file=dbgout)
self.listeners.remove(xListener)
def getSupportedConnectionTypes(self):
if dbg:
print("PyMailIMAPService getSupportedConnectionTypes", file=dbgout)
return self.supportedtypes
def connect(self, xConnectionContext, xAuthenticator):
if dbg:
print("PyMailIMAPService connect", file=dbgout)
self.connectioncontext = xConnectionContext
server = xConnectionContext.getValueByName("ServerName")
if dbg:
print(server, file=dbgout)
2011-07-26 12:24:49 +01:00
port = int(xConnectionContext.getValueByName("Port"))
if dbg:
print(port, file=dbgout)
connectiontype = xConnectionContext.getValueByName("ConnectionType")
if dbg:
print(connectiontype, file=dbgout)
print("BEFORE", file=dbgout)
if connectiontype.upper() == 'SSL':
self.server = imaplib.IMAP4_SSL(server, port)
else:
self.server = imaplib.IMAP4(server, port)
print("AFTER", file=dbgout)
user = xAuthenticator.getUserName()
password = xAuthenticator.getPassword()
if user != '':
if sys.version < '3': # fdo#59249 i#105669 Python 2 needs "ascii"
user = user.encode('ascii')
password = password.encode('ascii')
if dbg:
print("Logging in, username of: " + user, file=dbgout)
self.server.login(user, password)
for listener in self.listeners:
listener.connected(self.notify)
def disconnect(self):
if dbg:
print("PyMailIMAPService disconnect", file=dbgout)
if self.server:
self.server.logout()
self.server = None
for listener in self.listeners:
listener.disconnected(self.notify)
def isConnected(self):
if dbg:
print("PyMailIMAPService isConnected", file=dbgout)
return self.server != None
def getCurrentConnectionContext(self):
if dbg:
print("PyMailIMAPService getCurrentConnectionContext", file=dbgout)
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(self)
if dbg:
print("PyMailPOP3Service init", file=dbgout)
def addConnectionListener(self, xListener):
if dbg:
print("PyMailPOP3Service addConnectionListener", file=dbgout)
self.listeners.append(xListener)
def removeConnectionListener(self, xListener):
if dbg:
print("PyMailPOP3Service removeConnectionListener", file=dbgout)
self.listeners.remove(xListener)
def getSupportedConnectionTypes(self):
if dbg:
print("PyMailPOP3Service getSupportedConnectionTypes", file=dbgout)
return self.supportedtypes
def connect(self, xConnectionContext, xAuthenticator):
if dbg:
print("PyMailPOP3Service connect", file=dbgout)
self.connectioncontext = xConnectionContext
server = xConnectionContext.getValueByName("ServerName")
if dbg:
print(server, file=dbgout)
2011-07-26 12:24:49 +01:00
port = int(xConnectionContext.getValueByName("Port"))
if dbg:
print(port, file=dbgout)
connectiontype = xConnectionContext.getValueByName("ConnectionType")
if dbg:
print(connectiontype, file=dbgout)
print("BEFORE", file=dbgout)
if connectiontype.upper() == 'SSL':
self.server = poplib.POP3_SSL(server, port)
else:
tout = xConnectionContext.getValueByName("Timeout")
if dbg:
print(isinstance(tout,int), file=dbgout)
if not isinstance(tout,int):
tout = _GLOBAL_DEFAULT_TIMEOUT
if dbg:
print("Timeout: " + str(tout), file=dbgout)
self.server = poplib.POP3(server, port, timeout=tout)
print("AFTER", file=dbgout)
user = xAuthenticator.getUserName()
password = xAuthenticator.getPassword()
if sys.version < '3': # fdo#59249 i#105669 Python 2 needs "ascii"
user = user.encode('ascii')
password = password.encode('ascii')
if dbg:
print("Logging in, username of: " + user, file=dbgout)
self.server.user(user)
self.server.pass_(password)
for listener in self.listeners:
listener.connected(self.notify)
def disconnect(self):
if dbg:
print("PyMailPOP3Service disconnect", file=dbgout)
if self.server:
self.server.quit()
self.server = None
for listener in self.listeners:
listener.disconnected(self.notify)
def isConnected(self):
if dbg:
print("PyMailPOP3Service isConnected", file=dbgout)
return self.server != None
def getCurrentConnectionContext(self):
if dbg:
print("PyMailPOP3Service getCurrentConnectionContext", file=dbgout)
return self.connectioncontext
class PyMailServiceProvider(unohelper.Base, XMailServiceProvider):
def __init__( self, ctx ):
if dbg:
print("PyMailServiceProvider init", file=dbgout)
self.ctx = ctx
def create(self, aType):
if dbg:
print("PyMailServiceProvider create with", aType, file=dbgout)
if aType == SMTP:
return PyMailSMTPService(self.ctx);
elif aType == POP3:
return PyMailPOP3Service(self.ctx);
elif aType == IMAP:
return PyMailIMAPService(self.ctx);
else:
print("PyMailServiceProvider, unknown TYPE " + aType, file=dbgout)
class PyMailMessage(unohelper.Base, XMailMessage):
def __init__( self, ctx, sTo='', sFrom='', Subject='', Body=None, aMailAttachment=None ):
if dbg:
print("PyMailMessage init", file=dbgout)
self.ctx = ctx
self.recipients = [sTo]
self.ccrecipients = []
self.bccrecipients = []
self.aMailAttachments = []
if aMailAttachment != None:
self.aMailAttachments.append(aMailAttachment)
self.SenderName, self.SenderAddress = parseaddr(sFrom)
self.ReplyToAddress = sFrom
self.Subject = Subject
self.Body = Body
if dbg:
print("post PyMailMessage init", file=dbgout)
def addRecipient( self, recipient ):
if dbg:
print("PyMailMessage.addRecipient: " + recipient, file=dbgout)
self.recipients.append(recipient)
def addCcRecipient( self, ccrecipient ):
if dbg:
print("PyMailMessage.addCcRecipient: " + ccrecipient, file=dbgout)
self.ccrecipients.append(ccrecipient)
def addBccRecipient( self, bccrecipient ):
if dbg:
print("PyMailMessage.addBccRecipient: " + bccrecipient, file=dbgout)
self.bccrecipients.append(bccrecipient)
def getRecipients( self ):
if dbg:
print("PyMailMessage.getRecipients: " + self.recipients, file=dbgout)
return tuple(self.recipients)
def getCcRecipients( self ):
if dbg:
print("PyMailMessage.getCcRecipients: " + self.ccrecipients, file=dbgout)
return tuple(self.ccrecipients)
def getBccRecipients( self ):
if dbg:
print("PyMailMessage.getBccRecipients: " + self.bccrecipients, file=dbgout)
return tuple(self.bccrecipients)
def addAttachment( self, aMailAttachment ):
if dbg:
print("PyMailMessage.addAttachment", file=dbgout)
self.aMailAttachments.append(aMailAttachment)
def getAttachments( self ):
if dbg:
print("PyMailMessage.getAttachments", file=dbgout)
return tuple(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",),)