Sending Email Topic 4 Chapters 9 10 Network

  • Slides: 13
Download presentation
Sending E-mail Topic 4, Chapters 9, 10 Network Programming Kansas State University at Salina

Sending E-mail Topic 4, Chapters 9, 10 Network Programming Kansas State University at Salina

E-mail messages 7 -bit ASCII (128 characters) Works for English text only messages Initial

E-mail messages 7 -bit ASCII (128 characters) Works for English text only messages Initial binary file solution – uuencode and uudecode (uu = UNIX to UNIX) 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 – 1992 Multipurpose Internet Mail Extensions Divide the message into parts Each part has headers including the type of data, filename, encoding used and data encoded to 7 -bit ASCII

MIME advantages Multiple attachments Content types Styled text with different fonts and colors Interactive

MIME advantages Multiple attachments Content types Styled text with different fonts and colors Interactive multimedia Multi-language support for non-Roman languages like Japanese, Chinese, Arabic, Hebrew, etc. . .

Building simple messages in Python from email. MIMEText import MIMEText from email import Utils

Building simple messages in Python 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()

Multipart MIME messages Start with MIMEMultipart() object Create MIMEText() objects for body and any

Multipart MIME messages Start with MIMEMultipart() object Create MIMEText() objects for 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 in book and posted on KState Online.

MIME Encoders encode_quopri Encodes the payload into quoted-printable form and sets the Content-Transfer-Encoding: header

MIME Encoders encode_quopri Encodes the payload into quoted-printable form and sets the Content-Transfer-Encoding: header to quotedprintable 7. 2. This is a good encoding to use when most of your payload is normal printable data, but contains a few unprintable characters. encode_base 64 Encodes the payload into base 64 form and sets the Content-Transfer Encoding: header to base 64. This is a good encoding to use when most of your payload is unprintable data since it is a more compact form than quoted-printable. The drawback of base 64 encoding is that it renders the text non-human readable. encode_7 or 8 bit This doesn't actually modify the message's payload, but it does set the Content-Transfer-Encoding: header to either 7 bit or 8 bit as appropriate, based on the payload data.

MIME Alternatives Like MIMEMultipart, except the e-mail is to display either one part or

MIME Alternatives Like MIMEMultipart, except the e-mail is to display either one part or the other (text or HTML). Multiple parts are to hold the same content. 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.

Parsing E-mail messages You may skip this part More concerned with sending messages, which

Parsing E-mail messages You may skip this part More concerned with sending messages, which is more common for non-interactive programs to do than retrieving and parsing messages.

SMTP Simple Mail Transport Protocol Unlike HTTP, SMTP is a connection oriented protocol Multiple

SMTP 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: address 1, address 2, … DATA -- Send the message as a string – Blank line followed by single period to indicate end of data QUIT

SMTP (continued) See SMTPSocket. py – demonstration of simple SMTP messages between client and

SMTP (continued) See SMTPSocket. py – demonstration of simple SMTP messages between client and server. See demo video of running SMTPSocket. py smtplib – very easy sending of e-mail Always send data as a string 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()

Fake mail Server Fakemail. py – Download it from K-State Online Found with package

Fake mail Server Fakemail. py – Download it from K-State Online Found with package index on python. org 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 python fakemail. py

Fake mail server (continued) Use cntrl-Break to stop the fakemail server. Be patient, Windows

Fake mail server (continued) Use cntrl-Break to stop the fakemail server. Be patient, Windows is very slow to send signals to processes. It is immediate in Unix. Microsoft has never been able to implement signals correctly. Listens on port 8025 (Real SMTP port is 25) Saves messages to files in same directory Change file name extension to ‘eml’ to easily view the message with your e-mail client.

ESMTP Extended Simple Mail Transfer Protocol Newer version of SMTP Supported by some servers

ESMTP Extended Simple Mail Transfer Protocol Newer version of SMTP Supported by some servers Allows clients to get information, such as maximum allowed message size, from the server prior to sending the message Allows for encryption of the message Allows for a login before accepting the message Protocol session begins with ‘EHLO’ instead of ‘HELO’, which can be used to test if the server uses ESMTP or SMTP