Sending Test Emails With Python's Smtplib Module
Sending emails with Python: Use smtplib to send HTML emails via SMTP servers. Define a function `send_email` to construct and send email messages.
from email.mime.text import MIMEText
import smtplib
def send_email(server, recipient, subject, body, sender_name, sender_email):
msg = MIMEText(body, 'html')
msg['Subject'] = subject
msg['From'] = f"{sender_name} <{sender}>"
msg['To'] = recipient
msg['X-Mailer'] = f"{sender_name}"
try:
server.sendmail(sender, recipient, msg.as_string())
print("Email sent to sender: " + recipient)
except Exception as e:
print("Email failed to send to sender: " + recipient)
print(e)
smtp_host = 'smtp service host'
smtp_port = port
smtp_username = 'usern...