Here we will create an example to send text message using RFC 822 in Python 3 programming language. We have seen how to send simple text email using gmail SMTP server in Python programming language. In this example we will also send the text message using RFC 822 standard, because RFC 822 standard does not support special encoding for the message. Here we will use both TLS and SSL ports to send the message.
RFC 822 is a standard for ARPA Internet Text Messages. RFC 822 standard is where messages consist of lines of text. No special provisions are made for encoding, facsimile, speech, or structured text. A general “memo” framework is used. That is, a message con-sists of some information in a rigid format, followed by the main part of the message. The syntax of several fields of the rigidly-formated (“headers”) section is defined in this specification; some of these fields must be included in all messages.
For parsing the fields from the RFC 822 headers, a parser class from Python programming language is used.
RFC 822 Message Format
An example for RFC 822 message format can be given here:
'From: Foo Bar <[email protected]>\n' 'To: <[email protected]>\n' 'Subject: Test message\n' '\n' 'Body would go here\n'
From specifies the from email address with name as Foo Bar.
To specifies to email address.
Subject is the subject of the message followed by message of the text mail.
Python 3.8.0, Gmail Security Settings, Gmail SMTP Server – gmail.smtp.com, Gmail SMTP Port – 587, 465
Creating Python Script
We will create below Python script called text_email_headers.py. You can choose any location to place this file. Make sure your Python is accessible from anywhere using command prompt.
import smtplib from email.parser import BytesParser, Parser from email.policy import default headers = Parser(policy=default).parsestr( 'From: Foo Bar <[email protected]>\n' 'To: <[email protected]>\n' 'Subject: Simple Text Message\n' '\n' 'Email sending example using Python. It\'s Simple Text Message\n') s = smtplib.SMTP('smtp.gmail.com', 587) s.starttls() s.login('[email protected]', 'gmail\'s password') s.send_message(headers) s.quit()
In the above Python script, we first import
smtplib for sending the actual email.
The Parser API is most useful if you have the entire text of the message in memory, or if the entire message lives in a file on the file system.
The email package’s prime focus is the handling of email messages as described by the various email and MIME RFCs. However, the general format of email messages (a block of header fields each consisting of a name followed by a colon followed by a value, the whole block followed by a blank line and an arbitrary ‘body’), is a format that has found utility outside of the realm of email. Some of these uses conform fairly closely to the main email RFCs, some do not.
- How to send simple text message using Python
- How to send HTML message using Python
- How to send attachments in email message using Python
Policy objects give the email package the flexibility to handle all these disparate use cases.
In the above example we have included everything (email addresses, subject and message) in RFC 822 format.
Next we create a session using Gmail SMTP server –
smtp.gmail.com and port –
We need to issue
STARTTLS command otherwise you will get the following error:
#smtplib.SMTPSenderRefused: (530, b'5.7.0 Must issue a STARTTLS command first. p7sm24605501pfn.14 - gsmtp', '[email protected]')
You will get below error if your security level is high:
#smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials x10sm26098036pfn.36 - gsmtp')
Therefore you need to lower your Gmail’s security settings. This is required if you are using Gmail SMTP server using your gmail address to send the email.
Using SSL Port 465
If you want to use SSL port 465 to establish the secure connection with Gmail SMTP server then do the following changes:
s = smtplib.SMTP_SSL('smtp.gmail.com', 587) by
s = smtplib.SMTP_SSL('smtp.gmail.com', 465).
Testing the Email Sending
Just executing the file text_email_headers.py using command line tool will send the email.
Once your email successfully sent you will see the following output in Gmail Inbox:
As I have sent from my gmail address to my gmail address, so the from email address you see as me in the above image.
When you open the message you will see below output as shown in the image:
In the above image you can see the name has been included for the from email address as highlighted.
That’s all. Hope you got an idea how to send simple text message using RFC 822 standard using Python 3.
You can also use your own SMTP server and port to send email using Python.
Thanks for reading.