<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Python `smtplib`教程 > 原文: [http://zetcode.com/python/smtplib/](http://zetcode.com/python/smtplib/) Python `smtplib`教程展示了如何使用`smtplib`模塊在 Python 中發送電子郵件。 要發送電子郵件,我們使用 Python 開發服務器,Mailtrap 在線服務和共享的網絡托管郵件服務器。 ## SMTP 簡單郵件傳輸協議(SMTP)是用于電子郵件傳輸的通信協議。它是一個互聯網標準,該標準于 1982 年由 RFC 821 首次定義,并于 2008 年由 RFC 5321 更新為擴展 SMTP 添加。 郵件服務器和其他郵件傳輸代理使用 SMTP 發送和接收郵件。 ## `smtplib`模塊 `smtplib`是一個 Python 庫,用于使用簡單郵件傳輸協議(SMTP)發送電子郵件。 `smtplib`是內置模塊; 我們不需要安裝它。 它抽象了 SMTP 的所有復雜性。 ## 郵件服務器 要實際發送電子郵件,我們需要有權訪問郵件服務器。 Python 帶有一個簡單的開發郵件服務器。 Mailslurper 是易于使用的本地開發服務器。 共享的虛擬主機提供商使我們可以訪問郵件服務器。 我們可以在帳戶中找到詳細信息。 > **注意**:避免使用 Gmail,因為它是高度安全的服務器,并且使其工作非常復雜。 實際上,互聯網上的大多數(如果不是全部)示例演示了如何通過 Gmail 服務器發送電子郵件,這些示例都無法正常工作。 而是使用開發服務器或共享的虛擬主機服務器。 最后,我們可以使用 Web 服務。 有開發 Web 服務(例如 MailTrap 或 MailSlurp)或生產服務(例如 Mailgun 或 Mandrill)。 ## 使用 Python 內置郵件服務器 ```py $ python -m smtpd -c DebuggingServer -n localhost:1025 ``` 我們在端口 1025 上啟動 Python 內置郵件服務器。 `built_in.py` ```py #!/usr/bin/env python import smtplib from email.mime.text import MIMEText sender = 'admin@example.com' receivers = ['info@example.com'] port = 1025 msg = MIMEText('This is test mail') msg['Subject'] = 'Test mail' msg['From'] = 'admin@example.com' msg['To'] = 'info@example.com' with smtplib.SMTP('localhost', port) as server: # server.login('username', 'password') server.sendmail(sender, receivers, msg.as_string()) print("Successfully sent email") ``` 我們向本地開發郵件服務器發送一條簡單的文本消息。 ```py sender = 'admin@example.com' receivers = ['info@example.com'] ``` 我們提供發送者和接收者。 `example.com`是專門用于文檔中的說明性示例的域名。 ```py msg = MIMEText('This is test mail') msg['Subject'] = 'Test mail' msg['From'] = 'admin@example.com' msg['To'] = 'info@example.com' ``` `MimeText`用于發送文本電子郵件。 我們提供主題,從選項到選項。 ```py with smtplib.SMTP('localhost', port) as server: ... ``` `SMTP`類管理與 SMTP 服務器的連接。 ```py # server.login('username', 'password') ``` 由于我們使用本地開發服務器,因此不必登錄。 ```py server.sendmail(sender, receivers, msg.as_string()) ``` 電子郵件帶有`sendmail()`發送。 ```py $ python -m smtpd -c DebuggingServer -n localhost:1025 ---------- MESSAGE FOLLOWS ---------- b'Content-Type: text/plain; charset="us-ascii"' b'MIME-Version: 1.0' b'Content-Transfer-Encoding: 7bit' b'Subject: Test mail' b'From: admin@example.com' b'To: info@example.com' b'X-Peer: ::1' b'' b'This is test mail' ------------ END MESSAGE ------------ ``` 發送電子郵件后,我們會收到此消息。 ## 發送郵件到 Mailtrap Mailtrap 提供了一項免費計劃,使我們每個月可以發送 500 封郵件。 設置 Mailtrap 非常容易。 如果我們擁有 Github 或 Google 帳戶,則只需幾秒鐘。 設置頁面中提供了必要的憑據。 另外,還有一些簡短的代碼示例顯示了如何使用服務,包括`smtplib`,`Django`或`Flask`。 `mailtrap_simple.py` ```py #!/usr/bin/env python import smtplib from email.mime.text import MIMEText sender = 'admin@example.com' receiver = 'info@example.com' msg = MIMEText('This is test mail') msg['Subject'] = 'Test mail' msg['From'] = 'admin@example.com' msg['To'] = 'info@example.com' user = 'username' password = 'passoword' with smtplib.SMTP("smtp.mailtrap.io", 2525) as server: server.login(user, password) server.sendmail(sender, receiver, msg.as_string()) print("mail successfully sent") ``` 該示例將簡單郵件發送到 Mailtrap 帳戶。 ```py server.login(user, password) ``` 用戶名和密碼在設置頁面中給出; 它們由隨機字符組成,例如 24h328df3e32。 ## 發送帶有附件的電子郵件 當我們有附件或要提供相同內容的替代版本(例如純文本/ HTML 版本)時,將使用`MIMEMultipart`。 `words.txt` ```py falcon blue sky cloud ``` 我們有一個簡單的文本文件。 `mailtrap_attachment.py` ```py #!/usr/bin/python import smtplib from os.path import basename from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication sender = 'admin@example.com' receiver = 'info@example.com' msg = MIMEMultipart() msg['Subject'] = 'Test mail with attachment' msg['From'] = 'admin@example.com' msg['To'] = 'info@example.com' filename = 'words.txt' with open(filename, 'r') as f: part = MIMEApplication(f.read(), Name=basename(filename)) part['Content-Disposition'] = 'attachment; filename="{}"'.format(basename(filename)) msg.attach(part) user = 'username' password = 'password' with smtplib.SMTP("smtp.mailtrap.io", 2525) as server: server.login(user, password) server.sendmail(sender, receiver, msg.as_string()) print("Successfully sent email") ``` 該示例向 Mailtrap 發送帶有文本文件附件的電子郵件。 ```py filename = 'words.txt' with open(filename, 'r') as f: part = MIMEApplication(f.read(), Name=basename(filename)) ``` 我們閱讀了文本文件的內容。 ```py part['Content-Disposition'] = 'attachment; filename="{}"'.format(basename(filename)) msg.attach(part) ``` 附件通過`attach()`方法添加。 ## 使用 STARTTLS 的郵件陷阱 Mailtrap 在任何 SMTP 端口上都不支持 SSL,它僅支持`STARTTLS`。 如果我們嘗試使用 SSL,則會收到以下錯誤消息: ```py ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1045) ``` 所謂的機會 TLS (傳輸層安全性)是純文本通信協議的擴展。 它提供了一種將純文本連接升級為加密(TLS 或 SSL)連接的方法,而不是使用單獨的端口進行加密通信。 為此,一些協議使用名為`STARTTLS`的命令。 它主要旨在作為被動監視的對策。 `mailtrap_secured.py` ```py #!/usr/bin/env python import smtplib from email.mime.text import MIMEText port = 465 sender = 'admin@example.com' receiver = 'info@example.com' msg = MIMEText('Secured test mail') msg['Subject'] = 'Test mail' msg['From'] = 'admin@example.com' msg['To'] = 'info@example.com' user = 'username' password = 'password' with smtplib.SMTP("smtp.mailtrap.io", port) as server: server.starttls() # Secure the connection server.login(user, password) server.sendmail(sender, receiver, msg.as_string()) print("mail successfully sent") ``` 該示例將電子郵件發送到具有機會性 TLS 的 Mailtrap 帳戶。 ```py server.starttls() # Secure the connection ``` `starttls()`將與 SMTP 服務器的連接置于 TLS 模式。 ## 通過 SSL 發送郵件 以下示例通過 SSL 發送電子郵件。 使用了 Web 托管 SMTP 服務器(來自 websupport.sk)。 `send_mail_ssl.py` ```py #!/usr/bin/env python import smtplib, ssl from email.mime.text import MIMEText sender = 'admin@example.com' receivers = ['info@example.com'] port = 465 user = 'admin@example.com' password = 'password' msg = MIMEText('This is test mail') msg['Subject'] = 'Test mail' msg['From'] = 'admin@example.com' msg['To'] = 'info@example.com' context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.websupport.sk", port, context=context) as server: server.login(user, password) server.sendmail(sender, receivers, msg.as_string()) print('mail successfully sent') ``` `SMTP_SSL`通過 SSL 加密的套接字連接。 在本教程中,我們使用 Python `smtplib`模塊發送電子郵件。 您可能也對以下相關教程感興趣: [Django 電子郵件教程](/django/email/), [Python Jinja 教程](/python/jinja/)和 [Python 教程](/lang/python/),或列出[所有 Python 教程](/all/#python)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看