The Python3 programming language is quite versatile and powerful, sending email with Python3 is just as easy and enjoyable of a walk in the programming languages park. If you are using a linux email server, the postfix email program should be configured with the following statement in the main.cf text config file.

On Ubuntu, Debian, Linux Mint (apt packaging variations of the Linux OS)

sudo nano /etc/postfix/main.cf

There should be the following line, or add it to the bottom of the file.

smtputf8_enable = yes

Then, sudo postfix restart (or postfix reload)

Now, Python3 is ready to send Secure UTF8 email.

Here is some |337 code. Please send a comment, perhaps thanks would suffice.

#!/usr/bin/python3
import smtplib
import sys
from email.message import EmailMessage
# =========
# email.py
# =========
# define a variable, e.g.,  EMAIL_LOGIN = 'username@domain_name.us'
# define a variable, e.g.,  EMAIL_PASSWORD = 'password_to_email_server'
with open('/path_to_email_credentials/email.py') as f: exec(f.read())
email = EmailMessage()
SUBJECT = 'This is the subject of the email.'
MESSAGE = 'Some message body of the email, how are you?  Send utf8 or emojies ✅'
email['Subject'] = SUBJECT
email.set_content(MESSAGE)
email['From'] = 'user1@domain_name1'
email['To'] = 'user2@domain_name2'
SMTP_SERVER = 'mail.domain_name.us'
with smtplib.SMTP(SMTP_SERVER, port=587) as secure_send:
    secure_send.ehlo()
    secure_send.starttls()
    secure_send.login(user=EMAIL_LOGIN, password=EMAIL_PASSWORD)
    secure_send.send_message(email)

This will send a utf-8 encoded email via a TLS encrypted email. There is another way to send secure email via SSL, I’ll leave that to a topcoder.cloud reader exercise to do, or please post it in the comments.

By admin

One thought on “How to Send UTF8 Email that is Secure with Python3”
  1. This is the SSH version of Python3 to send secure email.

    #!/usr/bin/python3

    import smtplib
    import sys
    from email.message import EmailMessage
    \n
    # get from secured location file, or input from getpass
    # EMAIL_LOGIN
    # EMAIL_PASSWORD
    \n
    msg = EmailMessage()
    SUBJECT = ‘this is the subject data’
    MESSAGE = ‘this is the content of the email’
    msg[‘Subject’] = SUBJECT
    msg[‘From’] = ‘the_from_email_addr@email1111.com’
    msg[‘To’] = ‘the_to_email_addr@email1234.com’
    msg.set_content(MESSAGE)
    \n
    smtp_server = ‘mail.server456789.com’
    with smtplib.SMTP_SSL(smtp_server, 465) as email:
    \t email.ehlo()
    \t email.login(EMAIL_LOGIN, EMAIL_PASSWORD)
    \t #email.sendmail(msg[‘From’],msg[‘To’],msg.as_string())
    \t email.send_message(msg)

Leave a Reply

Your email address will not be published. Required fields are marked *