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