使用GAE代表Google Apps用户(转贴)发送电子邮件

时间:2015-01-02 05:27:26

标签: python forms google-app-engine

如果您只想向我指出有关此主题的回答问题,我先道歉。我搜索过,或者我没有得到它,或者我的问题不同。我正在尝试为我的网站上的网络用户创建一个表单,以便轻松与我联系。我不想让他们登录,或在我的网站上为他们创建帐户。我只是想填写表格;姓名,电子邮件,主题和消息,然后点击发送给我。我是Google应用域的超级管理员。

main.py

# [START imports]
import os
import urllib

from google.appengine.api import users
from google.appengine.ext import ndb
from google.appengine.api import mail

import jinja2
import webapp2

JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
    extensions=['jinja2.ext.autoescape'],
    autoescape=True)
# [END imports]

# [START main_page]
class MainPage(webapp2.RequestHandler):

    def get(self):
        template = JINJA_ENVIRONMENT.get_template('index.html')
        self.response.write(template.render())

    def post(self):
    name = self.request.get('name')
    message = mail.EmailMessage(to = 'my_google_apps_domain_user@mydomain.com',
                        sender = self.request.get('email'),
                    subject = self.request.get('subject'),
                    body = "From: " + name + " Message: " + self.request.get('message'))
    message.send()

    template = JINJA_ENVIRONMENT.get_template('index.html')
        self.response.write(template.render())
# [END main_page]

# [START sitemap_page]
class SiteMapPage(webapp2.RequestHandler):

    def get(self):
        template = JINJA_ENVIRONMENT.get_template('sitemap.xml')
        self.response.write(template.render())
# [END sitemap_page]

application = webapp2.WSGIApplication([('/', MainPage), ('/sitemap.xml', SiteMapPage)], debug=True)

表单上的index.html代码段

<form id="contact-form" method ="post">
                <div class="row">
                  <div class="col-md-6">
                    <div class="form-group">
                      <label for="name">Name</label>
                      <input type="text" class="form-control" id="name" name="name" placeholder="Enter name" required aria-required="true" />
                    </div>
                    <div class="form-group">
                      <label for="email">Email Address</label>
                      <div class="input-group">
                        <span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span>
                        </span>
                        <input type="email" class="form-control" id="email" name="email" placeholder="Enter email" required aria-required="true" />
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="subject">Subject</label>
                      <select id="subject" name="subject" class="form-control" required aria-required="true">
                        <option value="na" selected="">Choose One:</option>
                        <option value="service">General Customer Service</option>
                        <option value="suggestions">Suggestions</option>
                        <option value="product">Product Support</option>
                      </select>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <label for="name">Message</label>
                      <textarea name="message" id="message" class="form-control" rows="9" cols="25" required aria-required="true" placeholder="Message"></textarea>
                    </div>
                  </div>
                  <div class="col-md-12">
                    <button type="submit" class="btn btn-skin pull-right" id="btnContactUs">Send Message</button>
                  </div>
                </div>
              </form>

感谢您的帮助,如果您需要任何其他信息,请告知我们。

我收到了一个未经授权的发件人服务器错误;

Internal Server Error

The server has either erred or is incapable of performing the requested operation.

Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~www-is/1.381224894290286420/main.py", line 31, in post
    message.send()
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py", line 1115, in send
    raise ERROR_MAP[e.application_error](e.error_detail)
InvalidSenderError: Unauthorized sender

1 个答案:

答案 0 :(得分:0)

sender电子邮件地址应注册为GAE项目的应用程序管理员,或者您可以使用any_string@PROJECT_ID.appspotmail.com之类的内容。在您的情况下,您可以简单地将reply_to参数设置为用户的电子邮件地址,并使用来自其中一位管理员的发件人电子邮件。