首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python : smtplib sendmail函数

Python : smtplib sendmail函数
EN

Stack Overflow用户
提问于 2012-06-06 04:26:36
回答 2查看 2.6K关注 0票数 2

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

代码语言:javascript
复制
#!/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:])

这个脚本运行得很好,因为我已经通过命令行尝试过,如下所示:

代码语言:javascript
复制
./scriptname 'Reciepent Name' 'reciepent@gmail.com'

但是,我想从我的一个应用程序处理程序中调用激活脚本,如下所示。

代码语言:javascript
复制
import activation

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

但是当我这样做时,脚本返回None。有人能想出解决这个问题的办法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-06 04:41:17

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

票数 7
EN

Stack Overflow用户

发布于 2012-06-06 05:09:13

如果您只需要发送激活电子邮件,我的AE-BaseApp项目有一个可以使用的工作示例。AE-BaseApp/Python有一个指向我的Github存储库的链接,您可以在那里派生或下载代码。我也有一个正常工作的处理程序来接收/回复邮件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10904418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档