Last active
September 14, 2020 19:48
-
-
Save encima/4f18be556f2ecd0170c5751d56dcbaac to your computer and use it in GitHub Desktop.
Test script for HTML emails in SMTPlib|-|{"files":{"smtp_test.py":{"env":"plain"}},"tag":"Python"}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import smtplib | |
import sys | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from_email = "FROM_EMAIL_HERE" | |
from_pwd = "PWD_HERE" | |
to_email = "TO_EMAIL_HERE" | |
# Set up base of image | |
msg = MIMEMultipart('html') | |
msg['Subject'] = "Test SMTPlib Message" | |
msg['From'] = from_email | |
msg['To'] = to_email | |
# Create the body of the message (a plain-text and an HTML version). | |
html = """\ | |
<html> | |
<head></head> | |
<body> | |
<p>Hola!<br> | |
<b>Wanna learn some things?</b><br> | |
Here are my <a href="https://github.com/encima/cm6111_Comp_Thinking_In_Python">slides</a>. | |
</p> | |
</body> | |
</html> | |
""" | |
msg.attach(MIMEText(html, 'html')) | |
print(msg) | |
# May want to add some error checks here | |
# Change these based on the SMTP params of your mail provider | |
mail = smtplib.SMTP('outlook.office365.com', 587) | |
mail.ehlo() | |
mail.starttls() | |
mail.login(from_email, from_pwd) | |
mail.sendmail(from_email, to_email, msg.as_string()) | |
print("email sent") | |
mail.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment