source: MILK/trunk/milk_server/milk_server/lib/mailer.py @ 4469

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/MILK/trunk/milk_server/milk_server/lib/mailer.py@4469
Revision 4469, 1.6 KB checked in by cbyrom, 12 years ago (diff)

Strip out code not relevant to MILK - mainly WCS and WMS stuff - also including the CSML server code + trackback code
Also tidy up structure of 'public' dir - setting up new 'style' dir and
centralising icons in icons dir + remove all unused icons, javascript and stylesheets.
Also strip out testcase code and populate new test directory structure.

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
Note: See TracBrowser for help on using the repository browser.