Hướng dẫn python send anonymous email - python gửi email ẩn danh

Xin chào các tin tặc mũ xám đồng nghiệp, vì tin tặc cần thiết để chúng tôi có thể gửi email một cách ẩn danh. Cho dù để thổi còi hay chơi khăm hoặc kỹ thuật xã hội hay bất cứ điều gì, nó chỉ là cần thiết để chúng ta làm điều đó.

Vì vậy, sau đó tôi đã thử với Python Smtplib nhưng sau đó yêu cầu bạn đăng nhập và đó chỉ là tin xấu đặc biệt, nếu bạn có một loại keylogger gửi email cho bạn. Ý tôi là nếu người đó tìm thấy nó, bạn đã đụ ... lol

Vì vậy, tôi gần như đã bỏ cuộc cho đến khi ... Tôi đã nghe về email ẩn danh của Anonymouse

Thật khó chịu khi luôn mở trình duyệt web của bạn và phải đi đến url đó, ý tôi là không ai muốn làm điều đó .... cộng với nếu tôi làm cho nó trang chủ của mình hoặc tôi đánh dấu nó và anh trai tôi muốn sử dụng máy tính của tôi, anh ấy Sẽ thấy điều đó thật kỳ lạ và tôi sẽ không giải thích cho mẹ tôi tại sao tôi muốn gửi email nặc danh ... Tôi quyết định viết một kịch bản làm điều đó và làm điều đó nhanh chóng ... sau nhiều nghiên cứu và gỡ lỗi cuối cùng tôi đã hiểu đúng, Vì vậy, bạn đi đây ...

Mật mã

Hướng dẫn python send anonymous email - python gửi email ẩn danh

Hình ảnh qua imgur.com

Đó là mã, rất đẹp..let's giải thích nó

  1. Nhập cơ khí hóa (một mô -đun hoạt động như trình duyệt)
  2. Nhận thông tin về email từ người dùng
  3. Bao gồm các tiêu đề (tác nhân người dùng) để nó trông hợp pháp.
  4. Thêm một số cấu hình để ngăn chặn sự nghi ngờ và những thứ khác
  5. Chọn biểu mẫu đầu tiên trên trang bạn có thể sử dụng tên biểu mẫu..nhưng tôi chưa tìm hiểu điều đó và tôi không thực sự thích điều đó ... xin lỗi: D
  6. Dữ liệu đầu vào vào biểu mẫu
  7. Gửi biểu mẫu và nhận được phản hồi
  8. Nói với người dùng xem thông tin có được gửi hay không.

(Ngoài ra còn có một tùy chọn để sử dụng proxy nhưng tôi đã không sử dụng nó, đó là lý do tại sao tôi nhận xét nó!)

Demo!

Bây giờ cho phần thú vị, tôi sẽ gửi email với nó. Và các bạn có thể kiểm tra email đó !! Nó khá tuyệt !!

Hướng dẫn python send anonymous email - python gửi email ẩn danh

Hình ảnh qua imgur.com

Đó là mã, rất đẹp..let's giải thích nó

Hướng dẫn python send anonymous email - python gửi email ẩn danh

Nhập cơ khí hóa (một mô -đun hoạt động như trình duyệt)

Nhận thông tin về email từ người dùng
You can check out the message here later, just to be sure or if you don't believe me...lol
http://mailinator.com/inbox.jsp?to=nullbyte

Bao gồm các tiêu đề (tác nhân người dùng) để nó trông hợp pháp.

Thêm một số cấu hình để ngăn chặn sự nghi ngờ và những thứ khác

Chọn biểu mẫu đầu tiên trên trang bạn có thể sử dụng tên biểu mẫu..nhưng tôi chưa tìm hiểu điều đó và tôi không thực sự thích điều đó ... xin lỗi: D

http://pastebin.com/p3i6h59A

Dữ liệu đầu vào vào biểu mẫu

Gửi biểu mẫu và nhận được phản hồi

Nói với người dùng xem thông tin có được gửi hay không. Jump-start your hacking career with our 2020 Premium Ethical Hacking Certification Training Bundle from the new Null Byte Shop and get over 60 hours of training from cybersecurity professionals.

(Ngoài ra còn có một tùy chọn để sử dụng proxy nhưng tôi đã không sử dụng nó, đó là lý do tại sao tôi nhận xét nó!)

Bây giờ cho phần thú vị, tôi sẽ gửi email với nó. Và các bạn có thể kiểm tra email đó !! Nó khá tuyệt !!

  • Xin lỗi ... nếu bạn nhận thấy trong mã, tôi đã thêm vào để in phản hồi, bạn có thể bỏ nó ra (thực tế là bạn nên Một phần của phản hồi ... nhưng đây là tin nhắn được gửi. Bây giờ xác nhận ..
  • Hình ảnh qua imgur.com
  • Đây là xác nhận..bạn có thể xem tin nhắn ở đây sau, chỉ để chắc chắn hoặc nếu bạn không tin tôi ... Lolhttp: //mailinator.com/inbox.jsp? To = nullbyte
  • Sự kết luận

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem cùng với hướng dẫn bằng văn bản để hiểu sâu hơn về sự hiểu biết của bạn: Gửi email với Python This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Sending Emails With Python

Bạn có thể tìm thấy hướng dẫn này vì bạn muốn gửi email bằng Python. Có lẽ bạn muốn nhận lời nhắc email từ mã của mình, gửi email xác nhận cho người dùng khi họ tạo tài khoản hoặc gửi email cho các thành viên trong tổ chức của bạn để nhắc nhở họ thanh toán phí của họ. Gửi email theo cách thủ công là một nhiệm vụ tốn thời gian và dễ bị lỗi, nhưng nó dễ dàng tự động hóa với Python.

Trong hướng dẫn này, bạn sẽ học cách::

  • Thiết lập kết nối an toàn bằng cách sử dụng

    ---------- MESSAGE FOLLOWS ----------
    b'X-Peer: ::1'
    b''
    b'From: '
    b'To: '
    b'Subject: a local test mail'
    b''
    b'Hello there, here is a test email'
    ------------ END MESSAGE ------------
    
    5 và
    ---------- MESSAGE FOLLOWS ----------
    b'X-Peer: ::1'
    b''
    b'From: '
    b'To: '
    b'Subject: a local test mail'
    b''
    b'Hello there, here is a test email'
    ------------ END MESSAGE ------------
    
    6secure connection using
    ---------- MESSAGE FOLLOWS ----------
    b'X-Peer: ::1'
    b''
    b'From: '
    b'To: '
    b'Subject: a local test mail'
    b''
    b'Hello there, here is a test email'
    ------------ END MESSAGE ------------
    
    5 and
    ---------- MESSAGE FOLLOWS ----------
    b'X-Peer: ::1'
    b''
    b'From: '
    b'To: '
    b'Subject: a local test mail'
    b''
    b'Hello there, here is a test email'
    ------------ END MESSAGE ------------
    
    6

  • Sử dụng thư viện

    ---------- MESSAGE FOLLOWS ----------
    b'X-Peer: ::1'
    b''
    b'From: '
    b'To: '
    b'Subject: a local test mail'
    b''
    b'Hello there, here is a test email'
    ------------ END MESSAGE ------------
    
    7 tích hợp của Python để gửi email cơ bảnbasic emails

  • Gửi email với nội dung và tệp đính kèm HTML bằng gói

    ---------- MESSAGE FOLLOWS ----------
    b'X-Peer: ::1'
    b''
    b'From: '
    b'To: '
    b'Subject: a local test mail'
    b''
    b'Hello there, here is a test email'
    ------------ END MESSAGE ------------
    
    8HTML content and attachments using the
    ---------- MESSAGE FOLLOWS ----------
    b'X-Peer: ::1'
    b''
    b'From: '
    b'To: '
    b'Subject: a local test mail'
    b''
    b'Hello there, here is a test email'
    ------------ END MESSAGE ------------
    
    8 package

  • Gửi nhiều email được cá nhân hóa bằng tệp CSV với dữ liệu liên hệpersonalized emails using a CSV file with contact data

  • Sử dụng gói Yagmail để gửi email qua tài khoản Gmail của bạn chỉ bằng một vài dòng mãYagmail package to send email through your Gmail account using only a few lines of code

Bạn sẽ tìm thấy một vài dịch vụ email giao dịch vào cuối hướng dẫn này, sẽ hữu ích khi bạn muốn gửi một số lượng lớn email.

Bắt đầu

Python đi kèm với mô-đun

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
7 tích hợp để gửi email bằng giao thức chuyển thư đơn giản (SMTP).
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
7 sử dụng giao thức RFC 821 cho SMTP. Các ví dụ trong hướng dẫn này sẽ sử dụng máy chủ GMAIL SMTP để gửi email, nhưng các nguyên tắc tương tự áp dụng cho các dịch vụ email khác. Mặc dù phần lớn các nhà cung cấp email sử dụng cùng các cổng kết nối như các cổng trong hướng dẫn này, bạn có thể chạy tìm kiếm Google nhanh để xác nhận của bạn.

Để bắt đầu với hướng dẫn này, hãy thiết lập một tài khoản Gmail để phát triển hoặc thiết lập máy chủ gỡ lỗi SMTP để loại bỏ email bạn gửi và in chúng vào dấu nhắc lệnh. Cả hai tùy chọn được đặt ra cho bạn dưới đây. Máy chủ gỡ lỗi SMTP cục bộ có thể hữu ích để khắc phục mọi vấn đề với chức năng email và đảm bảo các chức năng email của bạn không có lỗi trước khi gửi bất kỳ email nào.

Tùy chọn 1: Thiết lập tài khoản Gmail để phát triển

Nếu bạn quyết định sử dụng tài khoản Gmail để gửi email, tôi khuyên bạn nên thiết lập tài khoản vứt bỏ để phát triển mã của bạn. Điều này là do bạn sẽ phải điều chỉnh các cài đặt bảo mật của tài khoản Gmail của mình để cho phép truy cập từ mã Python của bạn và vì có một cơ hội bạn có thể vô tình hiển thị chi tiết đăng nhập của mình. Ngoài ra, tôi thấy rằng hộp thư đến tài khoản thử nghiệm của tôi nhanh chóng điền vào các email kiểm tra, đó là lý do đủ để thiết lập một tài khoản Gmail mới để phát triển.

Một tính năng hay của Gmail là bạn có thể sử dụng dấu hiệu

import smtplib, ssl

port = 465  # For SSL
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
    server.login("", password)
    # TODO: Send email here
1 để thêm bất kỳ công cụ sửa đổi nào vào địa chỉ email của bạn, ngay trước dấu hiệu
import smtplib, ssl

port = 465  # For SSL
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
    server.login("", password)
    # TODO: Send email here
2. Ví dụ: thư được gửi đến
import smtplib, ssl

port = 465  # For SSL
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
    server.login("", password)
    # TODO: Send email here
3 và
import smtplib, ssl

port = 465  # For SSL
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
    server.login("", password)
    # TODO: Send email here
3 sẽ đến
import smtplib, ssl

port = 465  # For SSL
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
    server.login("", password)
    # TODO: Send email here
5. Khi kiểm tra chức năng email, bạn có thể sử dụng điều này để mô phỏng nhiều địa chỉ mà tất cả đều chỉ vào cùng một hộp thư đến.

Để thiết lập địa chỉ Gmail để kiểm tra mã của bạn, hãy làm như sau:

  • Tạo một Tài khoản Google mới.
  • Biến cho phép các ứng dụng ít an toàn hơn. Xin lưu ý rằng điều này giúp người khác dễ dàng truy cập vào tài khoản của bạn hơn.

Nếu bạn không muốn giảm các cài đặt bảo mật của tài khoản Gmail của mình, hãy xem tài liệu Google Google về cách có được thông tin đăng nhập quyền truy cập cho tập lệnh Python của bạn, sử dụng Khung ủy quyền OAuth2.

Tùy chọn 2: Thiết lập máy chủ SMTP cục bộ

Bạn có thể kiểm tra chức năng email bằng cách chạy máy chủ gỡ lỗi SMTP cục bộ, sử dụng mô-đun

import smtplib, ssl

port = 465  # For SSL
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
    server.login("", password)
    # TODO: Send email here
6 được cài đặt sẵn với Python. Thay vì gửi email đến địa chỉ được chỉ định, nó loại bỏ chúng và in nội dung của chúng vào bảng điều khiển. Chạy một máy chủ gỡ lỗi cục bộ có nghĩa là nó không cần thiết phải xử lý mã hóa tin nhắn hoặc sử dụng thông tin đăng nhập để đăng nhập vào máy chủ email.

Bạn có thể khởi động máy chủ gỡ lỗi SMTP cục bộ bằng cách nhập thông tin sau trong dấu nhắc lệnh:

$ python -m smtpd -c DebuggingServer -n localhost:1025

Trên Linux, sử dụng cùng một lệnh trước

import smtplib, ssl

port = 465  # For SSL
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
    server.login("", password)
    # TODO: Send email here
7.

Bất kỳ email nào được gửi qua máy chủ này sẽ bị loại bỏ và hiển thị trong cửa sổ đầu cuối dưới dạng đối tượng

import smtplib, ssl

port = 465  # For SSL
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
    server.login("", password)
    # TODO: Send email here
8 cho mỗi dòng:

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------

Đối với phần còn lại của hướng dẫn, tôi sẽ giả sử bạn đang sử dụng tài khoản Gmail, nhưng nếu bạn đang sử dụng máy chủ gỡ lỗi cục bộ, hãy đảm bảo sử dụng

import smtplib, ssl

port = 465  # For SSL
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
    server.login("", password)
    # TODO: Send email here
9 làm máy chủ SMTP của bạn và sử dụng cổng 1025 thay vì cổng 465 hoặc 587 . Bên cạnh đó, bạn sẽ không cần sử dụng
import smtplib, ssl

smtp_server = "smtp.gmail.com"
port = 587  # For starttls
sender_email = ""
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

# Try to log in to server and send email
try:
    server = smtplib.SMTP(smtp_server,port)
    server.ehlo() # Can be omitted
    server.starttls(context=context) # Secure the connection
    server.ehlo() # Can be omitted
    server.login(sender_email, password)
    # TODO: Send email here
except Exception as e:
    # Print any error messages to stdout
    print(e)
finally:
    server.quit() 
0 hoặc mã hóa giao tiếp bằng SSL/TLS.

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

Trước khi chúng tôi đi sâu vào việc gửi email với nội dung và tệp đính kèm HTML, bạn sẽ học cách gửi email văn bản đơn giản bằng Python. Đây là những email mà bạn có thể viết trong một trình soạn thảo văn bản đơn giản. Không có thứ lạ mắt như định dạng văn bản hoặc siêu liên kết. Bạn sẽ học được điều đó một chút sau đó.

Bắt đầu kết nối SMTP an toàn

Khi bạn gửi email qua Python, bạn nên đảm bảo rằng kết nối SMTP của bạn được mã hóa, để thông tin đăng nhập và thông tin đăng nhập của bạn không dễ dàng truy cập bởi người khác. SSL (lớp ổ cắm an toàn) và TLS (bảo mật lớp vận chuyển) là hai giao thức có thể được sử dụng để mã hóa kết nối SMTP. Nó không cần thiết phải sử dụng một trong hai thứ này khi sử dụng máy chủ gỡ lỗi cục bộ.

Có hai cách để bắt đầu kết nối an toàn với máy chủ email của bạn:

  • Bắt đầu kết nối SMTP được bảo mật ngay từ đầu bằng cách sử dụng
    ---------- MESSAGE FOLLOWS ----------
    b'X-Peer: ::1'
    b''
    b'From: '
    b'To: '
    b'Subject: a local test mail'
    b''
    b'Hello there, here is a test email'
    ------------ END MESSAGE ------------
    
    5.
  • Bắt đầu kết nối SMTP không có bảo đảm sau đó có thể được mã hóa bằng
    ---------- MESSAGE FOLLOWS ----------
    b'X-Peer: ::1'
    b''
    b'From: '
    b'To: '
    b'Subject: a local test mail'
    b''
    b'Hello there, here is a test email'
    ------------ END MESSAGE ------------
    
    6.

Trong cả hai trường hợp, Gmail sẽ mã hóa email bằng TLS, vì đây là kế thừa an toàn hơn của SSL. Theo các cân nhắc bảo mật của Python, rất khuyến khích bạn sử dụng

import smtplib, ssl

smtp_server = "smtp.gmail.com"
port = 587  # For starttls
sender_email = ""
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

# Try to log in to server and send email
try:
    server = smtplib.SMTP(smtp_server,port)
    server.ehlo() # Can be omitted
    server.starttls(context=context) # Secure the connection
    server.ehlo() # Can be omitted
    server.login(sender_email, password)
    # TODO: Send email here
except Exception as e:
    # Print any error messages to stdout
    print(e)
finally:
    server.quit() 
3 từ mô -đun
import smtplib, ssl

smtp_server = "smtp.gmail.com"
port = 587  # For starttls
sender_email = ""
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

# Try to log in to server and send email
try:
    server = smtplib.SMTP(smtp_server,port)
    server.ehlo() # Can be omitted
    server.starttls(context=context) # Secure the connection
    server.ehlo() # Can be omitted
    server.login(sender_email, password)
    # TODO: Send email here
except Exception as e:
    # Print any error messages to stdout
    print(e)
finally:
    server.quit() 
4. Điều này sẽ tải các chứng chỉ CA đáng tin cậy của hệ thống, kích hoạt kiểm tra tên máy chủ và xác thực chứng chỉ và cố gắng chọn cài đặt giao thức và mật mã bảo mật hợp lý.

Nếu bạn muốn kiểm tra mã hóa cho một email trong hộp thư đến gmail của bạn, hãy truy cập thêm → hiển thị bản gốc để xem loại mã hóa được liệt kê trong tiêu đề nhận được.

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
7 là mô-đun tích hợp Python, để gửi email đến bất kỳ máy internet nào có trình nền nghe SMTP hoặc ESMTP.

Tôi sẽ chỉ cho bạn cách sử dụng

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
5 trước, vì nó khởi tạo một kết nối được bảo mật ngay từ đầu và ngắn gọn hơn một chút so với thay thế
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
6. Hãy nhớ rằng Gmail yêu cầu bạn kết nối với Cổng 465 nếu sử dụng
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
5 và Cổng 587 khi sử dụng
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
6.

Tùy chọn 1: Sử dụng
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
5

Ví dụ mã bên dưới tạo kết nối an toàn với máy chủ SMTP Gmail, sử dụng

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
5 của
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
7 để bắt đầu kết nối được mã hóa TLS. Bối cảnh mặc định của
import smtplib, ssl

smtp_server = "smtp.gmail.com"
port = 587  # For starttls
sender_email = ""
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

# Try to log in to server and send email
try:
    server = smtplib.SMTP(smtp_server,port)
    server.ehlo() # Can be omitted
    server.starttls(context=context) # Secure the connection
    server.ehlo() # Can be omitted
    server.login(sender_email, password)
    # TODO: Send email here
except Exception as e:
    # Print any error messages to stdout
    print(e)
finally:
    server.quit() 
4 xác thực tên máy chủ và chứng chỉ của nó và tối ưu hóa bảo mật của kết nối. Đảm bảo điền vào địa chỉ email của riêng bạn thay vì
import smtplib, ssl

port = 465  # For SSL
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
    server.login("", password)
    # TODO: Send email here
5:

import smtplib, ssl

port = 465  # For SSL
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
    server.login("", password)
    # TODO: Send email here

Sử dụng

server.sendmail(sender_email, receiver_email, message)
5 đảm bảo rằng kết nối được tự động đóng ở cuối khối mã thụt. Nếu
server.sendmail(sender_email, receiver_email, message)
6 bằng không, hoặc không được chỉ định,
server.sendmail(sender_email, receiver_email, message)
7 sẽ sử dụng cổng tiêu chuẩn cho SMTP qua SSL (cổng 465).

Nó không thực hành an toàn để lưu trữ mật khẩu email của bạn trong mã của bạn, đặc biệt nếu bạn có ý định chia sẻ nó với người khác. Thay vào đó, hãy sử dụng

server.sendmail(sender_email, receiver_email, message)
8 để cho người dùng nhập mật khẩu của họ khi chạy tập lệnh, như trong ví dụ trên. Nếu bạn không muốn mật khẩu của mình hiển thị trên màn hình khi bạn nhập nó, bạn có thể nhập mô -đun
server.sendmail(sender_email, receiver_email, message)
9 và sử dụng
sender_email = ""
receiver_email = ""
message = """\
Subject: Hi there

This message is sent from Python."""

# Send email here
0 thay vì đầu vào mù của mật khẩu.

Tùy chọn 2: Sử dụng
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
6

Thay vì sử dụng

server.sendmail(sender_email, receiver_email, message)
7 để tạo kết nối được bảo mật ngay từ đầu, chúng tôi có thể tạo kết nối SMTP không bảo đảm và mã hóa nó bằng
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
6.

Để thực hiện việc này, hãy tạo một thể hiện

sender_email = ""
receiver_email = ""
message = """\
Subject: Hi there

This message is sent from Python."""

# Send email here
4, gói gọn kết nối SMTP và cho phép bạn truy cập vào các phương thức của nó. Tôi khuyên bạn nên xác định máy chủ SMTP và cổng của bạn khi bắt đầu tập lệnh của bạn để định cấu hình chúng một cách dễ dàng.

Đoạn mã bên dưới sử dụng cấu trúc

sender_email = ""
receiver_email = ""
message = """\
Subject: Hi there

This message is sent from Python."""

# Send email here
5, thay vì định dạng
sender_email = ""
receiver_email = ""
message = """\
Subject: Hi there

This message is sent from Python."""

# Send email here
6 mà chúng tôi đã sử dụng trong ví dụ trước. Để đảm bảo rằng mã của bạn không bị lỗi khi có sự cố xảy ra, hãy đặt mã chính của bạn vào khối
sender_email = ""
receiver_email = ""
message = """\
Subject: Hi there

This message is sent from Python."""

# Send email here
7 và để khối
sender_email = ""
receiver_email = ""
message = """\
Subject: Hi there

This message is sent from Python."""

# Send email here
8 in bất kỳ thông báo lỗi nào thành
sender_email = ""
receiver_email = ""
message = """\
Subject: Hi there

This message is sent from Python."""

# Send email here
9:

import smtplib, ssl

smtp_server = "smtp.gmail.com"
port = 587  # For starttls
sender_email = ""
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

# Try to log in to server and send email
try:
    server = smtplib.SMTP(smtp_server,port)
    server.ehlo() # Can be omitted
    server.starttls(context=context) # Secure the connection
    server.ehlo() # Can be omitted
    server.login(sender_email, password)
    # TODO: Send email here
except Exception as e:
    # Print any error messages to stdout
    print(e)
finally:
    server.quit() 

Để xác định bản thân với máy chủ,

import smtplib, ssl

port = 465  # For SSL
smtp_server = "smtp.gmail.com"
sender_email = ""  # Enter your address
receiver_email = ""  # Enter receiver address
password = input("Type your password and press enter: ")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
0 (SMTP) hoặc
import smtplib, ssl

port = 465  # For SSL
smtp_server = "smtp.gmail.com"
sender_email = ""  # Enter your address
receiver_email = ""  # Enter receiver address
password = input("Type your password and press enter: ")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
1 (ESMTP) nên được gọi sau khi tạo đối tượng
import smtplib, ssl

port = 465  # For SSL
smtp_server = "smtp.gmail.com"
sender_email = ""  # Enter your address
receiver_email = ""  # Enter receiver address
password = input("Type your password and press enter: ")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
2 và một lần nữa sau
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
6. Hàm này được gọi hoàn toàn bởi
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
6 và
import smtplib, ssl

port = 465  # For SSL
smtp_server = "smtp.gmail.com"
sender_email = ""  # Enter your address
receiver_email = ""  # Enter receiver address
password = input("Type your password and press enter: ")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
5 nếu cần, vì vậy trừ khi bạn muốn kiểm tra các tiện ích mở rộng dịch vụ SMTP của máy chủ, không cần thiết phải sử dụng rõ ràng
import smtplib, ssl

port = 465  # For SSL
smtp_server = "smtp.gmail.com"
sender_email = ""  # Enter your address
receiver_email = ""  # Enter receiver address
password = input("Type your password and press enter: ")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
0 hoặc
import smtplib, ssl

port = 465  # For SSL
smtp_server = "smtp.gmail.com"
sender_email = ""  # Enter your address
receiver_email = ""  # Enter receiver address
password = input("Type your password and press enter: ")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
1.

Gửi email văn bản đơn giản của bạn

Sau khi bạn bắt đầu kết nối SMTP an toàn bằng một trong các phương thức trên, bạn có thể gửi email của mình bằng

import smtplib, ssl

port = 465  # For SSL
smtp_server = "smtp.gmail.com"
sender_email = ""  # Enter your address
receiver_email = ""  # Enter receiver address
password = input("Type your password and press enter: ")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
5, điều này khá nhiều những gì nó nói trên hộp thiếc:

server.sendmail(sender_email, receiver_email, message)

Tôi khuyên bạn nên xác định địa chỉ email và nội dung tin nhắn ở đầu tập lệnh của bạn, sau khi nhập, để bạn có thể thay đổi chúng một cách dễ dàng:

sender_email = ""
receiver_email = ""
message = """\
Subject: Hi there

This message is sent from Python."""

# Send email here

Chuỗi

import smtplib, ssl

port = 465  # For SSL
smtp_server = "smtp.gmail.com"
sender_email = ""  # Enter your address
receiver_email = ""  # Enter receiver address
password = input("Type your password and press enter: ")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
9 bắt đầu với
import smtplib, ssl

port = 587  # For starttls
smtp_server = "smtp.gmail.com"
sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
    server.ehlo()  # Can be omitted
    server.starttls(context=context)
    server.ehlo()  # Can be omitted
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
0 theo sau là hai dòng mới (
import smtplib, ssl

port = 587  # For starttls
smtp_server = "smtp.gmail.com"
sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
    server.ehlo()  # Can be omitted
    server.starttls(context=context)
    server.ehlo()  # Can be omitted
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
1). Điều này đảm bảo
import smtplib, ssl

port = 587  # For starttls
smtp_server = "smtp.gmail.com"
sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
    server.ehlo()  # Can be omitted
    server.starttls(context=context)
    server.ehlo()  # Can be omitted
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
2 hiển thị như là chủ đề của email và văn bản theo các dòng mới sẽ được coi là cơ thể tin nhắn.

Ví dụ mã bên dưới gửi email văn bản đơn giản bằng cách sử dụng

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
5:

import smtplib, ssl

port = 465  # For SSL
smtp_server = "smtp.gmail.com"
sender_email = ""  # Enter your address
receiver_email = ""  # Enter receiver address
password = input("Type your password and press enter: ")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)

Để so sánh, đây là một ví dụ mã gửi email văn bản đơn giản qua kết nối SMTP được bảo mật với

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
6. Các dòng
import smtplib, ssl

port = 587  # For starttls
smtp_server = "smtp.gmail.com"
sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
    server.ehlo()  # Can be omitted
    server.starttls(context=context)
    server.ehlo()  # Can be omitted
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
5 có thể bị bỏ qua, vì chúng được gọi là ngầm bởi
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
6 và
import smtplib, ssl

port = 465  # For SSL
smtp_server = "smtp.gmail.com"
sender_email = ""  # Enter your address
receiver_email = ""  # Enter receiver address
password = input("Type your password and press enter: ")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
5, nếu được yêu cầu:

import smtplib, ssl

port = 587  # For starttls
smtp_server = "smtp.gmail.com"
sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
    server.ehlo()  # Can be omitted
    server.starttls(context=context)
    server.ehlo()  # Can be omitted
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)

Gửi email lạ mắt

Gói

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
8 tích hợp của Python cho phép bạn cấu trúc các email lạ mắt hơn, sau đó có thể được chuyển với
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
7 như bạn đã làm. Dưới đây, bạn sẽ tìm hiểu cách sử dụng gói
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
8 để gửi email với nội dung và tệp đính kèm HTML.

Bao gồm nội dung HTML

Nếu bạn muốn định dạng văn bản trong email của mình (in đậm, in nghiêng, v.v.) hoặc nếu bạn muốn thêm bất kỳ hình ảnh, siêu liên kết hoặc nội dung phản hồi nào, thì HTML rất tiện dụng. Ngày nay, loại email phổ biến nhất là email MIME (Phần mở rộng thư Internet đa năng), kết hợp HTML và văn bản đơn giản. Tin nhắn MIME được xử lý bởi mô -đun Python từ ____ ____81. Đối với một mô tả chi tiết, kiểm tra tài liệu.bold, italics, and so on), or if you want to add any images, hyperlinks, or responsive content, then HTML comes in very handy. Today’s most common type of email is the MIME (Multipurpose Internet Mail Extensions) Multipart email, combining HTML and plain-text. MIME messages are handled by Python’s

import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")

message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = receiver_email

# Create the plain-text and HTML version of your message
text = """\
Hi,
How are you?
Real Python has many great tutorials:
www.realpython.com"""
html = """\

  
    

Hi,
How are you?
Real Python has many great tutorials.

""" # Turn these into plain/html MIMEText objects part1 = MIMEText(text, "plain") part2 = MIMEText(html, "html") # Add HTML/plain-text parts to MIMEMultipart message # The email client will try to render the last part first message.attach(part1) message.attach(part2) # Create secure connection with server and send email context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: server.login(sender_email, password) server.sendmail( sender_email, receiver_email, message.as_string() )
1 module. For a detailed description, check the documentation.

Vì không phải tất cả các ứng dụng email hiển thị nội dung HTML theo mặc định và một số người chỉ chọn để nhận email văn bản đơn giản vì lý do bảo mật, điều quan trọng là phải bao gồm một phương pháp thay thế văn bản đơn giản cho các tin nhắn HTML. Vì ứng dụng email sẽ hiển thị tệp đính kèm nhiều phần cuối cùng trước tiên, hãy đảm bảo thêm thông báo HTML sau phiên bản văn bản đơn giản.

Trong ví dụ dưới đây, các đối tượng

import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")

message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = receiver_email

# Create the plain-text and HTML version of your message
text = """\
Hi,
How are you?
Real Python has many great tutorials:
www.realpython.com"""
html = """\

  
    

Hi,
How are you?
Real Python has many great tutorials.

""" # Turn these into plain/html MIMEText objects part1 = MIMEText(text, "plain") part2 = MIMEText(html, "html") # Add HTML/plain-text parts to MIMEMultipart message # The email client will try to render the last part first message.attach(part1) message.attach(part2) # Create secure connection with server and send email context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: server.login(sender_email, password) server.sendmail( sender_email, receiver_email, message.as_string() )
2 của chúng tôi sẽ chứa các phiên bản HTML và văn bản đơn giản của thông điệp của chúng tôi và phiên bản
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")

message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = receiver_email

# Create the plain-text and HTML version of your message
text = """\
Hi,
How are you?
Real Python has many great tutorials:
www.realpython.com"""
html = """\

  
    

Hi,
How are you?
Real Python has many great tutorials.

""" # Turn these into plain/html MIMEText objects part1 = MIMEText(text, "plain") part2 = MIMEText(html, "html") # Add HTML/plain-text parts to MIMEMultipart message # The email client will try to render the last part first message.attach(part1) message.attach(part2) # Create secure connection with server and send email context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: server.login(sender_email, password) server.sendmail( sender_email, receiver_email, message.as_string() )
3 kết hợp chúng thành một thông báo với hai tùy chọn kết xuất thay thế:

import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")

message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = receiver_email

# Create the plain-text and HTML version of your message
text = """\
Hi,
How are you?
Real Python has many great tutorials:
www.realpython.com"""
html = """\

  
    

Hi,
How are you?
Real Python has many great tutorials.

""" # Turn these into plain/html MIMEText objects part1 = MIMEText(text, "plain") part2 = MIMEText(html, "html") # Add HTML/plain-text parts to MIMEMultipart message # The email client will try to render the last part first message.attach(part1) message.attach(part2) # Create secure connection with server and send email context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: server.login(sender_email, password) server.sendmail( sender_email, receiver_email, message.as_string() )

Trong ví dụ này, trước tiên bạn xác định thông báo văn bản đơn giản và HTML dưới dạng chuỗi chuỗi, sau đó lưu trữ chúng là ________ 84/________ 85

import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")

message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = receiver_email

# Create the plain-text and HTML version of your message
text = """\
Hi,
How are you?
Real Python has many great tutorials:
www.realpython.com"""
html = """\

  
    

Hi,
How are you?
Real Python has many great tutorials.

""" # Turn these into plain/html MIMEText objects part1 = MIMEText(text, "plain") part2 = MIMEText(html, "html") # Add HTML/plain-text parts to MIMEMultipart message # The email client will try to render the last part first message.attach(part1) message.attach(part2) # Create secure connection with server and send email context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: server.login(sender_email, password) server.sendmail( sender_email, receiver_email, message.as_string() )
6 đối tượng. Chúng có thể được thêm vào theo thứ tự này vào thông báo
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")

message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = receiver_email

# Create the plain-text and HTML version of your message
text = """\
Hi,
How are you?
Real Python has many great tutorials:
www.realpython.com"""
html = """\

  
    

Hi,
How are you?
Real Python has many great tutorials.

""" # Turn these into plain/html MIMEText objects part1 = MIMEText(text, "plain") part2 = MIMEText(html, "html") # Add HTML/plain-text parts to MIMEMultipart message # The email client will try to render the last part first message.attach(part1) message.attach(part2) # Create secure connection with server and send email context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: server.login(sender_email, password) server.sendmail( sender_email, receiver_email, message.as_string() )
3 và được gửi qua kết nối an toàn của bạn với máy chủ email. Hãy nhớ thêm thông báo HTML sau khi thay thế văn bản đơn giản, vì các ứng dụng email sẽ cố gắng hiển thị phần phụ cuối cùng.

Thêm tệp đính kèm bằng gói ---------- MESSAGE FOLLOWS ---------- b'X-Peer: ::1' b'' b'From: ' b'To: ' b'Subject: a local test mail' b'' b'Hello there, here is a test email' ------------ END MESSAGE ------------ 8

Để gửi các tệp nhị phân đến một máy chủ email được thiết kế để hoạt động với dữ liệu văn bản, chúng cần được mã hóa trước khi vận chuyển. Điều này được thực hiện phổ biến nhất bằng cách sử dụng

import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")

message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = receiver_email

# Create the plain-text and HTML version of your message
text = """\
Hi,
How are you?
Real Python has many great tutorials:
www.realpython.com"""
html = """\

  
    

Hi,
How are you?
Real Python has many great tutorials.

""" # Turn these into plain/html MIMEText objects part1 = MIMEText(text, "plain") part2 = MIMEText(html, "html") # Add HTML/plain-text parts to MIMEMultipart message # The email client will try to render the last part first message.attach(part1) message.attach(part2) # Create secure connection with server and send email context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: server.login(sender_email, password) server.sendmail( sender_email, receiver_email, message.as_string() )
9, mã hóa dữ liệu nhị phân thành các ký tự ASCII có thể in.

Ví dụ mã bên dưới cho thấy cách gửi email với tệp PDF dưới dạng tệp đính kèm:

import email, smtplib, ssl

from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

subject = "An email with attachment from Python"
body = "This is an email with attachment sent from Python"
sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")

# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message["Bcc"] = receiver_email  # Recommended for mass emails

# Add body to email
message.attach(MIMEText(body, "plain"))

filename = "document.pdf"  # In same directory as script

# Open PDF file in binary mode
with open(filename, "rb") as attachment:
    # Add file as application/octet-stream
    # Email client can usually download this automatically as attachment
    part = MIMEBase("application", "octet-stream")
    part.set_payload(attachment.read())

# Encode file in ASCII characters to send by email    
encoders.encode_base64(part)

# Add header as key/value pair to attachment part
part.add_header(
    "Content-Disposition",
    f"attachment; filename= {filename}",
)

# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()

# Log in to server using secure context and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, text)

Thông báo

import email, smtplib, ssl

from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

subject = "An email with attachment from Python"
body = "This is an email with attachment sent from Python"
sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")

# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message["Bcc"] = receiver_email  # Recommended for mass emails

# Add body to email
message.attach(MIMEText(body, "plain"))

filename = "document.pdf"  # In same directory as script

# Open PDF file in binary mode
with open(filename, "rb") as attachment:
    # Add file as application/octet-stream
    # Email client can usually download this automatically as attachment
    part = MIMEBase("application", "octet-stream")
    part.set_payload(attachment.read())

# Encode file in ASCII characters to send by email    
encoders.encode_base64(part)

# Add header as key/value pair to attachment part
part.add_header(
    "Content-Disposition",
    f"attachment; filename= {filename}",
)

# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()

# Log in to server using secure context and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, text)
0 chấp nhận các tham số dưới dạng các cặp khóa/giá trị kiểu RFC5233, được lưu trữ trong một từ điển và được chuyển sang phương thức
import email, smtplib, ssl

from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

subject = "An email with attachment from Python"
body = "This is an email with attachment sent from Python"
sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")

# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message["Bcc"] = receiver_email  # Recommended for mass emails

# Add body to email
message.attach(MIMEText(body, "plain"))

filename = "document.pdf"  # In same directory as script

# Open PDF file in binary mode
with open(filename, "rb") as attachment:
    # Add file as application/octet-stream
    # Email client can usually download this automatically as attachment
    part = MIMEBase("application", "octet-stream")
    part.set_payload(attachment.read())

# Encode file in ASCII characters to send by email    
encoders.encode_base64(part)

# Add header as key/value pair to attachment part
part.add_header(
    "Content-Disposition",
    f"attachment; filename= {filename}",
)

# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()

# Log in to server using secure context and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, text)
1 của lớp cơ sở
import email, smtplib, ssl

from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

subject = "An email with attachment from Python"
body = "This is an email with attachment sent from Python"
sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")

# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message["Bcc"] = receiver_email  # Recommended for mass emails

# Add body to email
message.attach(MIMEText(body, "plain"))

filename = "document.pdf"  # In same directory as script

# Open PDF file in binary mode
with open(filename, "rb") as attachment:
    # Add file as application/octet-stream
    # Email client can usually download this automatically as attachment
    part = MIMEBase("application", "octet-stream")
    part.set_payload(attachment.read())

# Encode file in ASCII characters to send by email    
encoders.encode_base64(part)

# Add header as key/value pair to attachment part
part.add_header(
    "Content-Disposition",
    f"attachment; filename= {filename}",
)

# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()

# Log in to server using secure context and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, text)
2.

Kiểm tra tài liệu cho mô -đun Python từ

import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")

message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = receiver_email

# Create the plain-text and HTML version of your message
text = """\
Hi,
How are you?
Real Python has many great tutorials:
www.realpython.com"""
html = """\

  
    

Hi,
How are you?
Real Python has many great tutorials.

""" # Turn these into plain/html MIMEText objects part1 = MIMEText(text, "plain") part2 = MIMEText(html, "html") # Add HTML/plain-text parts to MIMEMultipart message # The email client will try to render the last part first message.attach(part1) message.attach(part2) # Create secure connection with server and send email context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: server.login(sender_email, password) server.sendmail( sender_email, receiver_email, message.as_string() )
1 để tìm hiểu thêm về việc sử dụng các lớp MIME.

Gửi nhiều email được cá nhân hóa

Hãy tưởng tượng bạn muốn gửi email cho các thành viên trong tổ chức của bạn, để nhắc nhở họ trả phí đóng góp của họ. Hoặc có thể bạn muốn gửi sinh viên trong lớp email được cá nhân hóa của bạn với các điểm cho bài tập gần đây của họ. Những nhiệm vụ này là một làn gió trong Python.

Tạo tệp CSV với thông tin cá nhân có liên quan

Điểm bắt đầu dễ dàng để gửi nhiều email được cá nhân hóa là tạo tệp CSV (giá trị phân tách bằng dấu phẩy) chứa tất cả các thông tin cá nhân cần thiết. .

Dưới đây là nội dung của tệp

import email, smtplib, ssl

from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

subject = "An email with attachment from Python"
body = "This is an email with attachment sent from Python"
sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")

# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message["Bcc"] = receiver_email  # Recommended for mass emails

# Add body to email
message.attach(MIMEText(body, "plain"))

filename = "document.pdf"  # In same directory as script

# Open PDF file in binary mode
with open(filename, "rb") as attachment:
    # Add file as application/octet-stream
    # Email client can usually download this automatically as attachment
    part = MIMEBase("application", "octet-stream")
    part.set_payload(attachment.read())

# Encode file in ASCII characters to send by email    
encoders.encode_base64(part)

# Add header as key/value pair to attachment part
part.add_header(
    "Content-Disposition",
    f"attachment; filename= {filename}",
)

# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()

# Log in to server using secure context and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, text)
4, mà tôi đã lưu trong cùng một thư mục với mã Python của tôi. Nó chứa tên, địa chỉ và điểm số cho một tập hợp những người hư cấu. Tôi đã sử dụng các cấu trúc
import smtplib, ssl

port = 465  # For SSL
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
    server.login("", password)
    # TODO: Send email here
3 để đảm bảo tất cả các email kết thúc trong hộp thư đến của riêng tôi, trong ví dụ này là:

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
0

Khi tạo tệp CSV, hãy đảm bảo tách các giá trị của bạn bằng dấu phẩy mà không có bất kỳ khoảng trắng xung quanh nào.

Vòng lặp qua hàng để gửi nhiều email

Ví dụ mã bên dưới cho bạn biết cách mở tệp CSV và lặp qua các dòng nội dung của nó (bỏ qua hàng tiêu đề). Để đảm bảo rằng mã hoạt động chính xác trước khi bạn gửi email đến tất cả các liên hệ của mình, tôi đã in

import email, smtplib, ssl

from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

subject = "An email with attachment from Python"
body = "This is an email with attachment sent from Python"
sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")

# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message["Bcc"] = receiver_email  # Recommended for mass emails

# Add body to email
message.attach(MIMEText(body, "plain"))

filename = "document.pdf"  # In same directory as script

# Open PDF file in binary mode
with open(filename, "rb") as attachment:
    # Add file as application/octet-stream
    # Email client can usually download this automatically as attachment
    part = MIMEBase("application", "octet-stream")
    part.set_payload(attachment.read())

# Encode file in ASCII characters to send by email    
encoders.encode_base64(part)

# Add header as key/value pair to attachment part
part.add_header(
    "Content-Disposition",
    f"attachment; filename= {filename}",
)

# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()

# Log in to server using secure context and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, text)
6 cho mỗi liên hệ, sau này chúng tôi có thể thay thế bằng chức năng thực sự gửi email:

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
1

Trong ví dụ trên, sử dụng

import email, smtplib, ssl

from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

subject = "An email with attachment from Python"
body = "This is an email with attachment sent from Python"
sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")

# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message["Bcc"] = receiver_email  # Recommended for mass emails

# Add body to email
message.attach(MIMEText(body, "plain"))

filename = "document.pdf"  # In same directory as script

# Open PDF file in binary mode
with open(filename, "rb") as attachment:
    # Add file as application/octet-stream
    # Email client can usually download this automatically as attachment
    part = MIMEBase("application", "octet-stream")
    part.set_payload(attachment.read())

# Encode file in ASCII characters to send by email    
encoders.encode_base64(part)

# Add header as key/value pair to attachment part
part.add_header(
    "Content-Disposition",
    f"attachment; filename= {filename}",
)

# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()

# Log in to server using secure context and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, text)
7Makes chắc chắn rằng tệp của bạn đóng ở cuối khối mã.
import email, smtplib, ssl

from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

subject = "An email with attachment from Python"
body = "This is an email with attachment sent from Python"
sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")

# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message["Bcc"] = receiver_email  # Recommended for mass emails

# Add body to email
message.attach(MIMEText(body, "plain"))

filename = "document.pdf"  # In same directory as script

# Open PDF file in binary mode
with open(filename, "rb") as attachment:
    # Add file as application/octet-stream
    # Email client can usually download this automatically as attachment
    part = MIMEBase("application", "octet-stream")
    part.set_payload(attachment.read())

# Encode file in ASCII characters to send by email    
encoders.encode_base64(part)

# Add header as key/value pair to attachment part
part.add_header(
    "Content-Disposition",
    f"attachment; filename= {filename}",
)

# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()

# Log in to server using secure context and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, text)
8 giúp dễ dàng đọc dòng tệp CSV theo từng dòng và trích xuất các giá trị của nó. Dòng
import email, smtplib, ssl

from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

subject = "An email with attachment from Python"
body = "This is an email with attachment sent from Python"
sender_email = ""
receiver_email = ""
password = input("Type your password and press enter:")

# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message["Bcc"] = receiver_email  # Recommended for mass emails

# Add body to email
message.attach(MIMEText(body, "plain"))

filename = "document.pdf"  # In same directory as script

# Open PDF file in binary mode
with open(filename, "rb") as attachment:
    # Add file as application/octet-stream
    # Email client can usually download this automatically as attachment
    part = MIMEBase("application", "octet-stream")
    part.set_payload(attachment.read())

# Encode file in ASCII characters to send by email    
encoders.encode_base64(part)

# Add header as key/value pair to attachment part
part.add_header(
    "Content-Disposition",
    f"attachment; filename= {filename}",
)

# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()

# Log in to server using secure context and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, text)
9 bỏ qua hàng tiêu đề, do đó dòng sau
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
00 chia các hàng tiếp theo tại mỗi dấu phẩy và lưu trữ các giá trị kết quả trong chuỗi
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
01,
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
8 và
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
03 cho tiếp xúc hiện tại.

Nếu các giá trị trong tệp CSV của bạn chứa khoảng trắng ở một hoặc cả hai bên, bạn có thể xóa chúng bằng phương thức

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
04.

Nội dung cá nhân hóa

Bạn có thể đặt nội dung được cá nhân hóa vào một tin nhắn bằng cách sử dụng

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
05 để điền vào các trình giữ chỗ trong khung xoăn. Ví dụ,
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
06 sẽ cung cấp cho bạn
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
07.

Kể từ Python 3.6, định dạng chuỗi có thể được thực hiện một cách tao nhã hơn bằng cách sử dụng các chuỗi F, nhưng những điều này đòi hỏi các giữ chỗ phải được xác định trước chính F-String. Để xác định thông báo email ở đầu tập lệnh và điền vào các trình giữ chỗ cho mỗi liên hệ khi lặp qua tệp CSV, phương thức

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
08 cũ hơn được sử dụng.

Với suy nghĩ này, bạn có thể thiết lập một cơ quan thông điệp chung, với những người giữ chỗ có thể được điều chỉnh cho các cá nhân.

Mã ví dụ

Ví dụ mã sau cho phép bạn gửi email được cá nhân hóa đến nhiều liên hệ. Nó lặp qua một tệp CSV với

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
09 cho mỗi liên hệ, như trong ví dụ trên.

Thông điệp chung được xác định ở phần đầu của tập lệnh và đối với mỗi liên hệ trong tệp CSV

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
10 và
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
11 được điền vào và một email được cá nhân hóa được gửi qua kết nối an toàn với máy chủ Gmail, như bạn đã thấy trước đây:

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
2

Yagmail

Có nhiều thư viện được thiết kế để làm cho việc gửi email dễ dàng hơn, chẳng hạn như phong bì, flanker và yagmail. Yagmail được thiết kế để hoạt động cụ thể với Gmail và nó đơn giản hóa rất nhiều quá trình gửi email qua API thân thiện, như bạn có thể thấy trong ví dụ mã bên dưới:

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
3

Ví dụ mã này gửi một email với tệp đính kèm PDF trong một phần của các dòng cần thiết cho ví dụ của chúng tôi bằng cách sử dụng

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
8 và
---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
7.

Khi thiết lập Yagmail, bạn có thể thêm xác nhận Gmail của mình vào Keyring của HĐH, như được mô tả trong tài liệu. Nếu bạn không làm điều này, Yagmail sẽ nhắc bạn nhập mật khẩu của mình khi được yêu cầu và lưu trữ nó tự động trong khóa.

Dịch vụ email giao dịch

Nếu bạn có kế hoạch gửi một khối lượng lớn email, muốn xem số liệu thống kê email và muốn đảm bảo giao hàng đáng tin cậy, có thể đáng để xem xét các dịch vụ email giao dịch. Mặc dù tất cả các dịch vụ sau đây đã trả tiền cho các kế hoạch gửi khối lượng email lớn, nhưng chúng cũng đi kèm với một kế hoạch miễn phí để bạn có thể dùng thử. Một số trong các gói miễn phí này là hợp lệ vô thời hạn và có thể đủ cho nhu cầu email của bạn.

Dưới đây là tổng quan về các kế hoạch miễn phí cho một số dịch vụ email giao dịch lớn. Nhấp vào tên nhà cung cấp sẽ đưa bạn đến phần định giá trên trang web của họ.

Bạn có thể chạy tìm kiếm trên Google để xem nhà cung cấp nào phù hợp nhất với nhu cầu của bạn hoặc chỉ cần thử một vài kế hoạch miễn phí để xem bạn thích làm việc với API nào.

Ví dụ mã SendGrid

Dưới đây, một ví dụ về mã để gửi email với SendGrid để cung cấp cho bạn hương vị về cách sử dụng dịch vụ email giao dịch với Python:

---------- MESSAGE FOLLOWS ----------
b'X-Peer: ::1'
b''
b'From: '
b'To: '
b'Subject: a local test mail'
b''
b'Hello there, here is a test email'
------------ END MESSAGE ------------
4

Để chạy mã này, trước tiên bạn phải:

  • Đăng ký tài khoản SendGrid (miễn phí)
  • Yêu cầu khóa API để xác thực người dùng
  • Thêm khóa API của bạn bằng cách nhập
    ---------- MESSAGE FOLLOWS ----------
    b'X-Peer: ::1'
    b''
    b'From: '
    b'To: '
    b'Subject: a local test mail'
    b''
    b'Hello there, here is a test email'
    ------------ END MESSAGE ------------
    
    14 vào dấu nhắc lệnh (để lưu trữ khóa API này vĩnh viễn) hoặc
    ---------- MESSAGE FOLLOWS ----------
    b'X-Peer: ::1'
    b''
    b'From: '
    b'To: '
    b'Subject: a local test mail'
    b''
    b'Hello there, here is a test email'
    ------------ END MESSAGE ------------
    
    15 để lưu trữ nó chỉ cho phiên khách hàng hiện tại

Thông tin thêm về cách thiết lập SendGrid cho Mac và Windows có thể được tìm thấy trong kho lưu trữ README trên GitHub.

Sự kết luận

Bây giờ bạn có thể bắt đầu kết nối SMTP an toàn và gửi nhiều email được cá nhân hóa đến những người trong danh sách liên hệ của bạn!

Bạn đã học được cách gửi email HTML với phương án thay thế theo văn bản đơn giản và đính kèm các tệp vào email của bạn. Gói Yagmail đơn giản hóa tất cả các tác vụ này khi bạn sử dụng tài khoản Gmail. Nếu bạn có kế hoạch gửi khối lượng email lớn, đáng để xem xét các dịch vụ email giao dịch.

Thích gửi email với Python và hãy nhớ: Không có thư rác xin vui lòng!

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem cùng với hướng dẫn bằng văn bản để hiểu sâu hơn về sự hiểu biết của bạn: Gửi email với Python This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Sending Emails With Python