1 / 13

Sending E-mail Topic 4, Chapters 9, 10

Network Programming Kansas State University at Salina. Sending E-mail Topic 4, Chapters 9, 10. 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)

Download Presentation

Sending E-mail Topic 4, Chapters 9, 10

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Network Programming Kansas State University at Salina Sending E-mailTopic 4, Chapters 9, 10

  2. 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

  3. 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...

  4. 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()

  5. 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 K-State Online.

  6. MIME Encoders • encode_quopri Encodes the payload into quoted-printable form and sets the Content-Transfer-Encoding: header to quoted-printable7.2. This is a good encoding to use when most of your payload is normal printable data, but contains a few unprintable characters. • encode_base64 Encodes the payload into base64 form and sets the Content-Transfer Encoding: header to base64. 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 base64 encoding is that it renders the text non-human readable. • encode_7or8bit This doesn't actually modify the message's payload, but it does set the Content-Transfer-Encoding: header to either 7bit or 8bit as appropriate, based on the payload data.

  7. 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.

  8. 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.

  9. 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: address1, address2,… DATA -- Send the message as a string – Blank line followed by single period to indicate end of data QUIT

  10. 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 SMTP (continued) 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()

  11. 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

  12. 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.

  13. 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

More Related