欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python調(diào)用工具包實(shí)現(xiàn)發(fā)送郵件服務(wù)

 更新時(shí)間:2023年05月12日 09:18:28   作者:Bruce小鬼  
這篇文章主要為大家詳細(xì)介紹了Python圖畫調(diào)用工具包實(shí)現(xiàn)發(fā)送郵件服務(wù)的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1.概述

這篇文章主要介紹python調(diào)用工具包實(shí)現(xiàn)發(fā)送郵件服務(wù)。在第二篇文章中介紹封裝郵件服務(wù)工具類,該工具類將可以應(yīng)用到其他的項(xiàng)目中。

2.郵件模塊介紹

python內(nèi)置了發(fā)送郵件的模塊,因此不需要單獨(dú)安裝。

  • smtplib 模塊負(fù)責(zé)發(fā)送郵件
  • email 模塊負(fù)責(zé)格式化郵件內(nèi)容樣式

2.1. 建立連接類型

當(dāng)使用smtplib模塊建立發(fā)送郵件連接時(shí)可以分為非安全和安全連接兩種方式。

安全連接: 當(dāng)python使用smtplib模塊調(diào)用郵箱服務(wù)端發(fā)送郵件過程,傳輸層使用了SSL加密,避免了明文傳遞郵件內(nèi)容,通過攔截請(qǐng)求獲取郵件內(nèi)容和登錄賬號(hào)密碼。

非安全連接:郵箱服務(wù)端采用明文發(fā)送郵件,通過連接請(qǐng)求可以獲取郵件內(nèi)容和登錄賬號(hào)密碼。

3.發(fā)送郵件

使用python發(fā)送郵件之前,需要登錄郵箱開啟SMTP服務(wù)才可以發(fā)送郵件。每家的郵箱開啟入口不同,開啟方式搜索具體產(chǎn)品的郵箱。

3.1.發(fā)送非安全連接郵件

建立非安全連接郵箱服務(wù)端默認(rèn)使用25端口發(fā)送郵件。

import smtplib
from email.mime.text import MIMEText
#設(shè)置服務(wù)器所需信息
#郵箱服務(wù)器地址
mail_host = 'mail.******.com'
#郵箱登錄用戶名
mail_user = '******@qq.com'
#密碼(部分郵箱為授權(quán)碼)
mail_pass = '123456'
#郵件發(fā)送方郵箱地址
sender = '******@qq.com'
#郵件接受方郵箱地址,注意需要[]包裹,這意味著你可以寫多個(gè)郵件地址群發(fā)
receivers = ['32321232@qq.com']
#設(shè)置email信息
#郵件內(nèi)容設(shè)置
message = MIMEText('content','plain','utf-8')
#郵件主題
message['Subject'] = 'title'
#發(fā)送方信息
message['From'] = sender
#接受方信息
message['To'] = receivers[0]
#登錄并發(fā)送郵件
try:
   # 建立非安全連接smtplib.SMTP()
    smtpObj = smtplib.SMTP()
    smtpObj.connect(mail_host,25)
    #登錄到服務(wù)器
    smtpObj.login(mail_user,mail_pass)
    #發(fā)送
    smtpObj.sendmail(
        sender,receivers,message.as_string())
    #退出
    smtpObj.quit()
    print('success')
except smtplib.SMTPException as e:
    print('error',e) #打印錯(cuò)誤

3.2.發(fā)送安全連接郵件

建立非安全連接郵箱服務(wù)端默認(rèn)使用465端口發(fā)送郵件。

import smtplib
from email.mime.text import MIMEText
#設(shè)置服務(wù)器所需信息
#郵箱服務(wù)器地址
mail_host = 'mail.******.com'
#郵箱登錄用戶名
mail_user = '******@qq.com'
#密碼(部分郵箱為授權(quán)碼)
mail_pass = '123456'
#郵件發(fā)送方郵箱地址
sender = '******@qq.com'
#郵件接受方郵箱地址,注意需要[]包裹,這意味著你可以寫多個(gè)郵件地址群發(fā)
receivers = ['32321232@qq.com']
#設(shè)置email信息
#郵件內(nèi)容設(shè)置
message = MIMEText('content','plain','utf-8')
#郵件主題
message['Subject'] = 'title'
#發(fā)送方信息
message['From'] = sender
#接受方信息
message['To'] = receivers[0]
# 登錄并發(fā)送郵件
# 建立安全連接SMTP_SSL,注意端口號(hào)默認(rèn)使用465
with smtplib.SMTP_SSL(mail_host, timeout=10, port=465) as smtpObj:
    try:
        # 登錄到服務(wù)器
        smtpObj.login(mail_user, mail_pass)
        # 發(fā)送
        smtpObj.sendmail(
            sender, receivers, message.as_string())
        print('success')
    except Exception as e:
        print(e)

在使用安全連接發(fā)送郵件時(shí)可能會(huì)拋出下面的異常,這個(gè)異常的含義是Python端與郵箱服務(wù)端建立連接SSL版本號(hào)不對(duì)。

ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:997)

這個(gè)異常出現(xiàn)的原因:

發(fā)送郵件的郵箱服務(wù)端沒有開啟SSL或者不支持,這種清空可以登錄郵箱服務(wù)端確認(rèn),如果不支持只能使用非安全建立連接。

端口號(hào)錯(cuò)誤造成,郵箱服務(wù)端模式使用465端口號(hào)連接SSL連接,如果python端設(shè)置的端口號(hào)與郵箱服務(wù)端端口號(hào)不一致建立連接也會(huì)報(bào)上面的異常。

4.設(shè)置郵件內(nèi)容格式

python內(nèi)置的email模塊負(fù)責(zé)設(shè)置郵件內(nèi)容格式,它包含三個(gè)模塊對(duì)應(yīng)郵件三種類型的格式。下面具體介紹他們的區(qū)別。

from email.mime.text import MIMEText    
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart    

4.1.郵件文本三種類型

1.MIMEText文本類型

發(fā)送普通的文本郵件內(nèi)容,MIMEText對(duì)象中有三個(gè)需要我們?cè)O(shè)置的參數(shù),一個(gè)是正文內(nèi)容,一個(gè)是正文內(nèi)容的類型,例如:”text/plain”和”text/html”,一個(gè)是正文內(nèi)容的編碼。

plain構(gòu)造普通文本

# 文本內(nèi)容
text_info = 'hello world '
# 構(gòu)造普通文本對(duì)象,包含三個(gè)參數(shù),郵件內(nèi)容,類型,編碼
text_sub = MIMEText(text_info,'plain', 'utf-8')  

完整代碼

import smtplib
from email.mime.text import MIMEText
# 創(chuàng)建郵件連接
def create_email_connect(mail_host, timeout, port):
    try:
        return smtplib.SMTP_SSL(host=mail_host, timeout=timeout, port=port)
    except Exception as e:
        print(e)
# 構(gòu)建郵件內(nèi)容模板
def email_content():
    # 郵件內(nèi)容設(shè)置
    text_info = 'hello world '
    message = MIMEText(text_info, 'plain', 'utf-8')
    # 郵件主題
    message['Subject'] = 'title'
    # 發(fā)送方信息
    message['From'] = sender_account
    # 接受方信息
    message['To'] = receivers_account[0]
    return message
def sent_email(mail_user, mail_pass, mail_host, sender_account, receivers_account, timeout=10, port=465):
    with create_email_connect(mail_host, timeout, port) as smtpObj:
        try:
            smtpObj.login(mail_user, mail_pass)
            # 發(fā)送郵件
            smtpObj.sendmail(
                sender_account, receivers_account, email_content().as_string())
            print('scusses')
        except Exception as e:
            print(e)
if __name__ == '__main__':
    # 設(shè)置服務(wù)器所需信息
    # 163郵箱服務(wù)器地址
    mail_host = 'smtp.163.com'
    # 163用戶名
    mail_user = '******@163.com'
    # 密碼(部分郵箱為授權(quán)碼)
    mail_pass = '******'
    # 郵件發(fā)送方郵箱地址
    sender_account = '******@163.com'
    # 郵件接受方郵箱地址,注意需要[]包裹,這意味著你可以寫多個(gè)郵件地址群發(fā)
    receivers_account = ['******@qq.com']
    sent_email(mail_user, mail_pass, mail_host, sender_account, receivers_account)

構(gòu)造html文本

# 郵件內(nèi)容
url = "https://xxxxx.com"
html_info = """
    <p>點(diǎn)擊以下鏈接,查看郵件詳情</p>
    <p><a href="%s">click me</a></p>
    <p>i am very glasses for you</p>
    """ % url
# 構(gòu)建郵件類型html對(duì)象
html_sub = MIMEText(html_info, 'html', 'utf-8')  
# 如果不加下邊這行代碼的話,上邊的文本是不會(huì)正常顯示的,會(huì)把超文本的內(nèi)容當(dāng)做文本顯示
html_sub["Content-Disposition"] = 'attachment; filename="email.html"'

構(gòu)造base64數(shù)據(jù)流,用于發(fā)送文件的時(shí)候使用,構(gòu)造附件代碼

txt_file = open(r'D:\python_files\files\hello_world.txt', 'rb').read()
txt = MIMEText(txt_file, 'base64', 'utf-8')
txt["Content-Type"] = 'application/octet-stream'
# 命名發(fā)送的附件
txt.add_header('Content-Disposition', 'attachment', filename='hello_world.txt')

2.MIMEImage圖片類型

構(gòu)造圖片類型對(duì)象

image_file = open(r'D:\python_files\images\test.png', 'rb').read()
image = MIMEImage(image_file)
image.add_header('Content-ID', '<image1>')
# 命名發(fā)送的圖片
image["Content-Disposition"] = 'attachment; filename="red_people.png"'

完整代碼

import smtplib
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
# 創(chuàng)建郵件連接
def create_email_connect(mail_host, timeout, port):
    try:
        return smtplib.SMTP_SSL(host=mail_host, timeout=timeout, port=port)
    except Exception as e:
        print(e)
# 構(gòu)建郵件內(nèi)容模板
def email_content():
    # 郵件內(nèi)容設(shè)置
    image_file = open(r'./白玉堂.jpg', 'rb').read()
    message = MIMEImage(image_file)
    # 命名發(fā)送的圖片
    message["Content-Disposition"] = 'attachment; filename="red_people.png"'
    # 郵件主題
    message['Subject'] = 'title'
    # 發(fā)送方信息
    message['From'] = sender_account
    # 接受方信息
    message['To'] = receivers_account[0]
    return message
def sent_email(mail_user, mail_pass, mail_host, sender_account, receivers_account, timeout=10, port=465):
    with create_email_connect(mail_host, timeout, port) as smtpObj:
        try:
            smtpObj.login(mail_user, mail_pass)
            # 發(fā)送郵件
            smtpObj.sendmail(
                sender_account, receivers_account, email_content().as_string())
            print('scusses')
        except Exception as e:
            print(e)
if __name__ == '__main__':
    # 設(shè)置服務(wù)器所需信息
    # 163郵箱服務(wù)器地址
    mail_host = 'smtp.163.com'
    # 163用戶名
    mail_user = '******@163.com'
    # 密碼(部分郵箱為授權(quán)碼)
    mail_pass = '******'
    # 郵件發(fā)送方郵箱地址
    sender_account = '******@163.com'
    # 郵件接受方郵箱地址,注意需要[]包裹,這意味著你可以寫多個(gè)郵件地址群發(fā)
    receivers_account = ['******@qq.com']
    sent_email(mail_user, mail_pass, mail_host, sender_account, receivers_account)

3.MIMEMultipart

對(duì)象創(chuàng)建的類型有三種,此模塊主要是通過attach方法把上邊構(gòu)造的內(nèi)容傳入到郵件的整體內(nèi)容中

郵件類型為”multipart/alternative”的郵件正文中包括純文本正文(text/plain)和超文本正文(text/html)。

郵件類型為”multipart/related”的郵件正文中包括圖片,聲音等內(nèi)嵌資源。

郵件類型為”multipart/mixed”的郵件包含附件,圖片,文本等都可以添加,所以發(fā)的內(nèi)容多的話一般是用這個(gè)的,選擇mixed類型,什么內(nèi)容都可以發(fā)。

MIMEMultipart使用方法

MIMEMultipart(‘mixed’).attach(MIMEText或者M(jìn)IMEImage對(duì)象),因?yàn)镸IMEMultipart對(duì)象代表郵件本身,把其他的構(gòu)造內(nèi)容添加到MIMEMultipart對(duì)象中就可以把文本,html,附件等一起發(fā)送。

構(gòu)造圖片類型內(nèi)容

import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# 創(chuàng)建郵件連接
def create_email_connect(mail_host, timeout, port):
    try:
        return smtplib.SMTP_SSL(host=mail_host, timeout=timeout, port=port)
    except Exception as e:
        print(e)
# 構(gòu)建郵件內(nèi)容模板
def email_content():
    # 郵件內(nèi)容設(shè)置
    image_file = open(r'./白玉堂.jpg', 'rb').read()
    # 創(chuàng)建MIMEImage類型對(duì)象
    mimei_image = MIMEImage(image_file)
    message = MIMEMultipart('mixed')
    # attach包裝MIMEImage對(duì)象
    message.attach(mimei_image)
    # 命名發(fā)送的圖片
    message["Content-Disposition"] = 'attachment; filename="red_people.png"'
    # 郵件主題
    message['Subject'] = 'title'
    # 發(fā)送方信息
    message['From'] = sender_account
    # 接受方信息
    message['To'] = receivers_account[0]
    return message
def sent_email(mail_user, mail_pass, mail_host, sender_account, receivers_account, timeout=10, port=465):
    with create_email_connect(mail_host, timeout, port) as smtpObj:
        try:
            smtpObj.login(mail_user, mail_pass)
            # 發(fā)送郵件
            smtpObj.sendmail(
                sender_account, receivers_account, email_content().as_string())
            print('scusses')
        except Exception as e:
            print(e)
if __name__ == '__main__':
    # 設(shè)置服務(wù)器所需信息
    # 163郵箱服務(wù)器地址
    mail_host = 'smtp.163.com'
    # 163用戶名
    mail_user = '******@163.com'
    # 密碼(部分郵箱為授權(quán)碼)
    mail_pass = '******'
    # 郵件發(fā)送方郵箱地址
    sender_account = '******@163.com'
    # 郵件接受方郵箱地址,注意需要[]包裹,這意味著你可以寫多個(gè)郵件地址群發(fā)
    receivers_account = ['******@qq.com']
    sent_email(mail_user, mail_pass, mail_host, sender_account, receivers_account)

構(gòu)造普通類型發(fā)送附件郵件

import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# 創(chuàng)建郵件連接
def create_email_connect(mail_host, timeout, port):
    try:
        return smtplib.SMTP_SSL(host=mail_host, timeout=timeout, port=port)
    except Exception as e:
        print(e)
# 構(gòu)建郵件內(nèi)容模板
def email_content():
    # 郵件內(nèi)容設(shè)置
    text_file = open(r'./白玉堂.jpg', 'rb').read()
    # 創(chuàng)建MIMEImage類型對(duì)象
    mime_obj = MIMEText(text_file, 'base64', 'utf-8')
    # 命名發(fā)送的圖片
    mime_obj.add_header('Content-Disposition', 'attachment', filename='red_people.jpg')
    mime_obj.add_header('Content-Type', 'application/octet-stream')
    # 構(gòu)建mime對(duì)象
    message = MIMEMultipart('mixed')
    # attach包裝MIMEImage對(duì)象
    message.attach(mime_obj)
    # 郵件主題
    message['Subject'] = '測(cè)試郵件'
    # 發(fā)送方信息
    message['From'] = sender_account
    # 接受方信息
    message['To'] = receivers_account[0]
    return message
def sent_email(mail_user, mail_pass, mail_host, sender_account, receivers_account, timeout=10, port=465):
    with create_email_connect(mail_host, timeout, port) as smtpObj:
        try:
            smtpObj.login(mail_user, mail_pass)
            # 發(fā)送郵件
            smtpObj.sendmail(
                sender_account, receivers_account, email_content().as_string())
            print('scusses')
        except Exception as e:
            print(e)
if __name__ == '__main__':
    # 設(shè)置服務(wù)器所需信息
    # 163郵箱服務(wù)器地址
    mail_host = 'smtp.163.com'
    # 163用戶名
    mail_user = '******@163.com'
    # 密碼(部分郵箱為授權(quán)碼)
    mail_pass = '******'
    # 郵件發(fā)送方郵箱地址
    sender_account = '******@163.com'
    # 郵件接受方郵箱地址,注意需要[]包裹,這意味著你可以寫多個(gè)郵件地址群發(fā)
    receivers_account = ['******@qq.com']
    sent_email(mail_user, mail_pass, mail_host, sender_account, receivers_account)

到此這篇關(guān)于Python調(diào)用工具包實(shí)現(xiàn)發(fā)送郵件服務(wù)的文章就介紹到這了,更多相關(guān)Python發(fā)送郵件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python中列表的常見操作梳理總結(jié)(二)

    python中列表的常見操作梳理總結(jié)(二)

    這篇文章主要介紹了python中列表的常見操作總結(jié),文章圍通過列表的索引與切片的相關(guān)資料展開全文詳細(xì)的內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07
  • Python爬蟲爬取糗事百科段子實(shí)例分享

    Python爬蟲爬取糗事百科段子實(shí)例分享

    在本篇文章里小編給大家整理了關(guān)于Python爬蟲爬取糗事百科段子實(shí)例內(nèi)容,需要的朋友們可以參考下。
    2020-07-07
  • 詳解如何用Python登錄豆瓣并爬取影評(píng)

    詳解如何用Python登錄豆瓣并爬取影評(píng)

    這篇文章主要介紹了如何用Python登錄豆瓣并爬取影評(píng),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • python求解三角形第三邊長(zhǎng)實(shí)例

    python求解三角形第三邊長(zhǎng)實(shí)例

    這篇文章主要介紹了python求解三角形第三邊長(zhǎng)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Elasticsearch映射字段數(shù)據(jù)類型及管理

    Elasticsearch映射字段數(shù)據(jù)類型及管理

    這篇文章主要介紹了Elasticsearch映射字段數(shù)據(jù)類型及管理的講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-04-04
  • Python使用plt庫實(shí)現(xiàn)繪制動(dòng)態(tài)曲線圖并導(dǎo)出為GIF或MP4

    Python使用plt庫實(shí)現(xiàn)繪制動(dòng)態(tài)曲線圖并導(dǎo)出為GIF或MP4

    這篇文章主要為大家詳細(xì)介紹了Python如何使用plt庫實(shí)現(xiàn)繪制動(dòng)態(tài)曲線圖并導(dǎo)出為GIF或MP4,文中的示例代碼講解詳細(xì),需要的可以了解一下
    2024-03-03
  • pycharm 使用tab跳出正在編輯的括號(hào)(){}{}等問題

    pycharm 使用tab跳出正在編輯的括號(hào)(){}{}等問題

    這篇文章主要介紹了pycharm 使用tab跳出正在編輯的括號(hào)(){}{}等問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • python爬取youtube視頻的示例代碼

    python爬取youtube視頻的示例代碼

    這篇文章主要介紹了python爬取youtube視頻的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • 詳解 Python中LEGB和閉包及裝飾器

    詳解 Python中LEGB和閉包及裝飾器

    這篇文章主要介紹了詳解 Python中LEGB和閉包及裝飾器的相關(guān)資料,主要介紹了函數(shù)作用域和閉包的理解和使用方法及Python中的裝飾器,需要的朋友可以參考下
    2017-08-08
  • python實(shí)現(xiàn)銀行管理系統(tǒng)

    python實(shí)現(xiàn)銀行管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)銀行管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10

最新評(píng)論