为什么我的应用程序只发送一封电子邮件,而不再发送?

2024-09-28 17:01:10 发布

您现在位置:Python中文网/ 问答频道 /正文

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

主.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)

附录yaml

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

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

libraries:
- name: webapp2
  version: latest

我已经成功地通过了GAE教程在Goggle的网站,但我很想找到教程,超越这一点。我发现很多都过时了,在试着跑步的时候给了我一大堆无可救药的错误。 在Pydev中使用Eclipse。用GAE发射器发射


Tags: toselfcomapplicationversionemailresponsecount
1条回答
网友
1楼 · 发布于 2024-09-28 17:01:10

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

相关问题 更多 >