使用python傳送郵件帶不同種類的附件

2021-10-17 03:32:56 字數 3309 閱讀 9246

import datetime

import smtplib

from email.header import header

from email.mime.image import mimeimage

from email.mime.text import mimetext

from email.mime.multipart import mimemultipart

# 第三方 smtp 服務

mail_host =

"smtp.qq.com"

# smtp伺服器

mail_user =

"1020487224"

# 使用者名稱

mail_pass =

"sdgfdsagdasg"

# 授權密碼,非登入密碼

sender =

'[email protected]'

# 發件人郵箱(最好寫全, 不然會失敗)

receivers =

['[email protected]'

]# 接收郵件,可設定為你的qq郵箱或者其他郵箱

content =

'我用python'

title =

'人生苦短'

# 郵件主題

file_name_new1=r"e:\123\文件\測試開發\django\05.html"

)# 內容, 格式, 編碼

# message = mimetext(content, "html")

message[

'from']=

"{}"

.format

(sender)

message[

'to']=

",".join(receivers)

message[

'subject'

]= title

try: smtpobj = smtplib.smtp_ssl(mail_host,

465)

# 啟用ssl發信, 埠一般是465

smtpobj.login(mail_user, mail_pass)

# 登入驗證

smtpobj.sendmail(sender, receivers, message.as_string())

# 傳送

print

("mail has been send successfully."

)except smtplib.smtpexception as e:

print

(e)def

sendemail2()

: message = mimemultipart(

)# 內容, 格式, 編碼

message[

'from']=

"{}"

.format

(sender)

message[

'to']=

",".join(receivers)

message[

'subject'

]= title

#新增郵件內容

text_content = mimetext(content)

#text_content = mimetext(content, "html")

message.attach(text_content)

# 新增附件(html,py,txt等檔案)

open

(file_name_new1,

'rb'

).read())

# 開啟附件

annex1.add_header(

'content-disposition'

,'attachment'

, filename=file_name_new1)

message.attach(annex1)

open

(file_name_new2,

'rb'

).read())

# 開啟附件

annex2.add_header(

'content-disposition'

,'attachment'

, filename=file_name_new2)

message.attach(annex2)

# 新增

# 1.

open

(file_name_new3,

'rb'

).read())

# 開啟附件

annex3.add_header(

'content-disposition'

,'attachment'

, filename=file_name_new3)

message.attach(annex3)

#2. annex4 = mimeimage(

open

(file_name_new3,

'rb'

).read())

# 開啟附件

annex4.add_header(

'content-disposition'

,'attachment'

, filename=file_name_new3)

message.attach(annex4)

try:

smtpobj = smtplib.smtp_ssl(mail_host,

465)

# 啟用ssl發信, 埠一般是465

smtpobj.login(mail_user, mail_pass)

# 登入驗證

smtpobj.sendmail(sender, receivers, message.as_string())

# 傳送

smtpobj.quit(

)print

("mail has been send successfully."

)except smtplib.smtpexception as e:

print

(e)if __name__ ==

'__main__'

: sendemail2(

)

python傳送帶附件郵件

1.不包括附件的郵件 coding utf 8 import smtplib import string 傳送普通的文字郵件 郵件smtp的位址 host smtp.163.com 定義郵件的標題 subject 這是郵件標題 發件人 from 163.com 收件人 to qq.com 傳送的郵件...

如何使用Python傳送帶附件的郵件

1 首先要理解乙個常識 rfc rfc the request for comments 是乙個關於internet各種標準的文件,定義了很多的網路協議和資料格式,標準的internet郵件遵從rfc2822 internet message format 等幾個文件,其中rfc822中描述了郵件頭...

使用python實現傳送帶附件的郵件

python中的smtplib模組是內建的傳送郵件模組。在使用smtp模組傳送郵件之前,需要先開啟郵箱的smtp服務 以qq郵箱為例 設定 賬戶 smtp服務,根據提示開通smtp服務,記錄授權碼 smtp smtplib.smtp ssl host smtp.qq.com port 465 smt...