Last active
April 24, 2024 22:24
-
-
Save ehrenfeu/aac724079ba252d0caacc286e19422e8 to your computer and use it in GitHub Desktop.
Use Python to send HTML emails with expand/collapse boxes working in TB and Android
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 | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
def send_html_mail(subject, body, to_addr, from_addr): | |
"""Send an HTML email using the given subject, body, etc.""" | |
# Create message container - the correct MIME type is multipart/alternative here! | |
message = MIMEMultipart('alternative') | |
message['subject'] = subject | |
message['To'] = to_addr | |
message['From'] = from_addr | |
message.preamble = """ | |
This message is in HTML only, which your mail reader doesn't seem to support! | |
""" | |
# Record the MIME type text/html. | |
html_body = MIMEText(body, 'html') | |
# Attach parts into message container. | |
# According to RFC 2046, the last part of a multipart message, in this case | |
# the HTML message, is best and preferred. | |
message.attach(html_body) | |
# The actual sending of the e-mail | |
# server = smtplib.SMTP('smtp.gmail.com:587') | |
server = smtplib.SMTP('localhost') | |
# Print debugging output when testing | |
if __name__ == "__main__": | |
server.set_debuglevel(1) | |
## # Credentials (if needed) for sending the mail | |
## password = "mypassword" | |
## server.starttls() | |
## server.login(from_addr,password) | |
server.sendmail(from_addr, [to_addr], message.as_string()) | |
server.quit() | |
def read_filecontent(filename): | |
with open(filename, 'r') as content_file: | |
content = content_file.read() | |
return content |
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
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<title>html title</title> | |
<style type="text/css" media="screen"> | |
.box_grey { | |
float: left; | |
margin: 15px; | |
padding: 25px; | |
border-color: #aaaaaa; | |
border-radius: 5px; | |
border-style: dotted; | |
border-width: 3px; | |
background: #cccccc; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="box_grey"> | |
<details> | |
<summary>Click the summary to expand details.</summary> | |
<p>The very exciting details!</p> | |
</details> | |
</div> | |
</body> |
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
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<title>html title</title> | |
<style type="text/css" media="screen"> | |
.answer, | |
#show, | |
#hide:target { | |
display: none; | |
} | |
#hide:target + #show, | |
#hide:target ~ .answer { | |
display: inline; | |
} | |
</style> | |
</head> | |
<body> | |
<details> | |
<summary>Click the summary to expand details.</summary> | |
<p>The very exciting details!</p> | |
</details> | |
<a href="#hide" id="hide">Show</a> | |
<a href="#show" id="show">Hide</a> | |
<div class="answer"><p>Answer</p></div> | |
</body> |
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
#!/usr/bin/env python | |
# | |
from html_mail import send_html_mail, read_filecontent | |
send_html_mail("Python HTML test email", read_filecontent('mail_body.html'), "[email protected]", "[email protected]") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment