xref: /AOO41X/main/scripting/source/pyprov/mailmerge.py (revision a02925634c972d18e814c930d28181453b4f7224)
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
55bb7facceSAriel 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
86bb7facceSAriel 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:
183cdf0e10cSrcweir			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
194cdf0e10cSrcweir
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)
208*a0292563SAriel Constenla-Haile			fname = attachment.ReadableName
209*a0292563SAriel Constenla-Haile			try:
210*a0292563SAriel Constenla-Haile				fname.encode('ascii')
211*a0292563SAriel Constenla-Haile			except:
212*a0292563SAriel Constenla-Haile				fname = ('utf-8','',fname.encode('utf-8'))
213cdf0e10cSrcweir			msgattachment.add_header('Content-Disposition', 'attachment', \
214*a0292563SAriel Constenla-Haile				filename=fname)
215cdf0e10cSrcweir			msg.attach(msgattachment)
216cdf0e10cSrcweir
217bb7facceSAriel Constenla-Haile
218cdf0e10cSrcweir		uniquer = {}
219cdf0e10cSrcweir		for key in recipients:
220cdf0e10cSrcweir			uniquer[key] = True
221cdf0e10cSrcweir		if len(ccrecipients):
222cdf0e10cSrcweir			for key in ccrecipients:
223cdf0e10cSrcweir				uniquer[key] = True
224cdf0e10cSrcweir		if len(bccrecipients):
225cdf0e10cSrcweir			for key in bccrecipients:
226cdf0e10cSrcweir				uniquer[key] = True
227cdf0e10cSrcweir		truerecipients = uniquer.keys()
228cdf0e10cSrcweir
229cdf0e10cSrcweir		if dbg:
230cdf0e10cSrcweir			print >> sys.stderr, "PyMailSMPTService recipients are", truerecipients
231cdf0e10cSrcweir
232cdf0e10cSrcweir		self.server.sendmail(sendermail, truerecipients, msg.as_string())
233cdf0e10cSrcweir
234cdf0e10cSrcweirclass PyMailIMAPService(unohelper.Base, XMailService):
235cdf0e10cSrcweir	def __init__( self, ctx ):
236cdf0e10cSrcweir		self.ctx = ctx
237cdf0e10cSrcweir		self.listeners = []
238cdf0e10cSrcweir		self.supportedtypes = ('Insecure', 'Ssl')
239cdf0e10cSrcweir		self.server = None
240cdf0e10cSrcweir		self.connectioncontext = None
241bb7facceSAriel Constenla-Haile		self.notify = EventObject(self)
242cdf0e10cSrcweir		if dbg:
243cdf0e10cSrcweir			print >> sys.stderr, "PyMailIMAPService init"
244cdf0e10cSrcweir	def addConnectionListener(self, xListener):
245cdf0e10cSrcweir		if dbg:
246cdf0e10cSrcweir			print >> sys.stderr, "PyMailIMAPService addConnectionListener"
247cdf0e10cSrcweir		self.listeners.append(xListener)
248cdf0e10cSrcweir	def removeConnectionListener(self, xListener):
249cdf0e10cSrcweir		if dbg:
250cdf0e10cSrcweir			print >> sys.stderr, "PyMailIMAPService removeConnectionListener"
251cdf0e10cSrcweir		self.listeners.remove(xListener)
252cdf0e10cSrcweir	def getSupportedConnectionTypes(self):
253cdf0e10cSrcweir		if dbg:
254cdf0e10cSrcweir			print >> sys.stderr, "PyMailIMAPService getSupportedConnectionTypes"
255cdf0e10cSrcweir		return self.supportedtypes
256cdf0e10cSrcweir	def connect(self, xConnectionContext, xAuthenticator):
257cdf0e10cSrcweir		if dbg:
258cdf0e10cSrcweir			print >> sys.stderr, "PyMailIMAPService connect"
259cdf0e10cSrcweir
260cdf0e10cSrcweir		self.connectioncontext = xConnectionContext
261cdf0e10cSrcweir		server = xConnectionContext.getValueByName("ServerName")
262cdf0e10cSrcweir		if dbg:
263cdf0e10cSrcweir			print >> sys.stderr, server
264cdf0e10cSrcweir		port = xConnectionContext.getValueByName("Port")
265cdf0e10cSrcweir		if dbg:
266cdf0e10cSrcweir			print >> sys.stderr, port
267cdf0e10cSrcweir		connectiontype = xConnectionContext.getValueByName("ConnectionType")
268cdf0e10cSrcweir		if dbg:
269cdf0e10cSrcweir			print >> sys.stderr, connectiontype
270cdf0e10cSrcweir		print >> sys.stderr, "BEFORE"
271bb7facceSAriel Constenla-Haile		if connectiontype.upper() == 'SSL':
272cdf0e10cSrcweir			self.server = imaplib.IMAP4_SSL(server, port)
273cdf0e10cSrcweir		else:
274cdf0e10cSrcweir			self.server = imaplib.IMAP4(server, port)
275cdf0e10cSrcweir		print >> sys.stderr, "AFTER"
276cdf0e10cSrcweir
277cdf0e10cSrcweir		user = xAuthenticator.getUserName().encode('ascii')
278cdf0e10cSrcweir		password = xAuthenticator.getPassword().encode('ascii')
279cdf0e10cSrcweir		if user != '':
280cdf0e10cSrcweir			if dbg:
281cdf0e10cSrcweir				print >> sys.stderr, 'Logging in, username of', user
282cdf0e10cSrcweir			self.server.login(user, password)
283cdf0e10cSrcweir
284cdf0e10cSrcweir		for listener in self.listeners:
285cdf0e10cSrcweir			listener.connected(self.notify)
286cdf0e10cSrcweir	def disconnect(self):
287cdf0e10cSrcweir		if dbg:
288cdf0e10cSrcweir			print >> sys.stderr, "PyMailIMAPService disconnect"
289cdf0e10cSrcweir		if self.server:
290cdf0e10cSrcweir			self.server.logout()
291cdf0e10cSrcweir			self.server = None
292cdf0e10cSrcweir		for listener in self.listeners:
293cdf0e10cSrcweir			listener.disconnected(self.notify)
294cdf0e10cSrcweir	def isConnected(self):
295cdf0e10cSrcweir		if dbg:
296cdf0e10cSrcweir			print >> sys.stderr, "PyMailIMAPService isConnected"
297cdf0e10cSrcweir		return self.server != None
298cdf0e10cSrcweir	def getCurrentConnectionContext(self):
299cdf0e10cSrcweir		if dbg:
300cdf0e10cSrcweir			print >> sys.stderr, "PyMailIMAPService getCurrentConnectionContext"
301cdf0e10cSrcweir		return self.connectioncontext
302cdf0e10cSrcweir
303cdf0e10cSrcweirclass PyMailPOP3Service(unohelper.Base, XMailService):
304cdf0e10cSrcweir	def __init__( self, ctx ):
305cdf0e10cSrcweir		self.ctx = ctx
306cdf0e10cSrcweir		self.listeners = []
307cdf0e10cSrcweir		self.supportedtypes = ('Insecure', 'Ssl')
308cdf0e10cSrcweir		self.server = None
309cdf0e10cSrcweir		self.connectioncontext = None
310bb7facceSAriel Constenla-Haile		self.notify = EventObject(self)
311cdf0e10cSrcweir		if dbg:
312cdf0e10cSrcweir			print >> sys.stderr, "PyMailPOP3Service init"
313cdf0e10cSrcweir	def addConnectionListener(self, xListener):
314cdf0e10cSrcweir		if dbg:
315cdf0e10cSrcweir			print >> sys.stderr, "PyMailPOP3Service addConnectionListener"
316cdf0e10cSrcweir		self.listeners.append(xListener)
317cdf0e10cSrcweir	def removeConnectionListener(self, xListener):
318cdf0e10cSrcweir		if dbg:
319cdf0e10cSrcweir			print >> sys.stderr, "PyMailPOP3Service removeConnectionListener"
320cdf0e10cSrcweir		self.listeners.remove(xListener)
321cdf0e10cSrcweir	def getSupportedConnectionTypes(self):
322cdf0e10cSrcweir		if dbg:
323cdf0e10cSrcweir			print >> sys.stderr, "PyMailPOP3Service getSupportedConnectionTypes"
324cdf0e10cSrcweir		return self.supportedtypes
325cdf0e10cSrcweir	def connect(self, xConnectionContext, xAuthenticator):
326cdf0e10cSrcweir		if dbg:
327cdf0e10cSrcweir			print >> sys.stderr, "PyMailPOP3Service connect"
328cdf0e10cSrcweir
329cdf0e10cSrcweir		self.connectioncontext = xConnectionContext
330cdf0e10cSrcweir		server = xConnectionContext.getValueByName("ServerName")
331cdf0e10cSrcweir		if dbg:
332cdf0e10cSrcweir			print >> sys.stderr, server
333cdf0e10cSrcweir		port = xConnectionContext.getValueByName("Port")
334cdf0e10cSrcweir		if dbg:
335cdf0e10cSrcweir			print >> sys.stderr, port
336cdf0e10cSrcweir		connectiontype = xConnectionContext.getValueByName("ConnectionType")
337cdf0e10cSrcweir		if dbg:
338cdf0e10cSrcweir			print >> sys.stderr, connectiontype
339cdf0e10cSrcweir		print >> sys.stderr, "BEFORE"
340bb7facceSAriel Constenla-Haile		if connectiontype.upper() == 'SSL':
341cdf0e10cSrcweir			self.server = poplib.POP3_SSL(server, port)
342cdf0e10cSrcweir		else:
343cdf0e10cSrcweir			self.server = poplib.POP3(server, port)
344cdf0e10cSrcweir		print >> sys.stderr, "AFTER"
345cdf0e10cSrcweir
346cdf0e10cSrcweir		user = xAuthenticator.getUserName().encode('ascii')
347cdf0e10cSrcweir		password = xAuthenticator.getPassword().encode('ascii')
348cdf0e10cSrcweir		if dbg:
349cdf0e10cSrcweir			print >> sys.stderr, 'Logging in, username of', user
350cdf0e10cSrcweir		self.server.user(user)
351bb7facceSAriel Constenla-Haile		self.server.pass_(password)
352cdf0e10cSrcweir
353cdf0e10cSrcweir		for listener in self.listeners:
354cdf0e10cSrcweir			listener.connected(self.notify)
355cdf0e10cSrcweir	def disconnect(self):
356cdf0e10cSrcweir		if dbg:
357cdf0e10cSrcweir			print >> sys.stderr, "PyMailPOP3Service disconnect"
358cdf0e10cSrcweir		if self.server:
359cdf0e10cSrcweir			self.server.quit()
360cdf0e10cSrcweir			self.server = None
361cdf0e10cSrcweir		for listener in self.listeners:
362cdf0e10cSrcweir			listener.disconnected(self.notify)
363cdf0e10cSrcweir	def isConnected(self):
364cdf0e10cSrcweir		if dbg:
365cdf0e10cSrcweir			print >> sys.stderr, "PyMailPOP3Service isConnected"
366cdf0e10cSrcweir		return self.server != None
367cdf0e10cSrcweir	def getCurrentConnectionContext(self):
368cdf0e10cSrcweir		if dbg:
369cdf0e10cSrcweir			print >> sys.stderr, "PyMailPOP3Service getCurrentConnectionContext"
370cdf0e10cSrcweir		return self.connectioncontext
371cdf0e10cSrcweir
372cdf0e10cSrcweirclass PyMailServiceProvider(unohelper.Base, XMailServiceProvider):
373cdf0e10cSrcweir	def __init__( self, ctx ):
374cdf0e10cSrcweir		if dbg:
375cdf0e10cSrcweir			print >> sys.stderr, "PyMailServiceProvider init"
376cdf0e10cSrcweir		self.ctx = ctx
377cdf0e10cSrcweir	def create(self, aType):
378cdf0e10cSrcweir		if dbg:
379cdf0e10cSrcweir			print >> sys.stderr, "PyMailServiceProvider create with", aType
380cdf0e10cSrcweir		if aType == SMTP:
381cdf0e10cSrcweir			return PyMailSMTPService(self.ctx);
382cdf0e10cSrcweir		elif aType == POP3:
383cdf0e10cSrcweir			return PyMailPOP3Service(self.ctx);
384cdf0e10cSrcweir		elif aType == IMAP:
385cdf0e10cSrcweir			return PyMailIMAPService(self.ctx);
386cdf0e10cSrcweir		else:
387cdf0e10cSrcweir			print >> sys.stderr, "PyMailServiceProvider, unknown TYPE", aType
388cdf0e10cSrcweir
389cdf0e10cSrcweirclass PyMailMessage(unohelper.Base, XMailMessage):
390cdf0e10cSrcweir	def __init__( self, ctx, sTo='', sFrom='', Subject='', Body=None, aMailAttachment=None ):
391cdf0e10cSrcweir		if dbg:
392cdf0e10cSrcweir			print >> sys.stderr, "PyMailMessage init"
393cdf0e10cSrcweir		self.ctx = ctx
394cdf0e10cSrcweir
395bb7facceSAriel Constenla-Haile		self.recipients = [sTo]
396bb7facceSAriel Constenla-Haile		self.ccrecipients = []
397bb7facceSAriel Constenla-Haile		self.bccrecipients = []
398bb7facceSAriel Constenla-Haile		self.aMailAttachments = []
399cdf0e10cSrcweir		if aMailAttachment != None:
400bb7facceSAriel Constenla-Haile			self.aMailAttachments.append(aMailAttachment)
401cdf0e10cSrcweir
402cdf0e10cSrcweir		self.SenderName, self.SenderAddress = parseaddr(sFrom)
403cdf0e10cSrcweir		self.ReplyToAddress = sFrom
404cdf0e10cSrcweir		self.Subject = Subject
405cdf0e10cSrcweir		self.Body = Body
406cdf0e10cSrcweir		if dbg:
407cdf0e10cSrcweir			print >> sys.stderr, "post PyMailMessage init"
408cdf0e10cSrcweir	def addRecipient( self, recipient ):
409cdf0e10cSrcweir		if dbg:
410cdf0e10cSrcweir			print >> sys.stderr, "PyMailMessage.addRecipient", recipient
411bb7facceSAriel Constenla-Haile		self.recipients.append(recipient)
412cdf0e10cSrcweir	def addCcRecipient( self, ccrecipient ):
413cdf0e10cSrcweir		if dbg:
414cdf0e10cSrcweir			print >> sys.stderr, "PyMailMessage.addCcRecipient", ccrecipient
415bb7facceSAriel Constenla-Haile		self.ccrecipients.append(ccrecipient)
416cdf0e10cSrcweir	def addBccRecipient( self, bccrecipient ):
417cdf0e10cSrcweir		if dbg:
418cdf0e10cSrcweir			print >> sys.stderr, "PyMailMessage.addBccRecipient", bccrecipient
419bb7facceSAriel Constenla-Haile		self.bccrecipients.append(bccrecipient)
420cdf0e10cSrcweir	def getRecipients( self ):
421cdf0e10cSrcweir		if dbg:
422cdf0e10cSrcweir			print >> sys.stderr, "PyMailMessage.getRecipients", self.recipients
423bb7facceSAriel Constenla-Haile		return tuple(self.recipients)
424cdf0e10cSrcweir	def getCcRecipients( self ):
425cdf0e10cSrcweir		if dbg:
426cdf0e10cSrcweir			print >> sys.stderr, "PyMailMessage.getCcRecipients", self.ccrecipients
427bb7facceSAriel Constenla-Haile		return tuple(self.ccrecipients)
428cdf0e10cSrcweir	def getBccRecipients( self ):
429cdf0e10cSrcweir		if dbg:
430cdf0e10cSrcweir			print >> sys.stderr, "PyMailMessage.getBccRecipients", self.bccrecipients
431bb7facceSAriel Constenla-Haile		return tuple(self.bccrecipients)
432cdf0e10cSrcweir	def addAttachment( self, aMailAttachment ):
433cdf0e10cSrcweir		if dbg:
434cdf0e10cSrcweir			print >> sys.stderr, "PyMailMessage.addAttachment"
435bb7facceSAriel Constenla-Haile		self.aMailAttachments.append(aMailAttachment)
436cdf0e10cSrcweir	def getAttachments( self ):
437cdf0e10cSrcweir		if dbg:
438cdf0e10cSrcweir			print >> sys.stderr, "PyMailMessage.getAttachments"
439bb7facceSAriel Constenla-Haile		return tuple(self.aMailAttachments)
440bb7facceSAriel Constenla-Haile
441cdf0e10cSrcweir
442cdf0e10cSrcweir# pythonloader looks for a static g_ImplementationHelper variable
443cdf0e10cSrcweirg_ImplementationHelper = unohelper.ImplementationHelper()
444cdf0e10cSrcweirg_ImplementationHelper.addImplementation( \
445cdf0e10cSrcweir	PyMailServiceProvider, "org.openoffice.pyuno.MailServiceProvider",
446cdf0e10cSrcweir		("com.sun.star.mail.MailServiceProvider",),)
447cdf0e10cSrcweirg_ImplementationHelper.addImplementation( \
448cdf0e10cSrcweir	PyMailMessage, "org.openoffice.pyuno.MailMessage",
449cdf0e10cSrcweir		("com.sun.star.mail.MailMessage",),)
450