python : smtplib sendmail function

标签 python google-app-engine sendmail smtplib

我有一个激活邮件发送脚本如下。

#!/usr/bin/python

__author__ = 'Amyth Arora (***@gmail.com)'

import smtplib
import string
import sys
import random

from email.MIMEText import MIMEText


def generate_activation_key(size=64, chars=string.ascii_lowercase + string.digits):
  return ''.join(random.choice(chars) for x in range(size))


def generate_activation_url(key):
  return 'http://www.example.com/users/activate/' + key

def Activate(name, to_addr):
  sender, reciever = 'mymail@gmail.com', to_addr
  act_url = generate_activation_url(generate_activation_key())
  main_mssg = """
  Dear %s,

  Thakyou for creating an account with Example.com. You are now just one click away from using your example account. Please click the following link to verify this email address and activate your account. 

  Please Note, You must complete this step to become a registered member of example. you will only need to visit this url once in order to activate your account.

  ============================
  Activation Link Below:
  ============================

  %s


  if you are facing problems activating your account please contact our support team at
  support@example.com

  Best Regards,

  Example Team
  """ % (name, act_url)


  message = MIMEText(main_mssg)
  message['From'] = 'Example <' + sender + '>'
  message['To'] = reciever
  message['MIME-Version'] = '1.0'
  message['Content-type'] = 'text/HTML'
  message['Subject'] = 'Activate Your Example Account'
  try:
    smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
    smtpObj.set_debuglevel(1)  # Swith On/Off Debug Mode
    smtpObj.ehlo()
    smtpObj.starttls()
    smtpObj.ehlo()
    smtpObj.login('mymail@gmail.com', 'mypass')
    smtpObj.sendmail(sender, reciever, message.as_string())
    smtpObj.close()
    return act_url
  except:
    return None

def main(argv):
  if len(argv) != 2:
    sys.exit(1)
  Activate(argv[0], argv[1])


if __name__ == '__main__':
  main(sys.argv[1:])

脚本工作正常,因为我已经通过这样的命令行尝试过它:

./scriptname 'Reciepent Name' 'reciepent@gmail.com'

但是我想像这样从我的一个应用程序处理程序中调用激活脚本。

import activation

act_key = activation.Activate('Reciepent Name', 'reciepent@gmail.com')

但是当我这样做时,脚本返回 None。谁能弄清楚如何解决这个问题?

最佳答案

App Engine 不允许用户使用 smtplib 库发送电子邮件。相反,您需要使用提供的 api。文档可在此处找到:https://developers.google.com/appengine/docs/python/mail

关于 python : smtplib sendmail function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10904418/

相关文章:

python - 使用Django ORM的bulk_create函数创建后如何高效地获取对象?

Python:BeautifulSoup Findall 跳到下一个标签

python - 在 Python 中查找字符串中的精确子字符串

google-app-engine - Google App Engine for Golang 中的 index.yaml 在哪里导入?

php - Sendmail 未在 XAMPP 中发送电子邮件

python - 在python中,如何对没有返回值的函数进行单元测试?

java - 在 App Engine 中查询数据存储的最有效方式

javascript - NodeJS 创建 CSV 直接写入 Google Storage

linux - 用户更改密码时发送电子邮件

php - 在 PHP 中使用 ini_set 更改 sendmail_path