Skip to main content
 首页 » 编程设计

Python 谷歌应用引擎 : Send Mail Error

2025年05月04日17shangdawei

我正在编写一个简单的测试应用程序来使用 Python GAE 发送电子邮件。我在日志中收到以下错误。我试过空身和其他改变,但似乎没有任何效果。我需要进行任何配置更改吗?

追溯(最近的调用最后):

文件“/base/data/home/apps/s~xxxx/1.360190002979488583/email.py”,第 5 行,在
从 google.appengine.api 导入邮件 文件“/base/python27_runtime/python27_lib/versions/1/google/appengine/api/mail.py”,第 37 行,在
从电子邮件导入 MIMEBase
ImportError: 无法导入名称 MIMEBase

下面是我的 app.yaml 文件

application: mailer 
version: 1 
runtime: python27 
api_version: 1 
threadsafe: no 
 
handlers: 
- url: /email 
  script: email.py 
 
libraries: 
- name: webapp2 
  version: "2.5.1" 

下面是我的 email.py(本质上它与 https://developers.google.com/appengine/docs/python/mail/sendingmail 中提到的代码相同。我刚刚更改了一个有效的电子邮件地址)

from google.appengine.api import mail 
import sys 
 
message = mail.EmailMessage(sender="Example.com Support <valid-from-address@gmail.com>", 
                            subject="Your account has been approved") 
 
message.to = "Valid User <valid.user@gmail.com>" 
message.body = """ 
Dear Albert: 
 
Your example.com account has been approved.   
 
The example.com Team 
""" 
 
message.send() 

请您参考如下方法:

不要将脚本命名为 email.py;这就是阻止您从标准库的 email 模块导入的原因。