Last active
June 18, 2016 18:34
-
-
Save tuxnker/047391fd0b50057b7c8ba1192642c19c to your computer and use it in GitHub Desktop.
Python script to send emails to internationalized addresses rfc6531
This file contains hidden or 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 python3.5 | |
#requires python 3.5 | |
#testing rfc6531 | |
#coding: utf-8 | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from email.header import Header | |
from email import charset | |
from email.generator import Generator | |
import smtplib | |
# Example address data | |
from_address = ['Test UTF-8', '[email protected]'] | |
recipient = ['Those #!@', 'utf8-email-addrees+é@gmail.com'] | |
subject = '⌘Unicode test⌘' | |
# Example body | |
html = 'Unicode⏎\nTest⏎' | |
text = 'Unicode⏎\nTest⏎' | |
# Default encoding mode set to Quoted Printable. Acts globally! | |
charset.add_charset('utf-8', charset.QP, charset.QP, 'utf-8') | |
# 'alternative’ MIME type – HTML and plain text bundled in one e-mail message | |
msg = MIMEMultipart('alternative') | |
msg['Subject'] = "%s" % Header(subject, 'utf-8') | |
# Only descriptive part of recipient and sender shall be encoded, not the email address | |
msg['From'] = "\"%s\" <%s>" % (from_address[0], from_address[1]) | |
msg['To'] = "\"%s\" <%s>" % (Header(recipient[0], 'utf-8'), recipient[1]) | |
# Attach both parts | |
htmlpart = MIMEText(html, 'html', 'UTF-8') | |
textpart = MIMEText(text, 'plain', 'UTF-8') | |
msg.attach(htmlpart) | |
msg.attach(textpart) | |
s = smtplib.SMTP('smtpserver', 587) | |
s.set_debuglevel(2) | |
s.ehlo() | |
s.starttls() | |
s.ehlo() | |
s.login("user", "pass") | |
s.sendmail("", recipient[1], msg.as_string(),mail_options=['BODY=8BITMIME', 'SMTPUTF8'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment