由于个人比较懒,每次写邮件时总要去打开邮箱地址然后登录,有的时候还容易忘记密码,感觉很不方便,于是就想写一个在txt文件中写内容,然后直接读取,执行脚本文件,直接发送内容。
还有很多需要改进的地方,如上传下载文件等,接收文件提醒,阅读文件内容等。还有一些配置内容也不是很方便。
py文件如下:
# !/usr/bin/python
# -*-coding:utf-8-*-
import smtplib
from email.mime.text import MIMEText
def send_email(host, username, passwd, send_to, subject, content):
msg = MIMEText(content)
my_email = username+"<"+username+">"
msg['From'] = my_email
msg['Subject'] = subject
msg['To'] = ",".join(send_to)
try:
# server = smtplib.SMTP()
# server.connect(host)
server = smtplib.SMTP_SSL(host,465)
server.ehlo()
# server.starttls()
server.login(username, passwd)
###第一种方法写的时候遇到了困难,问题没有是有strip(),因为此处使用了 msg.as_string(),应该是将内容转化成了str,然后又进行的处理
server.sendmail(username, send_to, msg.as_string())
server.close()
return 'sucessfully'
except Exception as e:
print 'Exception: send email failed', e
return 'failed to send mail'
if __name__ == '__main__':
filename = "emails_txt"
f = open(filename)
lines = f.readlines()
# file = f.read()
#######################方法1############
###此种方法是在txt文件中配置内容和写文件内容
host ="smtp.qq.com"
username = lines[1].split(':')[-1].strip()
# # host = "smtp." + str(username.split('@')[-1])
passwd = lines[2].split(':')[-1].strip()
to_list = lines[3].split(':')[-1].strip().split(',')
subject = lines[4].split(':')[-1].strip()
content = lines[5].split(':')[-1]
# print type(passwd), passwd
# print type(to_list), to_list
# print type(subject), subject
# print type(content), content
send_email(host, username, passwd, to_list, subject, content)
#######################方法2############
###下面这种方法是在程序中直接写内容,一旦文件内容比较多的时候,就很不适合使用了
"""
host = 'smtp.qq.com'
username = '1663462979@qq.com'
#此password为进入QQ邮箱设置页面,开启SMTP服务,发短信获取的授权码,而非通常我们使用的password
passwd = 'zcgmkptvlzqgfcef'
to_list = ['1663462979@qq.com','zgducas@163.com']
subject = "txt文件发送邮件"
content = "用python发送邮件"
content = file
# print type(passwd), passwd
# print type(to_list), to_list
# print type(subject), subject
# print type(content), content
if send_email(host, username, passwd, to_list, subject, content):
print "done!"
else:
print "failed!"
"""
txt文件
host:smtp.qq.com
username:1663462997@qq.com
passwd:zcgmkptvzlqgfcff
to_list:1663462997@qq.com,zgd_ucas@163.com
subject:这个邮件
content:试试能行不参考学习链接:https://www.programcreek.com/python/example/6443/smtplib.SMTP_SSL。
https://blog.youkuaiyun.com/zniahfag/article/details/51387996。
本文介绍了如何使用Python的smtplib.SMTP_SSL()模块实现从txt文件读取内容,自动登录邮箱并发送邮件。虽然目前功能较为基础,未来计划扩展到上传下载文件、接收文件提醒及阅读文件内容等更复杂的操作。
3366

被折叠的 条评论
为什么被折叠?



