栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用SMTP从Python发送邮件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用SMTP从Python发送邮件

我使用的脚本非常相似。我将其发布在此处,作为如何使用email。*模块生成MIME消息的示例。因此可以轻松修改此脚本以附加图片等。

我依靠ISP添加日期时间标头。

我的ISP要求我使用安全的smtp连接发送邮件,我依靠smtplib模块(可从http://www1.cs.columbia.edu/~db2501/ssmtplib.py下载)

就像您的脚本中一样,用于在SMTP服务器上进行身份验证的用户名和密码(下面提供了伪值)在源中为纯文本格式。这是一个安全漏洞。但是最好的选择取决于您对这些保护有多认真(想要?)。

=======================================

#! /usr/local/bin/pythonSMTPserver = 'smtp.att.yahoo.com'sender =     'me@my_email_domain.net'destination = ['recipient@her_email_domain.com']USERNAME = "USER_NAME_FOR_INTERNET_SERVICE_PROVIDER"PASSWORD = "PASSWORD_INTERNET_SERVICE_PROVIDER"# typical values for text_subtype are plain, html, xmltext_subtype = 'plain'content="""Test message"""subject="Sent from Python"import sysimport osimport refrom smtplib import SMTP_SSL as SMTP       # this invokes the secure SMTP protocol (port 465, uses SSL)# from smtplib import SMTP       # use this for standard SMTP protocol   (port 25, no encryption)# old version# from email.MIMEText import MIMETextfrom email.mime.text import MIMETexttry:    msg = MIMEText(content, text_subtype)    msg['Subject']=       subject    msg['From']   = sender # some SMTP servers will do this automatically, not all    conn = SMTP(SMTPserver)    conn.set_debuglevel(False)    conn.login(USERNAME, PASSWORD)    try:        conn.sendmail(sender, destination, msg.as_string())    finally:        conn.quit()except:    sys.exit( "mail failed; %s" % "CUSTOM_ERROR" ) # give an error message


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/638116.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号