Python Send Email: Simple Guide with Example Code
Here is an example of how to send an email using Python:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Set up sender and receiver email addresses
sender_email = 'sender@example.com'
receiver_email = 'receiver@example.com'
# Create message object
message = MIMEMultipart()
message['From'] = sender_email
message['To'] = receiver_email
message['Subject'] = 'Test email'
# Add body to message object
body = 'This is a test email from Python.'
message.attach(MIMEText(body, 'plain'))
# Set up SMTP server and send message
smtp_server = smtplib.SMTP('smtp.gmail.com', 587)
smtp_server.starttls()
smtp_server.login(sender_email, 'password') # replace 'password' with your actual password
smtp_server.sendmail(sender_email, receiver_email, message.as_string())
smtp_server.quit()
Make sure to replace the sender_email and receiver_email variables with your actual email addresses, and update the smtp_server variable with the correct SMTP server for your email provider. Also replace the 'password' string with your actual email password.
原文地址: https://www.cveoy.top/t/topic/nkja 著作权归作者所有。请勿转载和采集!