Hướng dẫn python email html template - mẫu html email python

Đây là mã mẫu. Điều này được lấy cảm hứng từ mã được tìm thấy trên trang web Cookbook Python (không thể tìm thấy liên kết chính xác)

Nội dung chính ShowShow

  • Tổng quan về việc gửi email bằng Python
  • Thiết lập tài khoản Gmail mới của bạn
  • Gửi email văn bản đơn giản
  • Gửi email đầu tiên của bạn với kết nối máy chủ SMTP an toàn
  • Thêm nhiều người nhận, chủ đề, vào
  • Gửi HTML với email đính kèm
  • Gửi nội dung HTML với mô -đun SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials - gsmtp')3
  • Thêm tệp đính kèm
  • Thêm hình ảnh vào cơ thể email
  • Làm thế nào để bạn gửi email HTML trong Python?
  • Làm cách nào để gửi nội dung HTML trong email?
  • Làm thế nào để bạn nhúng trực quan hóa dữ liệu của bạn trong các email HTML và gửi chúng bằng Python?
  • Làm thế nào để bạn tải một tệp HTML trong Python?

def createhtmlmail (html, text, subject, fromEmail):
    """Create a mime-message that will render HTML in popular
    MUAs, text in better ones"""
    import MimeWriter
    import mimetools
    import cStringIO

    out = cStringIO.StringIO() # output buffer for our message 
    htmlin = cStringIO.StringIO(html)
    txtin = cStringIO.StringIO(text)

    writer = MimeWriter.MimeWriter(out)
    #
    # set up some basic headers... we put subject here
    # because smtplib.sendmail expects it to be in the
    # message body
    #
    writer.addheader("From", fromEmail)
    writer.addheader("Subject", subject)
    writer.addheader("MIME-Version", "1.0")
    #
    # start the multipart section of the message
    # multipart/alternative seems to work better
    # on some MUAs than multipart/mixed
    #
    writer.startmultipartbody("alternative")
    writer.flushheaders()
    #
    # the plain text section
    #
    subpart = writer.nextpart()
    subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
    pout = subpart.startbody("text/plain", [("charset", 'us-ascii')])
    mimetools.encode(txtin, pout, 'quoted-printable')
    txtin.close()
    #
    # start the html subpart of the message
    #
    subpart = writer.nextpart()
    subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
    #
    # returns us a file-ish object we can write to
    #
    pout = subpart.startbody("text/html", [("charset", 'us-ascii')])
    mimetools.encode(htmlin, pout, 'quoted-printable')
    htmlin.close()
    #
    # Now that we're done, close our writer and
    # return the message body
    #
    writer.lastpart()
    msg = out.getvalue()
    out.close()
    print msg
    return msg

if __name__=="__main__":
    import smtplib
    html = 'html version'
    text = 'TEST VERSION'
    subject = "BACKUP REPORT"
    message = createhtmlmail(html, text, subject, 'From Host <>')
    server = smtplib.SMTP("smtp_server_address","smtp_port")
    server.login('username', 'password')
    server.sendmail('', '', message)
    server.quit()

Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách gửi email bằng Python. send emails using Python. send emails using Python.

Chúng tôi có thể dễ dàng tự động hóa quá trình gửi email bằng Python. Nó rất tiện dụng khi bạn gửi nội dung tương tự nhiều lần. Tự động hóa này có thể làm giảm lỗi của con người và cũng giải phóng thời gian của bạn! Ví dụ: nếu bạn cần cập nhật danh sách các liên hệ dựa trên dữ liệu hàng ngày mới, bạn có thể sử dụng Python để tạo báo cáo mới và chia sẻ nó với họ bằng cách gửi email. Hoặc, nếu bạn có một chương trình Python mất nhiều thời gian để chạy, bạn có thể yêu cầu Python gửi cho bạn một email nhắc nhở khi nó hoàn thành.For example, if you need to update a list of contacts based on new daily data, you can use Python to create a new report and share it with them by sending emails. Or, if you have a Python program that takes a long time to run, you can ask Python to send you a reminder email when it’s done.
For example, if you need to update a list of contacts based on new daily data, you can use Python to create a new report and share it with them by sending emails. Or, if you have a Python program that takes a long time to run, you can ask Python to send you a reminder email when it’s done.

Theo hướng dẫn này, bạn sẽ học với các ví dụ:

  • Cách kết nối an toàn với máy chủ SMTP email của bạn
  • Cách gửi email văn bản đơn giản (với chủ đề, đến, nhiều người nhận)Plain Text emails (with Subject, To, multiple recipients)Plain Text emails (with Subject, To, multiple recipients)
  • Cách gửi email nội dung HTML ưa thích (có tệp đính kèm / hình ảnh)HTML content emails (with attachments / images)HTML content emails (with attachments / images)
  • Và nhiều hơn nữa!

Chúng tôi sẽ chỉ cho bạn cách sử dụng các mô -đun Python, bao gồm

SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
8 và
SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
9, với nhiều ví dụ.

Nếu bạn muốn gửi email bằng Python, hướng dẫn này sẽ đưa bạn đi một chặng đường dài!

Học thêm: Để tạo các báo cáo sử dụng Python làm Excel, HTML, PDF, hãy xem hướng dẫn của chúng tôi cách tạo báo cáo với Python.: to generate reports using Python as Excel, HTML, PDF, check out our tutorial How to generate Reports with Python.: to generate reports using Python as Excel, HTML, PDF, check out our tutorial How to generate Reports with Python.

Hãy để lặn xuống!


Để làm theo hướng dẫn này, bạn cần biết:

  • Python Basics, mà bạn có thể học với khóa học về sự cố miễn phí & NBSP của chúng tôi: đột nhập vào khoa học dữ liệu., which you can learn with our FREE Python crash course: breaking into Data Science., which you can learn with our FREE Python crash course: breaking into Data Science.
  • Cơ bản về HTML, mà bạn có thể nhận được một cái nhìn tổng quan nhanh chóng với & nbsp; html & nbsp; Giới thiệu từ các trường W3., which you can get a quick overview with HTML Introduction from W3 Schools., which you can get a quick overview with HTML Introduction from W3 Schools.

Mục lục

  1. Tổng quan về việc gửi email bằng Python
  2. Thiết lập tài khoản Gmail mới của bạn
  3. Gửi email văn bản đơn giản
    • Gửi email đầu tiên của bạn với kết nối máy chủ SMTP an toàn
    • Thêm nhiều người nhận, chủ đề, vào
  4. Gửi HTML với email đính kèm
    • Gửi nội dung HTML với mô -đun SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials - gsmtp')3
    • Thêm tệp đính kèm
    • Thêm hình ảnh vào cơ thể email

Tổng quan về việc gửi email bằng Python

Thiết lập tài khoản Gmail mới của bạn

Gửi email văn bản đơn giản

  • Gửi email đầu tiên của bạn với kết nối máy chủ SMTP an toàn
    • Thêm nhiều người nhận, chủ đề, vào
      What is SMTP? It is an internet standard communication protocol for electronic mail transmission.
      In short, this module
      SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
      8 enables Python to use such a protocol to send emails reliably and efficiently. So it’s a foundation library for sending emails using Python.
    • Gửi HTML với email đính kèm
      In short, such a protocol makes communications over the computer network more secure.
  • Gửi nội dung HTML với mô -đun SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials - gsmtp')3
    • Thêm tệp đính kèm
    • Thêm hình ảnh vào cơ thể email
    • Làm thế nào để bạn gửi email HTML trong Python?
      This library makes managing email messages easier. We don’t have to know all the details behind the scenes when constructing complicated emails.
      For example, we’ll use the
      SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
      3 module to add HTML, and attach documents to the email. You can learn more about the Multipurpose Internet Mail Extensions (MIME) standard here.

Làm cách nào để gửi nội dung HTML trong email?

Đến thời điểm này, bạn có thể bị choáng ngợp với tất cả các thuật ngữ mới này. Nhưng đừng lo lắng, nó sẽ rõ ràng hơn với các ví dụ.

Một điều nữa trước khi lập trình, chúng tôi khuyên bạn nên tạo một tài khoản email mới cho mục đích kiểm tra.

Thiết lập tài khoản Gmail mới của bạn

Đối với hướng dẫn này, chúng tôi đã tạo một tài khoản Gmail mới. Có một vài lý do cho điều đó. Ví dụ, chúng tôi không muốn tiết lộ thông tin đăng nhập của mình một cách vô tình. Ngoài ra, cách dễ nhất để cho phép truy cập từ Python vào tài khoản Gmail của bạn là sửa đổi cài đặt bảo mật của nó.

Vì vậy, thật tốt khi có một tài khoản mới để kiểm tra gửi email bằng Python, trước khi áp dụng nó bằng tài khoản hiện tại của bạn. Sau giai đoạn phát triển, nếu bạn không muốn giảm cài đặt bảo mật của tài khoản Gmail hiện tại của mình, bạn có thể tìm kiếm một giải pháp trong phần API Gmail. Nhưng bây giờ, hãy để Lôi tạo một tài khoản Gmail mới để giữ cho mọi thứ đơn giản.

Dưới đây là 2 bước quy trình để thiết lập tài khoản Gmail thử nghiệm của bạn:

  • Tạo tài khoản Gmail
  • Cho phép các ứng dụng ít an toàn hơn: Bật công tắc để cung cấp quyền truy cập Python vào tài khoản của bạn, hãy đảm bảo bạn đang thay đổi cài đặt của tài khoản mới nếu bạn không thay đổi cài đặt này, khi chạy mã sau, bạn sẽ nhận được thông báo lỗi như bên dưới :Please make sure you are changing the setting of the new accountIf you don’t change this setting, when running the code later, you’ll get an error message like below:
    Please make sure you are changing the setting of the new account
    If you don’t change this setting, when running the code later, you’ll get an error message like below:
SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')

Cập nhật: Từ ngày 30 tháng 5 năm 2022, cài đặt truy cập ứng dụng ít an toàn hơn không còn có sẵn cho Gmail. Nhưng nó có thể có sẵn cho các nhà cung cấp dịch vụ thư khác. Để khắc phục các vấn đề với hướng dẫn của chúng tôi trên Gmail, vui lòng thiết lập mật khẩu ứng dụng. Các bước chung được chuyển đến tài khoản Google của bạn -> Bảo mật -> Bật xác minh 2 bước -> Bảo mật -> Thiết lập mật khẩu ứng dụng. Để biết thêm chi tiết, vui lòng truy cập tại đây. Sau đó, bạn có thể sử dụng mật khẩu như vậy để gửi email trong Python.: from May 30, 2022, ​​the Less secure app access setting is no longer available for Gmail. But it might be available for other mail service providers. To fix the problems with our tutorial on Gmail, please set up an App Password. The general steps are go to your Google Account -> Security -> turn on 2-Step Verification -> Security -> set up an App password. For more details, please visit here.Then, you can use such a password to send emails in Python.: from May 30, 2022, ​​the Less secure app access setting is no longer available for Gmail. But it might be available for other mail service providers.
To fix the problems with our tutorial on Gmail, please set up an App Password. The general steps are go to your Google Account -> Security -> turn on 2-Step Verification -> Security -> set up an App password. For more details, please visit here.
Then, you can use such a password to send emails in Python.

Lưu ý: Mặc dù chúng tôi sử dụng Gmail trong các ví dụ của chúng tôi, bạn có thể làm theo các quy trình tương tự bằng cách sử dụng các nhà cung cấp dịch vụ thư khác. Nếu bạn đang làm việc trong một công ty lớn, bạn có thể không có quyền truy cập vào máy chủ SMTP của mình. Trong trường hợp đó, bạn cần liên hệ với nhóm CNTT của bạn để được hỗ trợ.: Even though we use Gmail in our examples, you can follow similar procedures using other mail service providers. If you are working in a large corporation, you might not have access to your SMTP server. In that case, you need to contact your IT team for support.: Even though we use Gmail in our examples, you can follow similar procedures using other mail service providers.
If you are working in a large corporation, you might not have access to your SMTP server. In that case, you need to contact your IT team for support.

Bây giờ bạn nên thiết lập tài khoản kiểm tra gmail của bạn. Chúng tôi đã sẵn sàng để gửi email bằng Python!

Gửi email văn bản đơn giản

Chúng tôi sẽ bắt đầu từ email cơ bản nhất, chỉ có văn bản đơn giản. Xin đừng bỏ qua phần này, vì chúng tôi cần kiến ​​thức cơ bản này trong các phần sau.

Gửi email đầu tiên của bạn với kết nối máy chủ SMTP an toàn

Để gửi email bằng Python, chúng tôi cần kết nối an toàn với máy chủ SMTP của Gmail và gửi tin nhắn email qua nó.

Dưới đây là các chi tiết của thủ tục này. Như đã đề cập trong tổng quan, chúng tôi sử dụng hai mô -đun Python:

SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
8 và

SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
0.
  • Tạo một cài đặt mặc định an toàn bối cảnh sử dụng chức năng
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    6 để trả về một đối tượng SSLContext mới với cài đặt bảo mật mặc định. Điều này đảm bảo giao tiếp của chúng tôi với máy chủ hộp thư được mã hóa và bảo mật.Use the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    6 function to return a new SSLContext object with default security settings. This ensures our communication with the mailbox server is encrypted and secure.

    Use the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    6 function to return a new SSLContext object with default security settings. This ensures our communication with the mailbox server is encrypted and secure.
  • Kết nối với máy chủ thư gửi Gmail, SMTP với bối cảnh như vậy, chức năng
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    7 bạn có thể tìm hiểu về máy chủ GMAIL tựa SMTP (
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    8) và cổng (
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    9) tại đây. Nếu bạn đang sử dụng một dịch vụ email khác, vui lòng tìm kiếm và chuyển sang thông tin của họ.Use the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    7 function You can find out about Gmail’s SMTP server (
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    8) and port (
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    9) here. If you are using a different email service, please look up and switch to their information.

    Use the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    7 function
    You can find out about Gmail’s SMTP server (
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    8) and port (
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    9) here. If you are using a different email service, please look up and switch to their information.
  • Với kết nối như vậy, chúng tôi cung cấp thông tin đăng nhập Gmail, và gửi thư với thông báo dưới dạng chuỗi Python (văn bản thuần túy), chúng tôi sử dụng câu lệnh
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    80 để kết nối, để kết nối tự động thoát khi câu lệnh
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    80 thoát ra., we provide Gmail’s login information, and send a mail with the message as a Python string (plain text)We use the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    80 statement to connect, so that the connection automatically quits when the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    80 statement exits.
    , we provide Gmail’s login information, and send a mail with the message as a Python string (plain text)
    We use the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    80 statement to connect, so that the connection automatically quits when the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    80 statement exits.

Kết hợp mọi thứ, bên dưới là tập lệnh gửi email văn bản đơn giản cơ bản bằng Python. Vui lòng đảm bảo xóa mật khẩu của bạn nếu bạn đang chia sẻ mã với người khác.Please make sure to remove your password if you are sharing the code with others.Please make sure to remove your password if you are sharing the code with others.

Sau khi chạy nó thành công, bạn sẽ nhận được một thư trong hộp thư của người nhận như bên dưới.

Chúc mừng! Bạn đã gửi email đầu tiên của mình bằng Python.

Nếu bạn nhấp vào mũi tên nhỏ để mở rộng các chi tiết của email, bạn có thể thấy rằng bảo mật cho biết mã hóa tiêu chuẩn (TLS). Điều này xác nhận rằng chúng tôi đã gửi tin nhắn thông qua kết nối an toàn.

Thêm nhiều người nhận, chủ đề, vào

Như bạn có thể nhận thấy, email đầu tiên mà chúng tôi đã gửi không có chủ đề. Ngoài ra, trường đến thường hiển thị địa chỉ email của người nhận trống. Chúng tôi có thể thêm cả hai bằng cách thay đổi tin nhắn email. Ngoài ra, nếu chúng ta muốn gửi đến nhiều địa chỉ email? Hãy để thêm chúng.

Dưới đây là mã được sửa đổi dựa trên ví dụ trước. Những thay đổi mà chúng tôi đã thực hiện là:

  • Thêm nhiều người nhận làm Listpython sẽ gửi email đến tất cả các địa chỉ email được liệt kê. Nhưng Python sẽ không tự động hiển thị các địa chỉ này trong trường TO.multiple recipients as a listPython will send emails to all the email addresses listed. But Python will not automatically show these addresses in the To field.multiple recipients as a list
    Python will send emails to all the email addresses listed. But Python will not automatically show these addresses in the To field.
  • Thêm chủ đề và vào các trường Chúng tôi phải có chủ đề, và thông báo email trên các dòng riêng biệt. Vì vậy, vui lòng xác định chuỗi F là một chuỗi đa dòng với trích dẫn ba (
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    82Again, vì Python không tự động hiển thị các email của người nhận trong trường TO, chúng tôi phải in chúng theo cách thủ công. Mã
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    83 tham gia tất cả các email người nhận được lưu trữ trong
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    84 Chuỗi dấu phẩy (
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    85 là dấu phân cách. Vì vậy, nó trả về một chuỗi
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    86, sẽ được in dưới dạng trường trong email.Subject and To fieldsWe must have the Subject, To, and email message on separate lines. So please define the f-string as a multiline string with triple quotes (
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    82Again, since Python does not automatically show the recipient emails in the To field, we must print them manually. The code
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    83 joins all the recipient emails stored in
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    84, using the string of comma (
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    85 as the separator. So it returns a string of
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    86, which will be printed as the To field in the email.Subject and To fields
    We must have the Subject, To, and email message on separate lines. So please define the f-string as a multiline string with triple quotes (
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    82
    Again, since Python does not automatically show the recipient emails in the To field, we must print them manually. The code
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    83 joins all the recipient emails stored in
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    84, using the string of comma (
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    85 as the separator. So it returns a string of
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    86, which will be printed as the To field in the email.

Sau khi chạy thành công, bạn sẽ thấy một email như bên dưới, trong tất cả các hộp thư của người nhận. Và bạn sẽ xem email với chủ đề là một email văn bản đơn giản, và đến trường dưới dạng địa chỉ người nhận.

Gửi HTML với email đính kèm

Mặc dù email văn bản thuần túy rất tốt để chia sẻ một số thông tin, chúng tôi thường muốn thêm nhiều hơn vào email. Ví dụ: chúng tôi có thể muốn định dạng văn bản với HTML để người nhận dễ dàng đọc hoặc chúng tôi có thể muốn thêm nhiều nội dung vào email, chẳng hạn như tài liệu PDF.

Để bao gồm nội dung và tệp đính kèm HTML, chúng tôi sẽ sử dụng một mô -đun Python khác có tên là

SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
9. Cụ thể hơn, chúng tôi sẽ sử dụng mô -đun ________ 3 .________ 29 để xây dựng các email lạ mắt hơn này: các email tiêu chuẩn mở rộng thư (MIME) đa năng này.

Quy trình chung của việc tạo email MIME trong các ví dụ của chúng tôi là:

  1. Tạo một lớp
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    90
  2. Đính kèm các lớp
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    91 (ví dụ:
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    92 với tin nhắn email,
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    93 với tệp đính kèm)

Hãy cùng khám phá một số ví dụ dưới đây.

Gửi nội dung HTML với mô -đun SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials - gsmtp')3

HTML là ngôn ngữ đánh dấu tiêu chuẩn cho các trang web. Vì vậy, có rất nhiều nội dung và định dạng chúng tôi có thể làm với HTML. Ví dụ, chúng ta có thể làm văn bản thành tiêu đề, đoạn văn, in đậm hoặc in nghiêng; Chúng tôi cũng có thể bao gồm các phần tử HTML khác như bảng dữ liệu, hình ảnh, v.v. với HTML là thông điệp email, chúng tôi có thể hiển thị nội dung giàu thông tin và có cấu trúc tốt.bold, or italic; we can also include other HTML elements such as data tables, images, etc. With HTML as the email message, we can display information-rich and well-structured content.bold, or italic; we can also include other HTML elements such as data tables, images, etc. With HTML as the email message, we can display information-rich and well-structured content.

Dưới đây là một ví dụ về email nội dung HTML cơ bản:

  • Nhập các mô -đunbesides
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    8 và
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    0, chúng tôi cũng cần một số chức năng để tạo các lớp con MIME từ
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    3. Bạn có thể nghiên cứu các lớp MIME khác nhau ở đây. Chúng tôi cũng nhập thư viện
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    98 vì chúng tôi muốn đưa ngày hiện tại vào chủ đề, đó là tùy chọn để gửi email.Besides
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    8 and
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    0, we also need some functions to create MIME subclasses from
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    3. You can research different MIME classes here.We also import the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    98 library since we want to include the current date in the Subject, which is optional for sending emails.

    Besides
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    8 and
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    0, we also need some functions to create MIME subclasses from
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    3. You can research different MIME classes here.
    We also import the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    98 library since we want to include the current date in the Subject, which is optional for sending emails.
  • Xác định tài liệu HTML chỉ bao gồm một tài liệu đơn giản với phần tử
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    99 và
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    60. Nhưng bạn có thể bao gồm các yếu tố khác như
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    61. Nếu bạn đến từ cách tạo báo cáo với hướng dẫn Python, bạn có thể cố gắng hiển thị báo cáo
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    62 mà chúng tôi đã tạo, ngoại trừ phần tử
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    63. Yếu tố
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    63 cần thêm một số công việc để kết xuất, sẽ sớm được hiển thị.We only include a simple doc with an
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    99 and a
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    60 element. But you can include other elements such as
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    61. If you are coming from How to generate Reports with Python tutorial, you can try to render the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    62 report we’ve generated, except for the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    63 element. The
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    63 element needs some extra work to render, which will be shown soon.

    We only include a simple doc with an
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    99 and a
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    60 element. But you can include other elements such as
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    61.
    If you are coming from How to generate Reports with Python tutorial, you can try to render the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    62 report we’ve generated, except for the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    63 element. The
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    63 element needs some extra work to render, which will be shown soon.
  • Thiết lập địa chỉ email và mật khẩu xin lưu ý rằng bạn cũng có thể bao gồm nhiều người nhận làm ví dụ văn bản đơn giản.Please note that you can also include multiple recipients as the plain text example.
    Please note that you can also include multiple recipients as the plain text example.
  • Tạo một lớp
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    90 và thiết lập các trường từ, đến, chủ đề mà mô -đun
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    3 giúp dễ dàng thiết lập các trường từ, đến, chủ đề. Chúng tôi không phải đặt từng trường trên một dòng riêng trong chuỗi tin nhắn. Thay vào đó, chúng tôi xác định chúng một cách riêng biệt một cách rõ ràng hơn.Note that the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    3 module makes it easier to set up the From, To, Subject fields. We don’t have to put each field on a separate line in the message string. Instead, we define them separately in a more clear way.

    Note that the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    3 module makes it easier to set up the From, To, Subject fields. We don’t have to put each field on a separate line in the message string. Instead, we define them separately in a more clear way.
  • Đính kèm
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    62Doc được xác định trước đó, dưới dạng loại nội dung
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    92HTML vào MIME Messagenote rằng loại nội dung mặc định là ‘đơn giản, đã giành được tài liệu HTML.doc defined earlier, as a
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    92html content type to the MIME messageNote that the default content type is ‘plain’, which won’t render the HTML document properly.
    doc defined earlier, as a
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    92
    html content type to the MIME message
    Note that the default content type is ‘plain’, which won’t render the HTML document properly.
  • Chuyển đổi
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    69 dưới dạng chuỗi
  • Kết nối với máy chủ Gmail SMTP và gửi email, giống như trước đây, which is the same as before, which is the same as before

Cuối cùng, bạn sẽ nhận được một email như dưới đây. Như bạn có thể thấy, văn bản được định dạng tốt hơn.

Bây giờ bạn đã học cách gửi email HTML bằng Python, bạn cũng có thể sửa đổi tài liệu HTML để bao gồm các yếu tố khác như

SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
61!

Thêm tệp đính kèm

Bên cạnh nội dung HTML cơ bản, chúng tôi cũng thường muốn bao gồm các tệp đính kèm vào email.

Dựa trên ví dụ trước, bạn có thể sử dụng mã dưới đây để bao gồm các tệp đính kèm vào email:

  • Thêm một lớp con MIME mới để thêm tệp đính kèm có thể sử dụng lớp
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    93 để giữ các tệp đính kèm.We can use the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    93 class to hold attachments.

    We can use the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    93 class to hold attachments.
  • Xác định một chức năng để đính kèm các tệp là
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    93 Để email, việc thêm tệp đính kèm là phức tạp, tốt hơn là viết nó như một chức năng để sử dụng lại. Vui lòng xem chi tiết trong các bình luận.to the emailSince adding attachments is complicated, it’s better to write it as a function for reuse.Please see the details in the comments.
    to the email
    Since adding attachments is complicated, it’s better to write it as a function for reuse.
    Please see the details in the comments.
  • Đính kèm thêm (tài liệu) vào email, bằng cách áp dụng chức năng chúng tôi đính kèm ba tài liệu: 1 png, 1 excel, 1 pdf. Bạn có thể cố gắng đính kèm các tập tin của bạn. Vui lòng đảm bảo rằng chúng được lưu trong thư mục làm việc hiện tại, nếu bạn không muốn chỉ định đường dẫn. Nếu bạn đến từ cách tạo báo cáo với Python Hạt hướng, giờ đây bạn có thể đính kèm biểu đồ, Excel và PDF báo cáo chúng tôi ' ve tạo ra., by applying the functionWe attach three documents: 1 PNG, 1 Excel, 1 PDF. You can try to attach your files. Please make sure they are saved under the current working directory, if you don’t want to specify the path.If you are coming from How to generate Reports with Python tutorial, you can now attach the chart, Excel, and PDF reports we’ve generated., by applying the function
    We attach three documents: 1 PNG, 1 Excel, 1 PDF.
    You can try to attach your files. Please make sure they are saved under the current working directory, if you don’t want to specify the path.
    If you are coming from How to generate Reports with Python tutorial, you can now attach the chart, Excel, and PDF reports we’ve generated.

Nếu bạn chạy mã, bạn sẽ có thể nhận được một email như bên dưới, với các tệp đính kèm mà bạn chọn.

Thêm hình ảnh vào cơ thể email

Bây giờ, điều gì sẽ xảy ra nếu chúng ta muốn đưa biểu đồ vào tin nhắn email của chúng ta? Chúng ta cần bao gồm nó như một yếu tố

SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
63 trong nội dung HTML. Nhưng email được gửi bằng Python không hiển thị hình ảnh mà không cần xử lý thêm. Vì vậy, như một ví dụ cuối cùng trong hướng dẫn này, chúng tôi sẽ hiển thị một hình ảnh trong tin nhắn email HTML.

Dựa trên ví dụ trước, đây là những thay đổi bao gồm hình ảnh trong HTML:

  • Thêm phần tử
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    63 với
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    85 dưới dạng CID (ID nội dung) là MyImageIdyou có thể đặt CID thành các giá trị khác nếu bạn thích. with
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    85 as cid (Content ID) being myimageidYou can set the cid to other values if you’d like.
    with
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    85 as cid (Content ID) being myimageid
    You can set the cid to other values if you’d like.
  • Trong hàm để thêm các tệp đính kèm, thêm một đầu vào khác
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    86 này đầu vào có giá trị mặc định là không Giá trị, nó sẽ thêm giá trị của ____ 57 này làm tiêu đề cho đối tượng, có thể được sử dụng để đặt CID cho
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    63This input has a default value of None: – If it’s None, the function attaches the file as the previous example – If this input is not None, i.e., when we set it to a specific value, it will add this
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    87‘s value as header to the object, which can be used to set the cid for
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    63

    This input has a default value of None:
    – If it’s None, the function attaches the file as the previous example
    – If this input is not None, i.e., when we set it to a specific value, it will add this
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    87‘s value as header to the object, which can be used to set the cid for
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    63
  • Áp dụng chức năng với
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    87 trên
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    50THE ID nội dung của MyImageID phù hợp với phần tử
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    63 trong
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    62. Điều này hiển thị
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    50 trên nội dung HTML.The Content-ID of myimageid matches with the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    63 element in
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    62. This renders the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    50 on the HTML content.

    The Content-ID of myimageid matches with the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    63 element in
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    62. This renders the
    SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
    50 on the HTML content.

Nếu bạn chạy mã ngay bây giờ, bạn sẽ nhận được một email như bên dưới.

SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials - gsmtp')
50 hoặc biểu đồ bạn chọn được hiển thị trong nội dung HTML, thay vì được hiển thị dưới dạng tệp đính kèm.

Điều đó thật tuyệt!


Trong hướng dẫn này, bạn đã học cách gửi email bằng Python, từ các email văn bản đơn giản cơ bản nhất, đến các email HTML lạ mắt hơn với các tệp đính kèm.

Hy vọng bây giờ bạn có thể tự động hóa quá trình gửi email qua Python!

Bên cạnh các mô -đun Python nền tảng này, còn có các thư viện hoặc sản phẩm khác giúp gửi email bằng Python. Ví dụ: bạn có thể kiểm tra:

  • Yagmail: Mô -đun này đặc biệt cho Gmail, giúp đơn giản hóa quá trình rất nhiều. Nhưng bạn phải đảm bảo rằng gói được cập nhật dựa trên các mô -đun nền tảng và gmail
  • SendGrid: Dịch vụ này có một tầng miễn phí. Nhưng vì nó là một dịch vụ được quản lý, bạn phải tin tưởng nó.
  • Mailgun: Dịch vụ này cung cấp thử nghiệm miễn phí 3 tháng. Nhưng vì nó là một dịch vụ được quản lý, bạn phải tin tưởng nó.

Học thêm: Để tìm hiểu về việc tạo báo cáo với Python, hãy xem cách tạo báo cáo với Python (3 định dạng/4 công cụ). Để bạn có thể tạo báo cáo, gửi email để chia sẻ các báo cáo, cả hai đều tự động bằng Python!: to learn about generating reports with Python, check out How to generate Reports with Python (3 Formats/4 Tools). So that you can generate reports, sending emails to share the reports, both automatically in Python!: to learn about generating reports with Python, check out How to generate Reports with Python (3 Formats/4 Tools). So that you can generate reports, sending emails to share the reports, both automatically in Python!

Chúng tôi rất thích nghe từ bạn. Để lại một bình luận cho bất kỳ câu hỏi bạn có thể có hoặc bất cứ điều gì khác.

Làm thế nào để bạn gửi email HTML trong Python?

Gửi nội dung HTML với email ....

Nhập các mô -đun. ....

Xác định tài liệu HTML. ....

Thiết lập địa chỉ email và mật khẩu. ....

Tạo một lớp Mimemultipart, và thiết lập các trường từ, đến, chủ đề. ....

Gắn tài liệu HTML được xác định trước đó, dưới dạng loại nội dung HTML mimetext vào thông báo MIME. ....

Chuyển đổi email_message dưới dạng chuỗi ..

Làm cách nào để gửi nội dung HTML trong email?

Hãy chèn HTML vào Gmail...

Lấy mã mà bạn đã lưu dưới dạng tệp HTML và mở nó trong trình duyệt của bạn.....

Từ đó, bạn chỉ cần sao chép HTML vì nó đã hiển thị trong trình duyệt ..

Dán nó vào cửa sổ Compose Gmail mới của bạn ..

Nhấn Gửi, và sau đó bạn đã hoàn thành tất cả ..

Làm thế nào để bạn nhúng trực quan hóa dữ liệu của bạn trong các email HTML và gửi chúng bằng Python?

Cách nhúng trực quan hóa dữ liệu của bạn vào email HTML và gửi chúng bằng Python...

Tạo cốt truyện.....

Xác định tin nhắn của bạn và gửi thư.....

Mẫu 6 bước để thực sự vượt qua cuộc phỏng vấn kỹ thuật FAANG tiếp theo của bạn.....

8 Phải có các tiện ích mở rộng Google Chrome tiết kiệm giờ làm việc thành vài phút ..

Làm thế nào để bạn tải một tệp HTML trong Python?

Approach:...

Nhập mô -đun ..

Mở và tạo tệp ..

Thêm mã HTML ..

Ghi mã vào tệp ..

Đóng tập tin..

Mở tệp trong cửa sổ trình duyệt ..