Differences
This shows you the differences between two versions of the page.
python_wiki:send_email [2017/09/02 18:16] billdozor [The Code] |
python_wiki:send_email [2019/05/25 23:50] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Send Email ====== | ||
- | |||
- | **General Information** | ||
- | |||
- | Sending HTML Supported emails with Python. | ||
- | |||
- | **Checklist** | ||
- | * Multiple imports | ||
- | |||
- | ---- | ||
- | |||
- | ====== Usage ====== | ||
- | |||
- | Instructions here. | ||
- | <code bash> | ||
- | Execution code here | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ====== The Code ====== | ||
- | |||
- | <code python send-email.py> | ||
- | # | ||
- | # Import Modules | ||
- | # | ||
- | |||
- | # smtplib: send email | ||
- | import smtplib | ||
- | |||
- | # MIME: support for html messages | ||
- | from email.mime.multipart import MIMEMultipart | ||
- | from email.mime.text import MIMEText | ||
- | |||
- | |||
- | # | ||
- | # CUSTOMIZE HERE | ||
- | # | ||
- | # Send email to/from | ||
- | email_to=" | ||
- | email_from=" | ||
- | |||
- | |||
- | # | ||
- | # Functions; Main starts after | ||
- | # | ||
- | # Function: Send email | ||
- | def send_email(send_from, | ||
- | |||
- | #--DEBUG--# | ||
- | # | ||
- | # | ||
- | # | ||
- | |||
- | # Create HTML Supported MIME Type Message | ||
- | message_container = MIMEMultipart(' | ||
- | message_container[' | ||
- | message_container[' | ||
- | message_container[' | ||
- | |||
- | # Add HTML formatted Message | ||
- | message_html = MIMEText(message, | ||
- | message_container.attach(message_html) | ||
- | |||
- | # Send Email | ||
- | msg = smtplib.SMTP(' | ||
- | msg.sendmail(send_from, | ||
- | |||
- | return | ||
- | |||
- | |||
- | # | ||
- | # Main starts here | ||
- | # | ||
- | |||
- | # Variables can be built by creating strings and adding to it. Example: | ||
- | my_prebuilt_message = "" | ||
- | my_prebuilt_message += " | ||
- | my_prebuilt_message += " | ||
- | |||
- | # Create HTML formatted message | ||
- | email_html = """ | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | |||
- | <p> | ||
- | < | ||
- | <ul> | ||
- | {my_message} | ||
- | </ul> | ||
- | |||
- | <p> | ||
- | </ | ||
- | </ | ||
- | """ | ||
- | |||
- | # Variable mappings for email_html string above ( ' | ||
- | email_html_variables = {' | ||
- | |||
- | # Send Email | ||
- | print(" | ||
- | email_subject = "My Email Subject" | ||
- | send_email(email_from, | ||
- | </ | ||
- | |||
- | ---- | ||