在看flask,发现可以发送邮件,然后感觉垃圾邮件是不是这样发的,比着书没敲对,找了几个源码也没弄对,然后赛赛让我生气,我决定要把垃圾邮件写出来骚扰他
注意:
1. 安装sendmail
2. 在QQ邮箱设置打开SMTP,记住这个码
3. # -*- coding: utf-8 -*- 别乱写
缺点:
没有缺点,我学的少,看不出来
源码:
# -*- coding: utf-8 -*-
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
#参考 http://www.runoob.com/python/python-email.html
host_server = 'smtp.qq.com'
sender_qq = 'QQ号'
pwd = '"开启SMTP的时候显示的贼长字符串'
sender_qq_mail = '[email protected]'
receiver = '[email protected]'
mail_title = '必读邮件'
mail_content = '我来啦~'
#ssl登录
smtp = SMTP_SSL(host_server)
smtp.login(sender_qq, pwd)
msg = MIMEText(mail_content, "plain", 'utf-8')
msg["Subject"] = Header(mail_title, 'utf-8')
msg["From"] = sender_qq_mail
msg["To"] = receiver
smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
smtp.quit()
大胆想法:
每周一我们都要发邮件交作业,可不可以在网页上传作业、填写收件人邮箱,自动把作业传到服务器,然后服务器定时或者不定时的把作业发给收件人。毕竟每次还得打开电脑发作业好麻烦~坐等~
大胆想法实现了哈哈哈哈!!!
顺便补充了一下 5w。txt是用于记录谁在什么时候发送了什么给谁
# -*- coding: utf-8 -*-
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
import os
from flask import Flask, request, url_for, send_from_directory,render_template
from werkzeug import secure_filename
import socket,time
from email.mime.multipart import MIMEMultipart
app = Flask(__name__)
@app.route('/begin')
def begin():
return render_template('input_zy.html')
@app.route('/inputt', methods=['GET', 'POST'])
def inputt():
zy_name=request.form['zy_name']
receiver=request.form['rec_name']
subject=request.form['obj_name']
zy_own_name=request.form['zy_own_name']
return sczy(receiver,subject,zy_name,zy_own_name)
#return 'ok!'
#@app.route('/sczy')
def sczy(receiver,subject,zy_name,zy_own_name):
host_server = 'smtp.qq.com'
sender_qq = 'QQ号'
pwd = '对应码'
sender_qq_mail = '[email protected]'
#receiver = '[email protected]'
#创建一个带附件的实例
message = MIMEMultipart()
message['From'] = sender_qq_mail
message['To'] = receiver
#subject = '这是题目'
message['Subject'] = Header(subject, 'utf-8')
#邮件正文内容
#message.attach(MIMEText('这是摘要', 'plain', 'utf-8'))
# 构造附件1,传送指定目录下的 test.txt 文件
att1 = MIMEText(open("C:\\study\\zy_collect\\"+zy_name).read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename='+zy_own_name
message.attach(att1)
#ssl登录
smtp = SMTP_SSL(host_server)
smtp.login(sender_qq, pwd)
smtp.sendmail(sender_qq_mail, receiver, message.as_string())
smtp.quit()
f=open("C:\\study\\zy_collect\\5w.txt","a+")
f.write("from "+str(sender_qq_mail)+" to "+str(receiver )+" with "+str(zy_name)+" as "+str(zy_own_name)+" in ")
#f.write(sender_qq_mail+","+receiver+","+zy_name+","+zy_own_name+",")
f.write(time.strftime('%Y%m%d %H%M%S',time.gmtime(time.time())))
f.write("\n")
f.close()
return 'ok!'
def allowed_file(filename):
ALLOWED_EXTENSIONS = set(['txt','zip','rar','png', 'jpg', 'jpeg', 'gif'])
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
@app.route('/showzy')
def showzy():
zys=[]
path=os.listdir('C:\\study\\zy_collect')
for p in path:
if os.path.isfile:
zys.append(p)
return render_template('showzy.html', zys = zys)
@app.route('/sczy', methods=['GET', 'POST'])
def upload_file():
html = '''
<!DOCTYPE html>
<title>Upload File</title>
<h1>文件or图片上传</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=上传>
</form>
'''
app.config['UPLOAD_FOLDER'] = 'C:\\study\\zy_collect\\'
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
print(os.path.join(app.config['UPLOAD_FOLDER']))
file_url = url_for('uploaded_file', filename=filename)
return 'ok!'
return html
if __name__ == '__main__':
hostname = socket.gethostname()
ip='0.0.0.0'
app.run( host=ip,port=5000,debug=False )
input_zy.html:
<form method='post' action='/inputt'>
zy_name:<input type='text' name='zy_name'><br/>
rec_qq:<input type='text' name='rec_name'><br/>
obj_name:<input type='text' name='obj_name'><br/>
zy_own_name:<input type='text' name='zy_own_name'><br/>
<input type="submit"/>
</form>
showzy.html:
<table>
{% for zy in zys %}
<tr>
<td>{{zy}}</td>
</tr>
{%endfor%}
</table>
upload.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="gbk">
<title>Title</title>
</head>
<body>
<h1>文件上传示例</h1>
<form action="" enctype='multipart/form-data' method='POST'>
<input type="file" name="file">
<input type="submit" value="上传">
</form>
</body>
</html>
滴答!
qq号要自己填,对应码要自己填,文件目录啥的自己改改
没错!有错也不承认!用的python2.7