溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python怎樣基于smtplib發不同格式的郵件

發布時間:2021-02-02 10:17:13 來源:億速云 閱讀:184 作者:小新 欄目:開發技術

這篇文章主要介紹了Python怎樣基于smtplib發不同格式的郵件,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

常用郵箱SMTP、POP3域名及其端口號

發送普通文本內容的郵件

import smtplib
from email.header import Header
from email.mime.text import MIMEText

# smtp服務器信息
smtp_server = 'smtp.163.com'
server_port = 465

# 發送方信息
sender = '發送的郵箱地址'
password = '發送郵箱的smtp授權碼'

# 收件人地址,列表可發給多人
receivers = ['123456@qq.com', '3123123@qq.com', '12312@qq.com']

# 郵箱的正文內容,參數分別為內容,格式(plain 為純文本),編碼
msg = MIMEText('Here is content.', 'plain', 'utf-8')

# 郵件頭信息
msg['From'] = Header(sender) # 發件人
msg['To'] = Header(', '.join(receivers)) # 發到哪里,參數為字符串
msg['Subject'] = Header('This is subject') # 郵件標題

# 發送郵件的操作
try:
  server = smtplib.SMTP_SSL() # SSL加密傳輸
  server.connect(smtp_server, server_port) # 連接smtp,服務器和端口
  server.login(sender, password) # 登錄發信郵箱
  server.sendmail(sender, receivers, msg.as_string()) # 發送郵件
  server.quit() # 關閉服務器
  print('發送成功')

except smtplib.SMTPException:
  print('發送失敗')

發送html格式的郵件

Python發送HTML格式的郵件與發送純文本消息的郵件不同之處就是將MIMEText中_subtype設置為html

發送帶有附件的郵件

發送帶附件的郵件,首先要創建MIMEMultipart()實例,然后構造附件,如果有多個附件,可依次構造,最后利用smtplib.smtp發送。

import smtplib
from email.header import Header
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# smtp服務器信息
smtp_server = 'smtp.163.com'
server_port = 465

# 發送方信息
sender = '發送的郵箱地址'
password = '發送郵箱的smtp授權碼'

# 收件人地址,列表可發給多人
receivers = ['969831239@qq.com', '124123989@qq.com']

# 實例化,先添加正文內容
msg = MIMEMultipart()
msg.attach(MIMEText('This is content.', 'plain', 'utf-8'))

# 添加附件1
att1 = MIMEApplication(open('tips.py', 'rb').read(), 'utf-8')
att1['Content-Disposition'] = 'attachment; filename="down.py"'  # filename隨便起,是接收到的附件顯示名稱
# att1["Content-Type"] = 'application/octet-stream'  Content-Type默認為application/octet-stream
msg.attach(att1)

# 添加附件2
att2 = MIMEApplication(open('source.jpg', 'rb').read(), 'utf-8')
att2['Content-Disposition'] = 'attachment; filename="girl.jpg"'
msg.attach(att2)

# 郵件頭信息
msg['From'] = Header(sender) # 發件人
msg['To'] = Header(', '.join(receivers)) # 發到哪里,參數為字符串
msg['Subject'] = Header('This is subject') # 郵件標題

# 發送郵件的操作
try:
  server = smtplib.SMTP_SSL() # SSL加密傳輸
  server.connect(smtp_server, server_port) # 連接smtp,服務器和端口
  server.login(sender, password) # 登錄發信郵箱
  server.sendmail(sender, receivers, msg.as_string()) # 發送郵件
  server.quit() # 關閉服務器
  print('發送成功')

except smtplib.SMTPException:
  print('發送失敗')

發送正文帶有圖片的郵件

使用html格式的img標簽指定圖片cid顯示圖片,圖片需要用MIMEImage上傳并指定img Content-ID

import smtplib
from email.header import Header
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# smtp服務器信息
smtp_server = 'smtp.qq.com'
server_port = 465

# 發送方信息
sender = '發送的郵箱地址'
password = '發送郵箱的smtp授權碼'

# 收件人地址,列表可發給多人
receivers = ['dsaisu@163.com', '76das23021@qq.com', '253das58570@qq.com']

# 實例化,添加正文內容,html格式,<img>標簽 指定cid屬性的值等于上傳圖片中Content-ID的值
msg = MIMEMultipart()
msg.attach(MIMEText('<img src="cid:demo" />', 'html', 'utf-8'))  # 與下面的Content-ID的值相同

# 添加圖片
img = MIMEImage(open('source.jpg', 'rb').read())
img.add_header('Content-ID', 'demo')  # 與上面的cid相同
msg.attach(img)

# 郵件頭信息
msg['From'] = Header(sender) # 發件人
msg['To'] = Header(', '.join(receivers)) # 發到哪里,參數為字符串
msg['Subject'] = Header('This is subject') # 郵件標題

# 發送郵件的操作
try:
  server = smtplib.SMTP_SSL() # SSL加密傳輸
  server.connect(smtp_server, server_port) # 連接smtp,服務器和端口
  server.login(sender, password) # 登錄發信郵箱
  server.sendmail(sender, receivers, msg.as_string()) # 發送郵件
  server.quit() # 關閉服務器
  print('發送成功')

except smtplib.SMTPException:
  print('發送失敗')

感謝你能夠認真閱讀完這篇文章,希望小編分享的“Python怎樣基于smtplib發不同格式的郵件”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女