python自动发送邮件不需要发件邮箱_python使用QQ邮箱实现自动发送邮件
最近用到Python自動發送郵件,主要就是三步,登錄郵件、寫郵件內容、發送,用到的庫是 smtplib 和 email,直接使用pip安裝即可
我使用的是QQ郵箱,首先需要設置QQ郵箱POP3/SMTP服務
記住這個授權碼,這個授權碼就是Python腳本中登錄郵箱時的密碼,而不是你平時登錄郵箱時的那個密碼
一.發送普通文本郵件#發送多種類型的郵件
from?email.mime.multipart?import?MIMEMultipart
msg_from?=?'1508691067@qq.com'?#?發送方郵箱
passwd?=?'xxx'??#就是上面的授權碼
to=?['1508691067@qq.com']?#接受方郵箱
#設置郵件內容
#MIMEMultipart類可以放任何內容
msg?=?MIMEMultipart()
conntent="這個是字符串"
#把內容加進去
msg.attach(MIMEText(conntent,'plain','utf-8'))
#設置郵件主題
msg['Subject']="這個是郵件主題"
#發送方信息
msg['From']=msg_from
#開始發送
#通過SSL方式發送,服務器地址和端口
s?=?smtplib.SMTP_SSL("smtp.qq.com",?465)
#?登錄郵箱
s.login(msg_from,?passwd)
#開始發送
s.sendmail(msg_from,to,msg.as_string())
print("郵件發送成功")
二.發送攜帶附件的郵件import?smtplib
from?email.mime.text?import?MIMEText
#發送多種類型的郵件
from?email.mime.multipart?import?MIMEMultipart
msg_from?=?'1508691067@qq.com'?#?發送方郵箱
passwd?=?'xxxxx'
to=?['1508691067@qq.com']?#接受方郵箱
#設置郵件內容
#MIMEMultipart類可以放任何內容
msg?=?MIMEMultipart()
conntent="這個是字符串"
#把內容加進去
msg.attach(MIMEText(conntent,'plain','utf-8'))
#添加附件
att1=MIMEText(open('result.xlsx','rb').read(),'base64','utf-8')?#打開附件
att1['Content-Type']='application/octet-stream'??#設置類型是流媒體格式
att1['Content-Disposition']='attachment;filename=result.xlsx'?#設置描述信息
msg.attach(att1)??#加入到郵件中
#設置郵件主題
msg['Subject']="這個是郵件主題"
#發送方信息
msg['From']=msg_from
#開始發送
#通過SSL方式發送,服務器地址和端口
s?=?smtplib.SMTP_SSL("smtp.qq.com",?465)
#?登錄郵箱
s.login(msg_from,?passwd)
#開始發送
s.sendmail(msg_from,to,msg.as_string())
print("郵件發送成功")
三.發送攜帶圖片的附件
同理,可以使用上面的方法也可以發送圖片附件import?smtplib
from?email.mime.text?import?MIMEText
#發送多種類型的郵件
from?email.mime.multipart?import?MIMEMultipart
msg_from?=?'1508691067@qq.com'?#?發送方郵箱
passwd?=?'xxxxx'
to=?['1508691067@qq.com']?#接受方郵箱
#設置郵件內容
#MIMEMultipart類可以放任何內容
msg?=?MIMEMultipart()
conntent="這個是字符串"
#把內容加進去
msg.attach(MIMEText(conntent,'plain','utf-8'))
#添加附件
att1=MIMEText(open('result.xlsx','rb').read(),'base64','utf-8')?#打開附件
att1['Content-Type']='application/octet-stream'??#設置類型是流媒體格式
att1['Content-Disposition']='attachment;filename=result.xlsx'?#設置描述信息
att2=MIMEText(open('1.jpg','rb').read(),'base64','utf-8')
att2['Content-Type']='application/octet-stream'??#設置類型是流媒體格式
att2['Content-Disposition']='attachment;filename=1.jpg'?#設置描述信息
msg.attach(att1)??#加入到郵件中
msg.attach(att2)
#設置郵件主題
msg['Subject']="這個是郵件主題"
#發送方信息
msg['From']=msg_from
#開始發送
#通過SSL方式發送,服務器地址和端口
s?=?smtplib.SMTP_SSL("smtp.qq.com",?465)
#?登錄郵箱
s.login(msg_from,?passwd)
#開始發送
s.sendmail(msg_from,to,msg.as_string())
print("郵件發送成功")
四.發送 html 格式的郵件import?smtplib
from?email.mime.text?import?MIMEText
#發送多種類型的郵件
from?email.mime.multipart?import?MIMEMultipart
import?datetime
msg_from?=?'1508691067@qq.com'?#?發送方郵箱
passwd?=?'xxxxxx'
to=?['1508691067@qq.com']?#接受方郵箱
#設置郵件內容
#MIMEMultipart類可以放任何內容
msg?=?MIMEMultipart()
#?conntent="這個是字符串"
#?#把內容加進去
#?msg.attach(MIMEText(conntent,'plain','utf-8'))
#添加附件
att1=MIMEText(open('result.xlsx','rb').read(),'base64','utf-8')?#打開附件
att1['Content-Type']='application/octet-stream'??#設置類型是流媒體格式
att1['Content-Disposition']='attachment;filename=result.xlsx'?#設置描述信息
att2=MIMEText(open('1.jpg','rb').read(),'base64','utf-8')
att2['Content-Type']='application/octet-stream'??#設置類型是流媒體格式
att2['Content-Disposition']='attachment;filename=1.jpg'?#設置描述信息
msg.attach(att1)??#加入到郵件中
msg.attach(att2)
now_time?=?datetime.datetime.now()
year?=?now_time.year
month?=?now_time.month
day?=?now_time.day
mytime?=?str(year)?+?"?年?"?+?str(month)?+?"?月?"?+?str(day)?+?"?日?"
fayanren="愛因斯坦"
zhuchiren="牛頓"
#構造HTML
content?=?'''
這個是標題,xxxx通知
您好:
以下內容是本次會議的紀要,請查收!
發言人:{fayanren}
主持人:{zhuchiren}
{mytime}
'''.format(fayanren=fayanren,?zhuchiren=zhuchiren,?mytime=mytime)
msg.attach(MIMEText(content,'html','utf-8'))
#設置郵件主題
msg['Subject']="這個是郵件主題"
#發送方信息
msg['From']=msg_from
#開始發送
#通過SSL方式發送,服務器地址和端口
s?=?smtplib.SMTP_SSL("smtp.qq.com",?465)
#?登錄郵箱
s.login(msg_from,?passwd)
#開始發送
s.sendmail(msg_from,to,msg.as_string())
print("郵件發送成功")
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。
標簽:QQ,qq,python,發送,MIMEText,msg,郵箱,郵件
來源: https://blog.51cto.com/14825302/2544811
總結
以上是生活随笔為你收集整理的python自动发送邮件不需要发件邮箱_python使用QQ邮箱实现自动发送邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java url headers,Jav
- 下一篇: websocket python爬虫_p