Hướng dẫn smtplib Python chỉ ra cách gửi email bằng Python với mô-đun smtplib. Để gửi email, chúng tôi sử dụng máy chủ phát triển Python, dịch vụ trực tuyến Mailtrap và máy chủ thư lưu trữ web được chia sẻ
Giao thức truyền thư đơn giản (SMTP) là một giao thức truyền thông để truyền thư điện tử. Is là một tiêu chuẩn Internet, lần đầu tiên được xác định vào năm 1982 bởi RFC 821 và được cập nhật vào năm 2008 bởi RFC 5321 để bổ sung SMTP mở rộng. Máy chủ thư và các tác nhân chuyển thư khác sử dụng SMTP để gửi và nhận thư
Mô-đun smtplib
smtplib là một thư viện Python để gửi email bằng Giao thức chuyển thư đơn giản (SMTP). #!/usr/bin/python import smtplib from email.mime.text import MIMEText sender = 'admin@example.com' receivers = ['info@example.com'] port = 1025 msg = MIMEText('This is test mail') msg['Subject'] = 'Test mail' msg['From'] = 'admin@example.com' msg['To'] = 'info@example.com' with smtplib.SMTP('localhost', port) as server: # server.login('username', 'password') server.sendmail(sender, receivers, msg.as_string()) print("Successfully sent email") 8 là một mô-đun tích hợp sẵn; . Nó trừu tượng hóa tất cả sự phức tạp của SMTP
Để thực sự gửi email, chúng tôi cần có quyền truy cập vào máy chủ thư. Python đi kèm với một máy chủ thư phát triển đơn giản. Mailslurper là một máy chủ phát triển cục bộ dễ sử dụng. Các nhà cung cấp dịch vụ lưu trữ web được chia sẻ cung cấp cho chúng tôi quyền truy cập vào máy chủ thư. Chúng tôi có thể tìm thấy các chi tiết trong tài khoản
Ghi chú. Tránh sử dụng Gmail, vì đây là một máy chủ được bảo mật cao và khá phức tạp để làm cho nó hoạt động. Trên thực tế, hầu hết nếu không muốn nói là tất cả các ví dụ trên Internet về cách gửi email bằng máy chủ Gmail đều không hoạt động. Thay vào đó, hãy sử dụng máy chủ phát triển hoặc máy chủ lưu trữ web được chia sẻ
Cuối cùng, chúng ta có thể sử dụng một dịch vụ web. Có các dịch vụ web phát triển như MailTrap hoặc MailSlurp hoặc các dịch vụ sản xuất như Mailgun hoặc Mandrill
Sử dụng máy chủ thư tích hợp Python
$ python -m smtpd -c DebuggingServer -n localhost:1025Chúng tôi khởi động máy chủ thư tích hợp Python trên cổng 1025
#!/usr/bin/python import smtplib from email.mime.text import MIMEText sender = 'admin@example.com' receivers = ['info@example.com'] port = 1025 msg = MIMEText('This is test mail') msg['Subject'] = 'Test mail' msg['From'] = 'admin@example.com' msg['To'] = 'info@example.com' with smtplib.SMTP('localhost', port) as server: # server.login('username', 'password') server.sendmail(sender, receivers, msg.as_string()) print("Successfully sent email")Chúng tôi gửi một tin nhắn văn bản đơn giản đến máy chủ thư phát triển cục bộ
sender = 'admin@example.com' receivers = ['info@example.com']Chúng tôi cung cấp (những) người gửi và người nhận. #!/usr/bin/python import smtplib from email.mime.text import MIMEText sender = 'admin@example.com' receivers = ['info@example.com'] port = 1025 msg = MIMEText('This is test mail') msg['Subject'] = 'Test mail' msg['From'] = 'admin@example.com' msg['To'] = 'info@example.com' with smtplib.SMTP('localhost', port) as server: # server.login('username', 'password') server.sendmail(sender, receivers, msg.as_string()) print("Successfully sent email") 9 là tên miền chuyên dùng cho các ví dụ minh họa trong tài liệu
msg = MIMEText('This is test mail') msg['Subject'] = 'Test mail' msg['From'] = 'admin@example.com' msg['To'] = 'info@example.com'sender = 'admin@example.com' receivers = ['info@example.com'] 0 được sử dụng để gửi email văn bản. Chúng tôi cung cấp các tùy chọn chủ đề, từ và đến
with smtplib.SMTP('localhost', port) as server: ...Lớp sender = 'admin@example.com' receivers = ['info@example.com'] 1 quản lý kết nối đến máy chủ SMTP
# server.login('username', 'password')Vì chúng tôi sử dụng máy chủ phát triển cục bộ nên chúng tôi không phải đăng nhập
$ python -m smtpd -c DebuggingServer -n localhost:1025 0Email được gửi với sender = 'admin@example.com' receivers = ['info@example.com'] 2
$ python -m smtpd -c DebuggingServer -n localhost:1025 2Chúng tôi nhận được tin nhắn này sau khi chúng tôi gửi email
quảng cáoMailtrap cung cấp gói miễn phí cho phép chúng tôi gửi 500 thư mỗi tháng. Thiết lập Mailtrap rất dễ dàng. Thực sự chỉ mất vài giây nếu chúng ta có tài khoản Github hoặc Google
Thông tin đăng nhập cần thiết được cung cấp trong trang cài đặt. Ngoài ra, có những ví dụ mã ngắn cho biết cách sử dụng dịch vụ, bao gồm #!/usr/bin/python import smtplib from email.mime.text import MIMEText sender = 'admin@example.com' receivers = ['info@example.com'] port = 1025 msg = MIMEText('This is test mail') msg['Subject'] = 'Test mail' msg['From'] = 'admin@example.com' msg['To'] = 'info@example.com' with smtplib.SMTP('localhost', port) as server: # server.login('username', 'password') server.sendmail(sender, receivers, msg.as_string()) print("Successfully sent email") 8, sender = 'admin@example.com' receivers = ['info@example.com'] 4 hoặc sender = 'admin@example.com' receivers = ['info@example.com'] 5
$ python -m smtpd -c DebuggingServer -n localhost:1025 6Ví dụ gửi một thư đơn giản đến tài khoản Mailtrap
$ python -m smtpd -c DebuggingServer -n localhost:1025 7Tên người dùng và mật khẩu được cung cấp trong trang cài đặt;
Gửi email có tệp đính kèm
sender = 'admin@example.com' receivers = ['info@example.com'] 6 được sử dụng khi chúng tôi có tệp đính kèm hoặc muốn cung cấp các phiên bản thay thế của cùng một nội dung (e. g. phiên bản văn bản/HTML thuần túy)
#!/usr/bin/python import smtplib from email.mime.text import MIMEText sender = 'admin@example.com' receivers = ['info@example.com'] port = 1025 msg = MIMEText('This is test mail') msg['Subject'] = 'Test mail' msg['From'] = 'admin@example.com' msg['To'] = 'info@example.com' with smtplib.SMTP('localhost', port) as server: # server.login('username', 'password') server.sendmail(sender, receivers, msg.as_string()) print("Successfully sent email") 0Chúng tôi có một tệp văn bản đơn giản
#!/usr/bin/python import smtplib from email.mime.text import MIMEText sender = 'admin@example.com' receivers = ['info@example.com'] port = 1025 msg = MIMEText('This is test mail') msg['Subject'] = 'Test mail' msg['From'] = 'admin@example.com' msg['To'] = 'info@example.com' with smtplib.SMTP('localhost', port) as server: # server.login('username', 'password') server.sendmail(sender, receivers, msg.as_string()) print("Successfully sent email") 1Ví dụ gửi email có đính kèm tệp văn bản tới Mailtrap
#!/usr/bin/python import smtplib from email.mime.text import MIMEText sender = 'admin@example.com' receivers = ['info@example.com'] port = 1025 msg = MIMEText('This is test mail') msg['Subject'] = 'Test mail' msg['From'] = 'admin@example.com' msg['To'] = 'info@example.com' with smtplib.SMTP('localhost', port) as server: # server.login('username', 'password') server.sendmail(sender, receivers, msg.as_string()) print("Successfully sent email") 2Chúng tôi đọc nội dung của tệp văn bản
Quảng cáo#!/usr/bin/python import smtplib from email.mime.text import MIMEText sender = 'admin@example.com' receivers = ['info@example.com'] port = 1025 msg = MIMEText('This is test mail') msg['Subject'] = 'Test mail' msg['From'] = 'admin@example.com' msg['To'] = 'info@example.com' with smtplib.SMTP('localhost', port) as server: # server.login('username', 'password') server.sendmail(sender, receivers, msg.as_string()) print("Successfully sent email") 3Tệp đính kèm được thêm bằng phương pháp sender = 'admin@example.com' receivers = ['info@example.com'] 7
Mailtrap không hỗ trợ SSL trên bất kỳ cổng SMTP nào, nó chỉ hỗ trợ STARTTLS. Nếu chúng tôi cố gắng sử dụng SSL, chúng tôi sẽ nhận được thông báo lỗi sau
#!/usr/bin/python import smtplib from email.mime.text import MIMEText sender = 'admin@example.com' receivers = ['info@example.com'] port = 1025 msg = MIMEText('This is test mail') msg['Subject'] = 'Test mail' msg['From'] = 'admin@example.com' msg['To'] = 'info@example.com' with smtplib.SMTP('localhost', port) as server: # server.login('username', 'password') server.sendmail(sender, receivers, msg.as_string()) print("Successfully sent email") 4Cái gọi là TLS cơ hội (Transport Layer Security) là một phần mở rộng trong các giao thức truyền thông văn bản thuần túy. Nó cung cấp một cách để nâng cấp kết nối văn bản thuần túy thành kết nối được mã hóa (TLS hoặc SSL) thay vì sử dụng một cổng riêng để liên lạc được mã hóa. Một số giao thức sử dụng lệnh có tên STARTTLS cho mục đích này. Nó chủ yếu nhằm mục đích như một biện pháp đối phó với giám sát thụ động
#!/usr/bin/python import smtplib from email.mime.text import MIMEText sender = 'admin@example.com' receivers = ['info@example.com'] port = 1025 msg = MIMEText('This is test mail') msg['Subject'] = 'Test mail' msg['From'] = 'admin@example.com' msg['To'] = 'info@example.com' with smtplib.SMTP('localhost', port) as server: # server.login('username', 'password') server.sendmail(sender, receivers, msg.as_string()) print("Successfully sent email") 5Ví dụ gửi email đến tài khoản Mailtrap với TLS cơ hội
#!/usr/bin/python import smtplib from email.mime.text import MIMEText sender = 'admin@example.com' receivers = ['info@example.com'] port = 1025 msg = MIMEText('This is test mail') msg['Subject'] = 'Test mail' msg['From'] = 'admin@example.com' msg['To'] = 'info@example.com' with smtplib.SMTP('localhost', port) as server: # server.login('username', 'password') server.sendmail(sender, receivers, msg.as_string()) print("Successfully sent email") 6sender = 'admin@example.com' receivers = ['info@example.com'] 8 đặt kết nối đến máy chủ SMTP ở chế độ TLS
Gửi thư qua SSL
Ví dụ sau gửi email qua SSL. Máy chủ SMTP lưu trữ web (từ hỗ trợ web. sk) được sử dụng