为什么我的应用会发送一封电子邮件而不再发送?

时间:2014-03-17 04:30:20

标签: python google-app-engine email

简单应用,可在请求'/ email'时发送电子邮件。 部署后,当“/”或“/ email”发送时,它会发送一封电子邮件,然后不再发送电子邮件。 我认为我对代码如何在GAE上运行有一个基本的误解。

Main.py

import webapp2
from google.appengine.api import mail
count = 0 #to see how variables work
MyEmail = mail.EmailMessage(sender="IFG Cloud <ValidSender@gmail.com>",
                            subject="IFG Test Email")

MyEmail.to = "MyEmail@gmail.com>"
MyEmail.body = """IFG Test Message"""


class MainPage(webapp2.RequestHandler):
    count += 1
    def get(self):

        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('IFG Texting App.  Message test. ') #This works
        self.response.write(count) #count does not += 1, why? Do I need to use datastore?


class EmailWill(webapp2.RequestHandler):

    MyEmail.send() #This sends one email when you got to URL '/' or '/email' then upon refresh it sends no more.
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Sending Email to wwelker@gmail.com') #This works



application = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/email', EmailWill),                                   

], debug=True)

的app.yaml

application: ifgalert
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: Main.application

libraries:
- name: webapp2
  version: latest

我已经成功浏览了Goggle网站上的GAE教程,但我很想找到超越它的教程。我发现很多都是过时的,并且在试图运行时给我一堆无望的错误。 将Eclipse与Pydev结合使用。使用GAE Launcher启动。

1 个答案:

答案 0 :(得分:2)

MyEmail.send()行位于类定义的主体中,不在任何函数中。因此,它在声明类时执行,而不是在实际实例化为对象或调用get()时执行。

我创建了MyEmail并将其发送到get()函数:

class EmailWill(webapp2.RequestHandler):
    def get(self):
        MyEmail = mail.EmailMessage(
                        sender="IFG Cloud <ValidSender@gmail.com>",
                        subject="IFG Test Email")
        MyEmail.to = "MyEmail@gmail.com>"
        MyEmail.body = """IFG Test Message"""
        MyEmail.send()
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Sending Email to wwelker@gmail.com') #This works