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