2011-07-26 16 views
0

私はGAEを通じて電子メールを送信するために、オープンソースのWebサービスPythonアプリケーションを使用していますが、名前や電子メールの体は、アラビア語やヘブライ語の文字が含まれている場合、アプリケーションは、いくつかのエラーをスローします(例: "指定されたパラメータは有効ではありません")。したがって、私はこの問題を解決する方法を知る必要があります。私はPython初心者です(私がPythonで遊んでから1週間後)。のGoogleのApp Engine、パイソン、ユニコード、電子メール、メールAPI

# 
import cgi 
import os 
import logging 
import contextlib 
from xml.dom import minidom 
from xml.dom.minidom import Document 
import exceptions 
import warnings 
import imghdr 
from google.appengine.api import images 
from google.appengine.api import users 
from google.appengine.ext import db 
from google.appengine.ext import webapp 
from google.appengine.ext.webapp.util import run_wsgi_app 
from google.appengine.ext.webapp import template 
from google.appengine.api import mail 
import wsgiref.handlers 

# START Constants 
CONTENT_TYPE_HEADER = "Content-Type" 
CONTENT_TYPE_TEXT = "text/plain" 
XML_CONTENT_TYPE = "application/xml" 
XML_ENCODING = "utf-8" 
""" 
Allows you to specify IP addresses and associated "api_key"s to prevent others from using your app. 
Storage and Manipulation methods will check for this "api_key" in the POST/GET params. 
Retrieval methods don't use it (however you could enable them to use it, but maybe rewrite so you have a "read" key and a "write" key to prevent others from manipulating your data). 

Set "AUTH = False" to disable (allowing anyone use your app and CRUD your data). 

To generate a hash/api_key visit https://www.grc.com/passwords.htm 
To find your ip visit http://www.whatsmyip.org/ 
""" 
AUTH = False 
# END Constants 

# START Exception Handling 
class Error(StandardError): 
    pass 
class Forbidden(Error): 
    pass 

logging.getLogger().setLevel(logging.DEBUG) 

@contextlib.contextmanager 
def mailExcpHandler(ctx): 
    try: 
     yield {} 
    except (ValueError), exc: 
     xml_error_response(ctx, 400 ,'app.invalid_parameters', 'The indicated parameters are not valid: ' + exc.message) 
    except (Forbidden), exc: 
     xml_error_response(ctx, 403 ,'app.forbidden', 'You don\'t have permission to perform this action: ' + exc.message) 
    except (Exception), exc: 
     xml_error_response(ctx, 500 ,'system.other', 'An unexpected error in the web service has happened: ' + exc.message) 

def xml_error_response(ctx, status, error_id, error_msg): 
    ctx.error(status) 
    doc = Document() 
    errorcard = doc.createElement("error") 
    errorcard.setAttribute("id", error_id) 
    doc.appendChild(errorcard) 
    ptext = doc.createTextNode(error_msg) 
    errorcard.appendChild(ptext) 
    ctx.response.headers[CONTENT_TYPE_HEADER] = XML_CONTENT_TYPE 
    ctx.response.out.write(doc.toxml(XML_ENCODING)) 
# END Exception Handling 

# START Helper Methods 
def isAuth(ip = None, key = None): 
    if AUTH == False: 
     return True 
    elif AUTH.has_key(ip) and key == AUTH[ip]: 
     return True 
    else: 
     return False 

# END Helper Methods 


# START Request Handlers 
class Send(webapp.RequestHandler): 
    def post(self): 
     """ 
     Sends an email based on POST params. It will queue if resources are unavailable at the time. 

     Returns "Success" 

     POST Args: 
      to: the receipent address 
      from: the sender address (must be a registered GAE email) 
      subject: email subject 
      body: email body content 
     """ 
     with mailExcpHandler(self): 
      # check authorised 
      if isAuth(self.request.remote_addr,self.request.POST.get('api_key')) == False: 
       raise Forbidden("Invalid Credentials") 

      # read data from request 
      mail_to = str(self.request.POST.get('to')) 
      mail_from = str(self.request.POST.get('from')) 
      mail_subject = str(self.request.POST.get('subject')) 
      mail_plain = str(self.request.POST.get('plain')) 
      mail_html = str(self.request.POST.get('html')) 

      message = mail.EmailMessage() 
      message.sender = mail_from 
      message.to = mail_to 
      message.subject = mail_subject 
      message.body = mail_plain 
      if mail_html != None and mail_html != "": 
       message.html = mail_html 

      message.send() 

      self.response.headers[CONTENT_TYPE_HEADER] = CONTENT_TYPE_TEXT 
      self.response.out.write("Success") 


# END Request Handlers 

# START Application 
application = webapp.WSGIApplication([ 
                      ('/send', Send) 
                     ],debug=True) 

def main(): 
    run_wsgi_app(application) 

if __name__ == '__main__': 
    main() 

# END Application 
+0

あなたは何のpythonのバージョンを使用していて、どのようなバージョンでは、インポートされたライブラリがありますか? – Hyperboreus

+1

あなたは_Hebrew_文字を意味すると思います。私はテンプルの登場人物に出くわすことはないと思う。あなたは右のヘブライ文字です@agf – agf

+0

@HyperboreusのPythonのバージョンは2.7.1と1.4.2 GAE – Michael

答えて

0
mail_to = str(self.request.POST.get('to')) 
mail_from = str(self.request.POST.get('from')) 
mail_subject = str(self.request.POST.get('subject')) 
mail_plain = str(self.request.POST.get('plain')) 
mail_html = str(self.request.POST.get('html')) 

私はあなたが文字列に変換する必要があります疑います。 str()を使わずに試してみてください。

関連する問題