Universal Acceptance (UA) Micro-Learning Module: Module 9- Programming in Email Address Internationalization(EAI) Using Python.
Instructor Guide
1st Edition.
���© 2024 Creative Commons License - Attribution 4.0 International (CC BY 4.0).�
Universal Acceptance
Programming in EAI UA Micro-Learning Module Objectives.
| 2
Note About the Utilization of Unicode String Literals :
| 3
EAI programming: Handling Non-ASCII Characters in Emails.
| 4
EAI Programming: Handling Subject and Header Lines with UTF-8/Non-ASCII Characters(1/2):
Example1: MIME Encoded-Word Syntax Representing the Arabic Word “Marhaba”:
In the example above:
=?UTF-8?Q?مرحبا?=
| 5
EAI Programming: Handling Subject and Header Lines with UTF-8/Non-ASCII Characters(2/2):�
| 6
Example: Code Snippet for Handling Subject Line with UTF-8/non-ASCII Characters in Python:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.utils import formataddr
# Define sender and recipient email addresses
sender_email = 'your_sender_email@example.com' # Replace with your sender's email address
recipient_email = 'recipient_email@example.com' # Replace with the recipient's email address
# Create a MIME message
msg = MIMEMultipart('alternative')
# Set the subject line with non-ASCII characters
subject = 'Subject with Non-ASCII Characters: 你好, Hello'
msg['Subject'] = Header(subject, 'utf-8').encode()
| 7
EAI Programming: Handling Email Body with UTF-8/Non-ASCII Characters(½): �
message['Content-Type'] = 'text/plain; charset=utf-8'
email_body = '你好, Hello'
encoded_body = email_body.encode('utf-8')
| 8
EAI Programming: Handling Email Body with UTF-8/Non-ASCII Characters(2/2): �
from email.mime.text import MIMEText
text_part = MIMEText(encoded_body, 'plain', 'utf-8')
message.attach(text_part)
import smtplib
with smtplib.SMTP('smtp.example.com') as server: # Replace with your SMTP server details
server.send_message(message)
| 9
EAI Programming: Full Python Code for Handling Subject Line, Header Line and Email Body with UTF-8/Non-ASCII Characters (1/2):
import smtplib
from email.mime.text import MIMEText
# Define sender and recipient email addresses
sender_email = 'your_sender_email@example.com' # Replace with your sender's email address
recipient_email = 'recipient_email@example.com' # Replace with the recipient's email address
# Create a MIMEText message with internationalized text
message_text = """
Hello,
This is an example of internationalized email body text.
It includes non-ASCII characters like مرحبًا,ሀሎ,ආයුබෝවන්, 你好
(Arabic, Ethiopic, Sinhala and Chinese for "Hello").
Feel free to add more internationalized content as you like.
Best regards,
Your Name
"""
| 10
EAI Programming: Full Python Code for Handling Subject Line, Header Line and Email Body with UTF-8/Non-ASCII Characters (2/2):
msg = MIMEText(message_text, 'plain', 'utf-8')
# Establish an SMTP connection and send the email
smtp_server = 'your_smtp_server.com' # Replace with your SMTP server address
smtp_port = 587 # Replace with the appropriate port
smtp_username = 'your_smtp_username' # Replace with your SMTP username
smtp_password = 'your_smtp_password' # Replace with your SMTP password
msg['From'] = sender_email
msg['To'] = recipient_email
msg['Subject'] = 'Internationalized Email Body Text'
try:
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(smtp_username, smtp_password)
server.sendmail(sender_email, [recipient_email], msg.as_string())
server.quit()
print("Email sent successfully.")
except Exception as e:
print("Email sending failed:", str(e))
| 11
EAI Programming: Handling Attachments with UTF-8/Non-ASCII Characters:
| 12
Example: Code Snippets for Parts of the Python code for Handling Attachments in UTF-8:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
# Defining Email Configuration
sender_email = 'تجربة-بريد-الكتروني@تجربة-القبول-الشامل.موريتانيا'
recipient_email = '电子邮件测试@普遍适用测试.我爱你'
message = MIMEMultipart()
| 13
Example: Code Snippets for Parts of the Python code for Handling Attachments in UTF-8- Composing Email Content:
# Composing Email Content
message["From"] = sender_email
message["To"] = recipient_email
message["Subject"] = "Email with Attachment"
message_text = "Hello,\n\n" \
"Please find the attached file.\n\n" \
"Best regards,\n" \
"Your Name\n"
message.attach(MIMEText(message_text, "plain"))
attachment_file = "path/to/attachment"
attachment = open(attachment_file, "rb")
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header(
"Content-Disposition", f"attachment; filename=attachment_name; filename*=UTF-8''{attachment_file}",)
message.attach(part)
| 14
Example: Code Snippets for Parts of the Python code for Handling Attachments in UTF-8 – Sending Email with Attachment.
# Sending Email with Attachment
smtp_server = "your_smtp_server.com"
smtp_port = 587
smtp_username = "your_smtp_username"
smtp_password = "your_smtp_password"
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(smtp_username, smtp_password)
server.sendmail(sender_email, recipient_email, message.as_string())
print("Email sent successfully.")
| 15
Example: Full Code for Handling Attachments with UTF-8/Non-ASCII Characters in Python(1/3):
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
def send_email_with_attachment():
# Define sender and recipient email addresses
sender_email = 'تجربة-بريد-الكتروني@تجربة-القبول-الشامل.موريتانيا' # Replace with your sender's email address
recipient_email = '电子邮件测试@普遍适用测试.我爱你' # Replace with the recipient's email address
# Create a MIMEMultipart object
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = recipient_email
message["Subject"] = "Email with Attachment"
| 16
Example: Full Code for Handling Attachments with UTF-8/Non-ASCII Characters in Python(2/3):
# Add email text
message_text = "Hello,\n\n" \
"Please find the attached file.\n\n" \
"Best regards,\n" \
"Your Name\n"
message.attach(MIMEText(message_text, "plain"))
# Add attachment
attachment_file = "path/to/attachment" # Replace with the path to your attachment file
attachment = open(attachment_file, "rb")
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
f"attachment; filename=attachment_name; filename*=UTF-8''{attachment_file}",
)
message.attach(part)
| 17
Example: Full Code for Handling Attachments with UTF-8/Non-ASCII Characters in Python(3/3):
# Connect to the SMTP server and send the email
smtp_server = "your_smtp_server.com" # Replace with your SMTP server address
smtp_port = 587 # Replace with the appropriate port
smtp_username = "your_smtp_username" # Replace with your SMTP username
smtp_password = "your_smtp_password" # Replace with your SMTP password
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(smtp_username, smtp_password)
server.sendmail(sender_email, recipient_email, message.as_string())
print("Email sent successfully.")
# Call the function to send the email
send_email_with_attachment()
| 18
Example: Handling Attachments with UTF-8/Non-ASCII Filename in Python:
import email.utils
def handle_attachment(filename):
# Encode the filename using UTF-8
encoded_filename = filename.encode('utf-8')
# Set the Content-Disposition header with the encoded filename
content_disposition = f'attachment; filename="{encoded_filename.decode()}"'
# Create the email attachment object
attachment = email.mime.base.MIMEBase('application', 'octet-stream')
attachment.set_payload(open(filename, 'rb').read())
# Set the Content-Disposition header for the attachment
attachment['Content-Disposition'] = content_disposition
# Encode the attachment as base64
email.encoders.encode_base64(attachment)
return attachment
# Example usage
attachment_filename = "文件名.pdf" # Non-ASCII filename
attachment = handle_attachment(attachment_filename)
| 19
Handling SMTP Interactions with EAI Support: Libraries and APIs:
| 20
Example: Sending Emails with EAI Address with Python's smtplib and IDNA(1/4):
import smtplib
from email.mime.text import MIMEText
from email.header import Header
| 21
Example: Sending Emails with EAI Address with Python's smtplib and IDNA(2/4):
#Define EAI Addresses and Message
def send_multilingual_email(): # Define sender and recipient email addresses
sender_email = "تجربة-بريد-الكتروني@example.com"
recipient_email = "电子邮件测试@example.com"
# Create the email message
message = MIMEText("مرحبًا,ሀሎ,ආයුබෝවන්", "plain", "utf-8")
message["From"] = Header("Sender Name", "utf-8")
message["To"] = Header("Recipient Name", "utf-8")
message["Subject"] = Header("Multilingual Email", "utf-8")
| 22
Example: Sending Emails with EAI Address with Python's smtplib and IDNA(3/4):
#Connect to the SMTP Server and Send the Email
smtp_server = "your_smtp_server.com"
smtp_port = 587
smtp_username = "your_smtp_username"
smtp_password = "your_smtp_password"
try:
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(smtp_username, smtp_password)
# Encode email addresses using IDNA (Internationalized Domain Names in Applications)
sender_email_encoded = sender_email.encode("idna").decode("utf-8")
recipient_email_encoded = recipient_email.encode("idna").decode("utf-8")
| 23
Example: Sending Emails with EAI Address with Python's smtplib and IDNA(4/4):
# Send the email
server.sendmail(sender_email_encoded, recipient_email_encoded, message.as_string())
print("Email sent successfully.")
except (smtplib.SMTPException, socket.gaierror) as e:
print(f"Error sending email: {str(e)}")
try:
send_multilingual_email()
except Exception as e:
print(f"An error occurred: {str(e)}")
| 24
Ensuring Email Delivery Across Languages: Internationalized Email Address Validation with Confirmation Links:
| 25
Ensuring Email Delivery Across Languages: Internationalized Email Address Validation with Confirmation Links(1/4):
user_email = '你好@example.com'
confirmation_token = 'unique_token_generated_for_user'
| 26
Ensuring Email Delivery Across Languages: Internationalized Email Address Validation with Confirmation Links(2/4):
import smtplib
from email.mime.text import MIMEText
# Create an email message
msg = MIMEText(f"Click the following link to confirm your email address:
http://example.com/confirm?token={confirmation_token}", 'plain', 'utf-8')
msg['Subject'] = 'Confirm Your Email Address'
msg['From'] = 'noreply@example.com'
msg['To'] = user_email
# Send the email using an SMTP server
smtp_server = 'your_smtp_server.com'
smtp_port = 587
smtp_username = 'your_smtp_username'
smtp_password = 'your_smtp_password'
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(smtp_username, smtp_password)
server.sendmail('noreply@example.com', [user_email], msg.as_string())
server.quit()
| 27
Ensuring Email Delivery Across Languages: Internationalized Email Address Validation with Confirmation Links(3/4):
database = {
user_email: confirmation_token
}
clicked_token = 'unique_token_generated_for_user'
# Verify the clicked token against the stored token
if clicked_token == database.get(user_email):
print("Email address confirmed.")
else:
print("Confirmation link is invalid.")
database = {
user_email: confirmation_token
}
| 28
Ensuring Email Delivery Across Languages: Internationalized Email Address Validation with Confirmation Links(4/4):
| 29
Understanding Multilingual Mailboxes: Parsing Internationalized Email Addresses:
| 30
Example: Validating Email Addresses Length Using Python.
import re
def is_valid_email_address(email):
try:
# Validate email address using regular expression
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
if not re.match(pattern, email):
return False
# Check length validation
return len(email) <= 254 and len(email.split('@')[0]) <= 64 and len(email.split('@')[1]) <= 255
except Exception:
return False
# Test the validation
email = 'تجربة-بريد-الكتروني@تجربة-القبول-الشامل.موريتاني'
is_valid = is_valid_email_address(email)
print(f"Is valid email address: {is_valid}")
Is valid email address: True
| 31
EAI-Specific Considerations for Setting Up Email Mailbox Names:�
| 32
Reference:
[1]. Freed, N., & Borenstein, N. (1996). Multipurpose Internet Mail Extensions (MIME) Part 3: Message Header Extensions for Non-ASCII Text (RFC 2047). Retrieved from [https://www.ietf.org/] on 2023-12-11.
[2]. Klensin, J., & Hildebrand, M. (2008). Internationalized Email Headers (RFC 5335). Retrieved from [https://datatracker.ietf.org/wg/tewg/documents/] on 2023-12-11.
[3]. The Unicode Consortium. (n.d.). Retrieved from https://unicode.org/consortium/] on 2023-12-11.
[4]. Freed, N. (1996). Multipurpose Internet Mail Extensions (MIME) Part 5: Base64 Content-Transfer-Encoding (RFC 2048). Retrieved from http://tools.ietf.org/html/rfc2048: http://tools.ietf.org/html/rfc2048 on 2023-12-11.
[5]. Python, Chardet. (n.d.). Retrieved from https://github.com/chardet/chardet on 2023-12-11.
[6]. Python, Idna. (n.d.). Retrieved from https://pypi.org/project/idna/
[7]. Java, Java Platform, Standard Edition 7 API Specification. (n.d.). Charset class. Retrieved from https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html on 2023-12-11..
[8]. Hoffman, P., & Dürst, M. (2010, July). Internationalized Domain Names in Applications (IDNA2008) (RFC 5893). Retrieved from https://www.ietf.org/ on on 2023-12-11.
[9]. Python Documentation, (n.d.):https://docs.python.org/: https://docs.python.org/, on 2023-12-11.
| 33
Reference:
[10]. JavaMail, (nd): https://www.oracle.com/java/technologies/javamail-api.html, on 2023-12-11.
[11]. Python, smtplib module, (n.d.): https://docs.python.org/3/library/smtplib.html, on 2023-12-11.
[12]. (Stack Exchange, 2023). What's the best approach to confirm user email address - sending an email-confirm link? Retrieved from https://ux.stackexchange.com/questions/111252/from-a-ux-perspective-when-should-you-send-the-confirm-your-email-message on December 11, 2023.
| 34
Author:
| 35