1. 程式人生 > >使用python傳送qq郵件

使用python傳送qq郵件

在看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