source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/lib/mailer.py @ 2868

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/lib/mailer.py@2868
Revision 2868, 2.5 KB checked in by spepler, 13 years ago (diff)

Fixed bug so it works with python 2.4

Line 
1import smtplib
2try:
3    #python 2.5
4    from email.mime.text import MIMEText
5    from email.mime.multipart import MIMEMultipart
6    from email.MIMEBase import MIMEBase
7except:
8    #python 2.4
9    from email.MIMEText import MIMEText
10    from email.MIMEMultipart import MIMEMultipart
11    from email.MIMEBase import MIMEBase
12
13debug=0
14
15def mailHandler(recipients,subject,body,
16        xmlAttachments=[],sender='ndg@badc.nerc.ac.uk',server='localhost'):
17    ''' Provides a mailing tool for messages from within the stack.
18        If present attachments should be a list of strings with mimetypes,
19            e.g [('<blah>xml</blah>',application,xml),(...),...] '''
20   
21    assert type(recipients)==list
22    assert type(xmlAttachments)==list
23     
24    try:
25   
26        if xmlAttachments==[]:
27           
28            msg=MIMEText(body)
29           
30        else:
31           
32            msg=MIMEMultipart()
33            msg.attach(MIMEText(body))
34       
35            for a in xmlAttachments:
36               
37                part=MIMEBase('application','text/xml')
38                part.set_payload(a[0])
39                # see www.ietf.org/rfc/rfc2183.txt
40                part.add_header('Content-Disposition','attachment; filename="%s"'%a[1])
41                msg.attach(part)
42               
43        msg['From']=sender
44        msg['To']=', '.join(recipients)
45        msg['Subject']=subject
46     
47        smtp=smtplib.SMTP(server)
48        smtp.sendmail(sender,recipients,msg.as_string())
49        smtp.close()
50       
51        return 1,'ok'
52     
53    except Exception,e:
54        return 0, 'Error sending mail [%s]'%e
55       
56       
57if __name__=="__main__":
58   
59    server='outbox.rl.ac.uk'
60    recipients=['s.j.pepler@rl.ac.uk',]
61   
62    import unittest
63   
64    class TestCase(unittest.TestCase):
65       
66        def testNormal(self):
67            ''' test sending a normal email message '''
68            body='test body'
69            subject='mailer test'
70            status,message=mailHandler(recipients,subject,body,server=server)
71            self.assertEqual(message,'ok')
72           
73        def testMime(self):
74            '''test sending an xml attachment '''
75            body='test body'
76            subject='mailer mime test'
77            a=('<xml><something>blah</something></blah>','trackback.xml')
78            att=[a,]
79            status,message=mailHandler(recipients,subject,body,xmlAttachments=att,server=server)
80            self.assertEqual(message,'ok')
81           
82       
83    unittest.main()
84       
Note: See TracBrowser for help on using the repository browser.