#!/usr/bin/env/python3
# -*- coding:utf-8 -*-
'''
Author:leo
Date&Time:2020/5/28 and 15:44
Project:Python3
FileName:20200528_email.py
Description:
QQ郵件發送
'''
import smtplib # smtplib 用於郵件的發信動作
from email.mime.text import MIMEText # email 用於構建郵件內容
from email.header import Header # 用於構建郵件頭
from 抵押貸項目sit.library.basic import Basic
import time, random
logTime = time.strftime('%Y%m%d_%H:%M:%S', time.localtime(time.time()))
curTime = time.strftime('%Y%m%d %H%M%S', time.localtime(time.time()))
class TX_email():
def __init__(self):
self.logTime = time.strftime('%Y%m%d_%H:%M:%S', time.localtime(time.time()))
self.curTime = time.strftime('%Y%m%d %H%M%S', time.localtime(time.time()))
self.nowdate_8, self.nowtime_6, self.random_3 = self.curTime.split(" ")[0], self.curTime.split(" ")[1], str(random.randint(100, 999))
def send_email(self, title='python_title', message='python test message', send_status=False):
if send_status == True:
# 發信方的信息:發信郵箱,QQ 郵箱授權碼
from_addr = '[email protected]'
password = '123456'
# 收信方郵箱
to_addr = '[email protected]'
# 發信服務器
smtp_server = 'smtp.qq.com'
# 郵箱正文內容,第一個參數爲內容,第二個參數爲格式(plain 爲純文本),第三個參數爲編碼
msg = MIMEText(message, 'plain', 'utf-8')
# 郵件頭信息
msg['From'], msg['To'], msg['Subject'] = Header(from_addr), Header(to_addr), Header(title)
# 開啓發信服務,這裏使用的是加密傳輸
server = smtplib.SMTP_SSL()
server.connect(smtp_server, 465)
# 登錄發信郵箱
server.login(from_addr, password)
# 發送郵件
server.sendmail(from_addr, to_addr, msg.as_string())
# 關閉服務器
server.quit()
Basic().write_txt(filename=f"run_file\\{self.nowdate_8[:]}_runlog.txt", mode="a+", log_info="(email:)" + "----郵件發送成功----" + "\n(messg:)" + str(f"{message}"))
print(f"\n----郵件發送成功----")
if __name__ == '__main__':
# # 郵件通知
tx_message = "test"
TX_email().send_email(title=f"測試數據:{logTime}", message=f"{tx_message}")