Python登錄QQ郵箱發(fā)送郵件的實(shí)現(xiàn)示例
最近使用QuickBI訂閱功能,發(fā)現(xiàn)對(duì)訂閱內(nèi)容有1w行限制,便想到自己寫代碼繞過(guò)這個(gè)限制。
代碼如下:
# This is a sample Python script.
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
import os
import pymysql
import openpyxl
import pandas as pd
import email.utils
sql = '''select id,name from t'''
//將本地文件以附件的方式發(fā)送
def send_mail(message, subject, sender_show, recipient_show, to_addrs, cc_show=''):
'''
:param message: str 郵件內(nèi)容
:param subject: str 郵件主題描述
:param sender_show: str 發(fā)件人顯示,不起實(shí)際作用如:"xxx"
:param recipient_show: str 收件人顯示,不起實(shí)際作用 多個(gè)收件人用','隔開如:"xxx,xxxx"
:param to_addrs: str 實(shí)際收件人
:param cc_show: str 抄送人顯示,不起實(shí)際作用,多個(gè)抄送人用','隔開如:"xxx,xxxx"
'''
# 填寫真實(shí)的發(fā)郵件服務(wù)器用戶名、密碼
user = 'user_name@qq.com'
password = 'jxsnhxlaerizbihi' //授權(quán)碼
host = 'smtp.qq.com'
# 郵件內(nèi)容
# msg = MIMEText(message, 'plain', _charset="utf-8")
content = MIMEText(message)
msg = MIMEMultipart() # 多個(gè)MIME對(duì)象
msg.attach(content) # 添加內(nèi)容
# 郵件主題描述
msg["Subject"] = subject
# 發(fā)件人顯示,不起實(shí)際作用
msg["From"] = sender_show
# 收件人顯示,不起實(shí)際作用
msg["To"] = recipient_show
# 抄送人顯示,不起實(shí)際作用
msg["Cc"] = cc_show
cur_dir = os.path.dirname(os.path.realpath(__file__))
file_name = 'filename.xlsx' # 文件名
file_path = os.path.join(cur_dir, file_name) # 文件路徑
xlsx = MIMEApplication(open(file_path, 'rb').read()) # 打開Excel,讀取Excel文件
xlsx["Content-Type"] = 'application/octet-stream' # 設(shè)置內(nèi)容類型
xlsx.add_header('Content-Disposition', 'attachment', filename=file_name) # 添加到header信息
msg.attach(xlsx)
with SMTP_SSL(host=host, port=465) as smtp:
# 登錄發(fā)郵件服務(wù)器
smtp.login(user=user, password=password)
# 實(shí)際發(fā)送、接收郵件配置
# smtp.sendmail(from_addr=user, to_addrs=to_addrs.split(','), msg=msg.as_string())
smtp.sendmail(from_addr=user, to_addrs=to_addrs.split(','), msg=msg.as_string())
//從mysql中查詢數(shù)據(jù)寫入本地xlsx文件
def get_data():
con_engine = pymysql.connect(host='host_address',
user='app_read', password='pwd', database='db_name', port=3306, charset='utf8') # 通過(guò)參數(shù)形式傳遞,參數(shù)是字符串形式
global sql
df = pd.read_sql(sql, con_engine)
file = 'file_name.xlsx'
if os.path.exists(file):
os.remove(file)
df.to_excel(file, 'sheet1', index=False)
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
get_data()
send_mail('見附件', '標(biāo)題', 'sender', 'recipient','jjt@xxx.com')代碼中主要有兩個(gè)步驟:
- 從MySql中查詢數(shù)據(jù)通過(guò)pandas形成本地Excel文件
- 將本地Excel文件以附件的方式發(fā)送到目標(biāo)郵箱
就想人為發(fā)郵件先登錄QQ郵箱一樣,借助代碼自動(dòng)發(fā)送也需要用戶名密碼登錄QQ SMTP服務(wù)器,只是這里的密碼是在QQ郵箱開啟SMTP服務(wù)獲取的授權(quán)碼:

這樣借助Python就能實(shí)現(xiàn)發(fā)送郵件的功能,若需定時(shí)發(fā)送借助Crontab即可實(shí)現(xiàn)。
到此這篇關(guān)于Python登錄QQ郵箱發(fā)送郵件的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Python登錄QQ郵箱發(fā)送郵件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python使用QQ郵箱實(shí)現(xiàn)自動(dòng)發(fā)送郵件
- python3通過(guò)qq郵箱發(fā)送郵件以及附件
- python實(shí)現(xiàn)QQ郵箱發(fā)送郵件
- Python使用QQ郵箱發(fā)送郵件實(shí)例與QQ郵箱設(shè)置詳解
- Python使用QQ郵箱發(fā)送郵件報(bào)錯(cuò)smtplib.SMTPAuthenticationError
- python3使用QQ郵箱發(fā)送郵件
- 淺談Python用QQ郵箱發(fā)送郵件時(shí)授權(quán)碼的問(wèn)題
- Python利用QQ郵箱發(fā)送郵件的實(shí)現(xiàn)方法(分享)
- Python實(shí)現(xiàn)給qq郵箱發(fā)送郵件的方法
相關(guān)文章
Python采集大學(xué)教務(wù)系統(tǒng)成績(jī)單實(shí)戰(zhàn)示例
這篇文章主要為大家介紹了Python采集大學(xué)教務(wù)系統(tǒng)成績(jī)單實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
python utc datetime轉(zhuǎn)換為時(shí)間戳的方法
今天小編就為大家分享一篇python utc datetime轉(zhuǎn)換為時(shí)間戳的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Python獲取當(dāng)前公網(wǎng)ip并自動(dòng)斷開寬帶連接實(shí)例代碼
這篇文章主要介紹了Python獲取當(dāng)前公網(wǎng)ip并自動(dòng)斷開寬帶連接實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01

