python 自動傳送郵件

2022-10-08 02:24:05 字數 2414 閱讀 8131

import smtplib

from email.mime.text import mimetext

from email.header import header

#配置郵箱資訊

mailhost="smtp.qq.com"

mailuser="*******"

mailpass="**********"

#配置傳送人和收件人資訊

sender="********@qq.com"

receivers=['********@qq.com']

#配置郵件內容

mailmessage= mimetext("yetangjian前來報到","plain","utf-8")

mailmessage['from']=header("頑皮老闆喝不醉",'utf-8')

mailmessage['to']=header("pangpang",'utf-8')

mailmessage['subject']=header("這是一封簡單的自動傳送郵件",'utf-8')

#傳送郵件

try:

serve=smtplib.smtp(mailhost)#呼叫伺服器

serve.login(mailuser,mailpass)#登入你的服務賬戶

serve.sendmail(sender,receivers,mailmessage.as_string())#呼叫發件郵箱,收件郵箱,傳送的內容

print("傳送成功")

except:

print("傳送失敗")

————————————————————————————————————————————

import smtplib

from email.mime.multipart import mimemultipart

from email.mime.text import mimetext

from email.header import header

import os

#配置郵箱資訊

mailhost="smtp.qq.com"

mailuser="*******"

mailpass="********"

#配置傳送人和收件人資訊

sender="********@qq.com"

receivers=['********@qq.com']

#配置郵件內容

mailmessage=mimemultipart()#多個mime物件

mailmessage.attach(content1)#新增內容

mailmessage['from']=header("yetangjian",'utf-8')#發件人

mailmessage['to']=header("pangpang",'utf-8')#收件人

mailmessage['subject']=header("yetangjian",'utf-8')#主題

#新增附件

file_name='15to20work.pdf'#檔名

fail_path=os.path.join("c:/users/administrator/desktop\python/"+file_name)#檔案路徑

pdf.add_header('content-disposition','attachment',filename=file_name)

mailmessage.attach(pdf)

return mailmessage

#傳送郵件

def send_mail(mailmessage):

try:

mailmessage=message_config()

serve=smtplib.smtp(mailhost)#呼叫伺服器

serve.login(mailuser,mailpass)#登入你的服務賬戶

serve.sendmail(sender,receivers,mailmessage.as_string())#呼叫發件郵箱,收件郵箱,傳送的內容

print("傳送成功")

except:

print("傳送失敗")

if __name__ == "__main__":

print("開始傳送郵件")

mailmessage=message_config()#呼叫配置

send_mail(mailmessage)

print("傳送完成")

python 自動傳送郵件

測試環境 python3.4 發件伺服器 網易yeah 收件伺服器 移動139郵箱 注意 一定要在發件的郵箱裡面設定pop3 smtp imap為開啟狀態,要不然會報錯 535 error authentication failed 通過本程式,可以自動給自己的手機郵箱傳送郵件,在手機郵箱中把發件位...

Python自動傳送郵件

本文主要介紹如何使用python實現郵件的自動傳送。首先這個功能要求使用的模組是stmplib,email。smtp message transport protocol 簡單資訊傳輸協議,只能由於傳送郵件。下面是簡要的程式 usr bin python coding utf 8 import sm...

Python 自動傳送郵件

可直接複製進行呼叫 修改main裡對應引數即可 usr bin env python coding utf 8 time 2022 2 11 11 05 author file myemail.py version 1.0 function import datetime import smtpli...