Package src :: Package gui :: Module email_sender
[hide private]
[frames] | no frames]

Source Code for Module src.gui.email_sender

 1  #!/usr/bin/python 
 2  # -*- coding: UTF-8 -*- 
 3  # 
 4  #The MIT License 
 5  # 
 6  #Copyright (c) 2011 
 7  # 
 8  #Permission is hereby granted, free of charge, to any person obtaining a copy 
 9  #of this software and associated documentation files (the "Software"), to deal 
10  #in the Software without restriction, including without limitation the rights 
11  #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
12  #copies of the Software, and to permit persons to whom the Software is 
13  #furnished to do so, subject to the following conditions: 
14  # 
15  #The above copyright notice and this permission notice shall be included in 
16  #all copies or substantial portions of the Software. 
17  # 
18  #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
19  #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
20  #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
21  #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
22  #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
23  #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
24  #THE SOFTWARE. 
25  # 
26  #Authors: 
27  #   Vili Auvinen (vili.k.auvinen@jyu.fi) 
28  #   Olli Kauppinen (olli.kauppinen@jyu.fi) 
29  #   Juho Tammela (juho.i.tammela@jyu.fi) 
30   
31  '''The module contains the function for sending email. 
32   
33  @author: Juho Tammela 
34  ''' 
35  from smtplib import SMTP 
36  from email.MIMEText import MIMEText 
37  #from email.MIMEMessage import MIMEMessage 
38  from urllib import unquote 
39   
40 -def index(req):
41 '''The main function is called by a JavaScript file asynchronously. It sends email to the user. 42 43 Email address and the message body is stored in Request object. 44 45 @param req: Mod_Python request object holding the information given in the Ajax call. 46 47 @return: The string to indicate success or failure in sending the email. The string can be parsed as XML because of Internet Explorer. 48 49 Internet Explorer wants XML as response, else it thinks something went wrong when it tries to parse xml out of the response and doesn't succeed! 50 ''' 51 #Couldn't get this to show scandinavian characters properly in the email-message with utf-8 although the html-page is in utf-8. 52 #iso-8859-1 seems to work though. 53 54 req.content_type = "text/xml; charset=ISO-8859-1" 55 #req.content_type = "text/xml; charset=utf-8" 56 57 emailAddress = req.form.getfirst("email") 58 body = unquote(req.form.getfirst("data")).decode('iso-8859-1').encode('iso-8859-1') 59 60 # for body_charset in 'ASCII', 'ISO-8859-1', 'UTF-8', 'UTF-16': 61 # try: 62 # body.encode(body_charset) 63 # except UnicodeError: 64 # pass 65 # else: 66 # break 67 68 # msg = MIMEText(body.encode(body_charset), 'plain', body_charset) 69 70 msg = MIMEText(body, 'plain', 'iso-8859-1') 71 72 you = str(emailAddress) 73 me = str(emailAddress) 74 msg['From'] = me 75 msg['To'] = you 76 77 #Add more receivers here: 78 #msg['Cc'] = third_party 79 80 #The source text is utf-8. 81 msg['Subject'] = 'Tietokone ja tietoverkot työvälineenä - palaute harjoitustyöstäsi!'.decode('utf-8') 82 83 84 try: 85 s = SMTP('smtp.jyu.fi') 86 s.sendmail(me, you, msg.as_string()) 87 s.quit() 88 #Internet Explorer wants xml as response, else it thinks something went wrong when it tries to parse xml out of the response and doesn't succeed! 89 req.write("<status>200 OK</status>") 90 except: 91 #req.write(str(sys.exc_info()[0])) 92 req.write("<status>ERROR</status>")
93