📜  Python -SMTP(1)

📅  最后修改于: 2023-12-03 15:04:03.740000             🧑  作者: Mango

Python - SMTP

SMTP stands for Simple Mail Transfer Protocol, which is a protocol for sending emails. In Python, the smtplib module provides a way to send emails using SMTP.

Sending an Email

To send an email using Python, you first need to import the smtplib module and create an instance of the SMTP class. Then, you can use the sendmail method to send the email.

Here is an example of sending an email using Gmail SMTP:

import smtplib

# Email information
sender_email = "example@gmail.com"
receiver_email = "receiver@example.com"
password = "your_password"

# SMTP server information
smtp_server = "smtp.gmail.com"
smtp_port = 587

# Create the SMTP object
with smtplib.SMTP(smtp_server, smtp_port) as server:
    # Login to the SMTP server
    server.starttls()
    server.login(sender_email, password)

    # Compose the message
    subject = "Subject"
    body = "Body"
    message = f"Subject: {subject}\n\n{body}"

    # Send the message
    server.sendmail(sender_email, receiver_email, message)

In the example above, we first specify the email information, including the sender's email address, receiver's email address, and password. Then, we specify the SMTP server information, including the server name and port number.

Next, we create an instance of the SMTP class by passing in the server name and port number. We then use the starttls method to encrypt the connection and the login method to authenticate with the SMTP server.

After that, we compose the message by specifying the subject and body and concatenating them into a single string. Finally, we use the sendmail method to send the email.

Handling Exceptions

When sending an email using Python, it is important to handle exceptions. There are various exceptions that can occur when sending an email, such as network errors, authentication errors, and email formatting errors.

Here is an example of handling exceptions when sending an email:

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

# Email information
sender_email = "example@gmail.com"
receiver_email = "receiver@example.com"
password = "your_password"

# SMTP server information
smtp_server = "smtp.gmail.com"
smtp_port = 587

# Create the message
subject = "Subject"
body = "Body"
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message.attach(MIMEText(body, "plain"))

try:
    # Create the SMTP object and login to the server
    with smtplib.SMTP(smtp_server, smtp_port) as server:
        server.starttls()
        server.login(sender_email, password)

        # Send the message
        server.sendmail(sender_email, receiver_email, message.as_string())
except Exception as e:
    print("Error:", e)

In the example above, we first create the message using the MIMEMultipart and MIMEText classes. We then specify the sender's email address, receiver's email address, subject, and body.

Next, we try to create an instance of the SMTP class and login to the SMTP server using the starttls and login methods. If there are any exceptions, we print an error message.

Finally, we use the sendmail method to send the message, passing in the sender's email address, receiver's email address, and the message in string format.

Conclusion

In conclusion, Python provides a convenient and easy way to send emails using the smtplib module. By following the examples and guidelines above, you can start using Python to send emails using SMTP.