Làm cách nào để gửi email bằng SMTP trong Python?

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 = '[email protected]'
receivers = ['[email protected]']


port = 1025
msg = MIMEText('This is test mail')

msg['Subject'] = 'Test mail'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'

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:1025

Chú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 = '[email protected]'
receivers = ['[email protected]']


port = 1025
msg = MIMEText('This is test mail')

msg['Subject'] = 'Test mail'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'

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 = '[email protected]'
receivers = ['[email protected]']

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 = '[email protected]'
receivers = ['[email protected]']


port = 1025
msg = MIMEText('This is test mail')

msg['Subject'] = 'Test mail'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'

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'] = '[email protected]'
msg['To'] = '[email protected]'

sender = '[email protected]'
receivers = ['[email protected]']
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 = '[email protected]'
receivers = ['[email protected]']
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
0

Email được gửi với

sender = '[email protected]'
receivers = ['[email protected]']
2

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

Chúng tôi nhận được tin nhắn này sau khi chúng tôi gửi email

quảng cáo

Mailtrap 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 = '[email protected]'
receivers = ['[email protected]']


port = 1025
msg = MIMEText('This is test mail')

msg['Subject'] = 'Test mail'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'

with smtplib.SMTP('localhost', port) as server:
    
    # server.login('username', 'password')
    server.sendmail(sender, receivers, msg.as_string())
    print("Successfully sent email")
8,
sender = '[email protected]'
receivers = ['[email protected]']
4 hoặc
sender = '[email protected]'
receivers = ['[email protected]']
5

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

Ví dụ gửi một thư đơn giản đến tài khoản Mailtrap

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

Tê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 = '[email protected]'
receivers = ['[email protected]']
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 = '[email protected]'
receivers = ['[email protected]']


port = 1025
msg = MIMEText('This is test mail')

msg['Subject'] = 'Test mail'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'

with smtplib.SMTP('localhost', port) as server:
    
    # server.login('username', 'password')
    server.sendmail(sender, receivers, msg.as_string())
    print("Successfully sent email")
0

Chú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 = '[email protected]'
receivers = ['[email protected]']


port = 1025
msg = MIMEText('This is test mail')

msg['Subject'] = 'Test mail'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'

with smtplib.SMTP('localhost', port) as server:
    
    # server.login('username', 'password')
    server.sendmail(sender, receivers, msg.as_string())
    print("Successfully sent email")
1

Ví 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 = '[email protected]'
receivers = ['[email protected]']


port = 1025
msg = MIMEText('This is test mail')

msg['Subject'] = 'Test mail'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'

with smtplib.SMTP('localhost', port) as server:
    
    # server.login('username', 'password')
    server.sendmail(sender, receivers, msg.as_string())
    print("Successfully sent email")
2

Chú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 = '[email protected]'
receivers = ['[email protected]']


port = 1025
msg = MIMEText('This is test mail')

msg['Subject'] = 'Test mail'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'

with smtplib.SMTP('localhost', port) as server:
    
    # server.login('username', 'password')
    server.sendmail(sender, receivers, msg.as_string())
    print("Successfully sent email")
3

Tệp đính kèm được thêm bằng phương pháp

sender = '[email protected]'
receivers = ['[email protected]']
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 = '[email protected]'
receivers = ['[email protected]']


port = 1025
msg = MIMEText('This is test mail')

msg['Subject'] = 'Test mail'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'

with smtplib.SMTP('localhost', port) as server:
    
    # server.login('username', 'password')
    server.sendmail(sender, receivers, msg.as_string())
    print("Successfully sent email")
4

Cá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 = '[email protected]'
receivers = ['[email protected]']


port = 1025
msg = MIMEText('This is test mail')

msg['Subject'] = 'Test mail'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'

with smtplib.SMTP('localhost', port) as server:
    
    # server.login('username', 'password')
    server.sendmail(sender, receivers, msg.as_string())
    print("Successfully sent email")
5

Ví 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 = '[email protected]'
receivers = ['[email protected]']


port = 1025
msg = MIMEText('This is test mail')

msg['Subject'] = 'Test mail'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'

with smtplib.SMTP('localhost', port) as server:
    
    # server.login('username', 'password')
    server.sendmail(sender, receivers, msg.as_string())
    print("Successfully sent email")
6

sender = '[email protected]'
receivers = ['[email protected]']
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

Làm cách nào để gửi email bằng Python?

Sử dụng thư viện smtplib tích hợp sẵn của Python để gửi email cơ bản . Gửi email có nội dung HTML và tệp đính kèm bằng gói email. Gửi nhiều email được cá nhân hóa bằng tệp CSV có dữ liệu liên hệ. 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ã.

Làm cách nào để tạo máy chủ SMTP bằng Python?

Python cung cấp mô-đun smtplib, mô-đun này xác định đối tượng phiên máy khách SMTP có thể được sử dụng để gửi thư đến bất kỳ máy Internet nào có trình nền trình nghe SMTP hoặc ESMTP. host − This is the host running your SMTP server. You can specify IP address of the host or a domain name like tutorialspoint.com.

Làm cách nào để gửi email mà không cần SMTP Python?

Nếu bạn không quan tâm đến khả năng gửi thì tất nhiên bạn có thể sử dụng SendMail cục bộ từ Python , SendMail lắng nghe địa chỉ loopback (127. 0. 0. 1) trên cổng 25 giống như bất kỳ máy chủ SMTP nào khác, vì vậy bạn có thể sử dụng smtplib để gửi qua SendMail mà không cần sử dụng máy chủ SMTP bên ngoài.

Python có thể được sử dụng để gửi email không?

Gửi email bằng Python và SMTP . Mô-đun sử dụng giao thức RFC 821 tiêu chuẩn, do đó không cần cài đặt thêm hoặc thủ thuật nào. in its standard library there is a built-in smtplib module that is used for sending emails via SMTP connection. The module uses the standard RFC 821 protocol, so no extra installations or tricks are required.