7-bit ASCII (128 characters)
- Works for English text only messages
- Attachments require special handling
Variable headers: To, From, Subject, Date and Message-ID are the normal minimum. Others are allowed and may be added by servers that handle the message.
MIME – introduced in 1992
MIME stands for Multipurpose Internet Mail Extensions and in comparison to networking and E-mail itself, it is relatively new. MIME defines the mechanisms to encode binary or Unicode data into ASCII and embed the data in an E-mail message so that it can so that it can be sent using SMTP.
Before MIME was widely available, most people either did not send binary attachments in E-mail messages, or they used a couple tools outside of their E-mail system to handle the attachments. uuencode (uu = UNIX to UNIX) is a program that could be used to convert binary data in a file, to a much larger file on ASCII data. As the name implies, it came from UNIX, but was also available for other systems such as MS-DOS. One would encode a file as following:
$ uuencode some_file > encoded_file
If you would examine the encoded file, you would find a line similar to the following:
begin 0664 some_file
In this example, we are saying the file’s name was some_file and its file permissions in UNIX octal notation, were 0664. After the begin line would be a large number of lines contained what appeared to be random characters. At the end of the file, there would be a line, which just said: end.
This encoded file could then be read into an E-mail message and sent to someone else. The recipient should save the whole E-mail message to a temporary file and then run use the uudecode command as follows:
$ uudecode tempfile
The result of the above command would be a new file in the recipient’s current working directory named some_file with file permissions of 0664 (-rw-rw-r–), just as exists on the sender’s computer.
Multiple attachments
- Divide the message into parts
- Each part has headers including the type of data, filename, encoding used and data encoded to 7-bit ASCII
Content types
Styled text with different fonts and colors
Interactive multimedia
Multi-language support for non-Roman languages like Japanese, Chinese, Arabic, Hebrew, etc...
Note, discussion of smtplib module follows later, here we discuss just the generation of the e-mail message.
Most of the e-mail tools are contained in submodules of the email module, rather than the email module.
Constructor for a simple text MIME e-mail object. The returned object may be used as a dictionary to add the mail headers.
| Parameter: | message (string) – The content of a text only e-mail message |
|---|---|
| Return type: | Dictionary like e-mail object. |
Inherited function from email.LazyImporter. Returns an e-mail message string as specified by RFC 2822.
| Return type: | string |
|---|
>>> from email.MIMEText import MIMEText
>>> msg = MIMEText("hi")
>>> msg.as_string()
'Content-Type: text/plain; charset="us-ascii"\nMIME-Version:
1.0\nContent-Transfer-Encoding: 7bit\n\nhi'
Returns a date string as specified by RFC 2822, e.g.:
Fri, 09 Nov 2001 01:08:47 -0000
| Parameters: |
|
|---|---|
| Return type: | string |
from email.MIMEText import MIMEText
from email import Utils
import smtplib
message = """Hello,
This is a test message.
-- Tim"""
msg = MIMEText(message)
msg['To'] = 'you@home'
msg['From'] = 'me@school'
msg['Subject'] = 'Testing Python e-mail'
msg['Date'] = Utils.formatdate(localtime = 1)
msg['Message-ID'] = Utils.make_msgid()
server = 'localhost:8025'
s = smtplib.SMTP(server)
s.sendmail(msg['From'], [msg['To']], msg.as_string())
s.close()
Attach a MIME e-mail object to the message
Start with a MIMEMultipart object
Create MIMEText objects for the message body and any text attachments; attach to the message
For binary file attachments, create other MIME objects, encode the data for each; attach to the message.
- MIMEBase – generic binary file
- MIMEAudio audio file
- MIMEImage graphic image file
See example code from The Text Book (mime_gen_basic.py)
Like email.MIMEMultipart, except the e-mail client program is to display one part or the other (text or HTML), but not both. Multiple parts of the message are used to hold the same content in different formats.
No Content-Disposition header is inserted
- Content-Disposition header is used to give the reader program some clues of what to do with the file, such as a default file name for saving it.
- Mime Alternatives are for viewing, so file handling is not needed.
For the Network Programming Class, the students may skip this section of The Text Book. We are more concerned with sending messages, which is more common for non-interactive programs to do than retrieving and parsing messages.
Simple Mail Transport Protocol
Unlike HTTP, SMTP is a connection oriented protocol
- Multiple messages exchanged between client and server to set who
the message is to and from and to send the data.
Normal conversation is:
HELO {domain_name} MAIL From: {address} RCPT To: {address1, address2, ...} DATA {-- Send the message as a string --} *Blank line followed by a single period to indicate end of data* QUIT
Always send data as a string
See smtpSocket.py for a demonstration of sending a simple SMTP message between client and server using sockets directly.
See demo video of running SMTPSocket.py with Fake mail
Make a connection to a SMTP server. Returns an object, which can be used to send a message.
| Parameter: | server (string) – The server to connect to. If the SMTP server is not listening on port 25, put a colon after the server name and then list the port number after the colon. |
|---|---|
| Return type: | object |
Send an e-mail message
Download the following example: smtplibTest.py
import smtplib
message = """Hi everyone,
-- Tim"""
msg = MIMEText(message)
msg['To'] = 'you@home'
msg['From'] = 'me@school'
msg['Subject'] = 'Testing Python e-mail'
msg['Date'] = Utils.formatdate(localtime = 1)
msg['Message-ID'] = Utils.make_msgid()
s = smtplib.SMTP('localhost:8025')
s.sendmail(msg['From'], [msg['To']], msg.as_string())
s.close()
Download it from here (fakemail.py)
- Found with package index on python.org [PYPI]
- Author: Graham Ashton [ASHTON05]
- Fakemail Project Page
Creates a SMTP server that you can send messages to for testing much better for debugging than sending to a real server.
Recommend to run from Command Prompt:
C:\tmp\> python fakemail.py
Use Control-c (if that does not work in Windows, use Control-Break) to stop the fakemail server. Be patient, the Windows command prompt is very slow to send signals to processes. It is immediate in Unix.
Defaults to listen on port 8025 (Real SMTP port is 25)
Saves messages to files in same directory
Change file name extension to ‘.eml’ to view the message with your e-mail client.