<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://lms.onnocenter.or.id/wiki/index.php?action=history&amp;feed=atom&amp;title=Python%3A_email</id>
	<title>Python: email - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://lms.onnocenter.or.id/wiki/index.php?action=history&amp;feed=atom&amp;title=Python%3A_email"/>
	<link rel="alternate" type="text/html" href="https://lms.onnocenter.or.id/wiki/index.php?title=Python:_email&amp;action=history"/>
	<updated>2026-04-20T12:21:08Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://lms.onnocenter.or.id/wiki/index.php?title=Python:_email&amp;diff=45202&amp;oldid=prev</id>
		<title>Onnowpurbo at 02:28, 3 December 2015</title>
		<link rel="alternate" type="text/html" href="https://lms.onnocenter.or.id/wiki/index.php?title=Python:_email&amp;diff=45202&amp;oldid=prev"/>
		<updated>2015-12-03T02:28:39Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;https://lms.onnocenter.or.id/wiki/index.php?title=Python:_email&amp;amp;diff=45202&amp;amp;oldid=45201&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>Onnowpurbo</name></author>
	</entry>
	<entry>
		<id>https://lms.onnocenter.or.id/wiki/index.php?title=Python:_email&amp;diff=45201&amp;oldid=prev</id>
		<title>Onnowpurbo: New page: Sumber: https://docs.python.org/2/library/email-examples.html   Here are a few examples of how to use the email package to read, write, and send simple email messages, as well as more comp...</title>
		<link rel="alternate" type="text/html" href="https://lms.onnocenter.or.id/wiki/index.php?title=Python:_email&amp;diff=45201&amp;oldid=prev"/>
		<updated>2015-12-03T02:22:42Z</updated>

		<summary type="html">&lt;p&gt;New page: Sumber: https://docs.python.org/2/library/email-examples.html   Here are a few examples of how to use the email package to read, write, and send simple email messages, as well as more comp...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Sumber: https://docs.python.org/2/library/email-examples.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here are a few examples of how to use the email package to read, write, and send simple email messages, as well as more complex MIME messages.&lt;br /&gt;
&lt;br /&gt;
First, let’s see how to create and send a simple text message:&lt;br /&gt;
&lt;br /&gt;
 # Import smtplib for the actual sending function&lt;br /&gt;
 import smtplib&lt;br /&gt;
 &lt;br /&gt;
 # Import the email modules we&amp;#039;ll need&lt;br /&gt;
 from email.mime.text import MIMEText&lt;br /&gt;
 &lt;br /&gt;
 # Open a plain text file for reading.  For this example, assume that&lt;br /&gt;
 # the text file contains only ASCII characters.&lt;br /&gt;
 fp = open(textfile, &amp;#039;rb&amp;#039;)&lt;br /&gt;
 # Create a text/plain message&lt;br /&gt;
 msg = MIMEText(fp.read())&lt;br /&gt;
 fp.close()&lt;br /&gt;
 &lt;br /&gt;
 # me == the sender&amp;#039;s email address&lt;br /&gt;
 # you == the recipient&amp;#039;s email address&lt;br /&gt;
 msg[&amp;#039;Subject&amp;#039;] = &amp;#039;The contents of %s&amp;#039; % textfile&lt;br /&gt;
 msg[&amp;#039;From&amp;#039;] = me&lt;br /&gt;
 msg[&amp;#039;To&amp;#039;] = you&lt;br /&gt;
 &lt;br /&gt;
 # Send the message via our own SMTP server, but don&amp;#039;t include the&lt;br /&gt;
 # envelope header.&lt;br /&gt;
 s = smtplib.SMTP(&amp;#039;localhost&amp;#039;)&lt;br /&gt;
 s.sendmail(me, [you], msg.as_string())&lt;br /&gt;
 s.quit()&lt;br /&gt;
&lt;br /&gt;
And parsing RFC822 headers can easily be done by the parse(filename) or parsestr(message_as_string) methods of the Parser() class:&lt;br /&gt;
&lt;br /&gt;
 # Import the email modules we&amp;#039;ll need&lt;br /&gt;
 from email.parser import Parser&lt;br /&gt;
 &lt;br /&gt;
 #  If the e-mail headers are in a file, uncomment this line:&lt;br /&gt;
 #headers = Parser().parse(open(messagefile, &amp;#039;r&amp;#039;)) &lt;br /&gt;
 &lt;br /&gt;
 #  Or for parsing headers in a string, use:&lt;br /&gt;
 headers = Parser().parsestr(&amp;#039;From: &amp;lt;user@example.com&amp;gt;\n&amp;#039;&lt;br /&gt;
         &amp;#039;To: &amp;lt;someone_else@example.com&amp;gt;\n&amp;#039;&lt;br /&gt;
         &amp;#039;Subject: Test message\n&amp;#039;&lt;br /&gt;
         &amp;#039;\n&amp;#039;&lt;br /&gt;
         &amp;#039;Body would go here\n&amp;#039;) &lt;br /&gt;
 &lt;br /&gt;
 #  Now the header items can be accessed as a dictionary:&lt;br /&gt;
 print &amp;#039;To: %s&amp;#039; % headers[&amp;#039;to&amp;#039;]&lt;br /&gt;
 print &amp;#039;From: %s&amp;#039; % headers[&amp;#039;from&amp;#039;]&lt;br /&gt;
 print &amp;#039;Subject: %s&amp;#039; % headers[&amp;#039;subject&amp;#039;]&lt;br /&gt;
 &lt;br /&gt;
Here’s an example of how to send a MIME message containing a bunch of family pictures that may be residing in a directory:&lt;br /&gt;
&lt;br /&gt;
# Import smtplib for the actual sending function&lt;br /&gt;
import smtplib&lt;br /&gt;
&lt;br /&gt;
# Here are the email package modules we&amp;#039;ll need&lt;br /&gt;
from email.mime.image import MIMEImage&lt;br /&gt;
from email.mime.multipart import MIMEMultipart&lt;br /&gt;
&lt;br /&gt;
COMMASPACE = &amp;#039;, &amp;#039;&lt;br /&gt;
&lt;br /&gt;
# Create the container (outer) email message.&lt;br /&gt;
msg = MIMEMultipart()&lt;br /&gt;
msg[&amp;#039;Subject&amp;#039;] = &amp;#039;Our family reunion&amp;#039;&lt;br /&gt;
# me == the sender&amp;#039;s email address&lt;br /&gt;
# family = the list of all recipients&amp;#039; email addresses&lt;br /&gt;
msg[&amp;#039;From&amp;#039;] = me&lt;br /&gt;
msg[&amp;#039;To&amp;#039;] = COMMASPACE.join(family)&lt;br /&gt;
msg.preamble = &amp;#039;Our family reunion&amp;#039;&lt;br /&gt;
&lt;br /&gt;
# Assume we know that the image files are all in PNG format&lt;br /&gt;
for file in pngfiles:&lt;br /&gt;
    # Open the files in binary mode.  Let the MIMEImage class automatically&lt;br /&gt;
    # guess the specific image type.&lt;br /&gt;
    fp = open(file, &amp;#039;rb&amp;#039;)&lt;br /&gt;
    img = MIMEImage(fp.read())&lt;br /&gt;
    fp.close()&lt;br /&gt;
    msg.attach(img)&lt;br /&gt;
&lt;br /&gt;
# Send the email via our own SMTP server.&lt;br /&gt;
s = smtplib.SMTP(&amp;#039;localhost&amp;#039;)&lt;br /&gt;
s.sendmail(me, family, msg.as_string())&lt;br /&gt;
s.quit()&lt;br /&gt;
&lt;br /&gt;
Here’s an example of how to send the entire contents of a directory as an email message: [1]&lt;br /&gt;
&lt;br /&gt;
#!/usr/bin/env python&lt;br /&gt;
&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;Send the contents of a directory as a MIME message.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
import os&lt;br /&gt;
import sys&lt;br /&gt;
import smtplib&lt;br /&gt;
# For guessing MIME type based on file name extension&lt;br /&gt;
import mimetypes&lt;br /&gt;
&lt;br /&gt;
from optparse import OptionParser&lt;br /&gt;
&lt;br /&gt;
from email import encoders&lt;br /&gt;
from email.message import Message&lt;br /&gt;
from email.mime.audio import MIMEAudio&lt;br /&gt;
from email.mime.base import MIMEBase&lt;br /&gt;
from email.mime.image import MIMEImage&lt;br /&gt;
from email.mime.multipart import MIMEMultipart&lt;br /&gt;
from email.mime.text import MIMEText&lt;br /&gt;
&lt;br /&gt;
COMMASPACE = &amp;#039;, &amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    parser = OptionParser(usage=&amp;quot;&amp;quot;&amp;quot;\&lt;br /&gt;
Send the contents of a directory as a MIME message.&lt;br /&gt;
&lt;br /&gt;
Usage: %prog [options]&lt;br /&gt;
&lt;br /&gt;
Unless the -o option is given, the email is sent by forwarding to your local&lt;br /&gt;
SMTP server, which then does the normal delivery process.  Your local machine&lt;br /&gt;
must be running an SMTP server.&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
    parser.add_option(&amp;#039;-d&amp;#039;, &amp;#039;--directory&amp;#039;,&lt;br /&gt;
                      type=&amp;#039;string&amp;#039;, action=&amp;#039;store&amp;#039;,&lt;br /&gt;
                      help=&amp;quot;&amp;quot;&amp;quot;Mail the contents of the specified directory,&lt;br /&gt;
                      otherwise use the current directory.  Only the regular&lt;br /&gt;
                      files in the directory are sent, and we don&amp;#039;t recurse to&lt;br /&gt;
                      subdirectories.&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
    parser.add_option(&amp;#039;-o&amp;#039;, &amp;#039;--output&amp;#039;,&lt;br /&gt;
                      type=&amp;#039;string&amp;#039;, action=&amp;#039;store&amp;#039;, metavar=&amp;#039;FILE&amp;#039;,&lt;br /&gt;
                      help=&amp;quot;&amp;quot;&amp;quot;Print the composed message to FILE instead of&lt;br /&gt;
                      sending the message to the SMTP server.&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
    parser.add_option(&amp;#039;-s&amp;#039;, &amp;#039;--sender&amp;#039;,&lt;br /&gt;
                      type=&amp;#039;string&amp;#039;, action=&amp;#039;store&amp;#039;, metavar=&amp;#039;SENDER&amp;#039;,&lt;br /&gt;
                      help=&amp;#039;The value of the From: header (required)&amp;#039;)&lt;br /&gt;
    parser.add_option(&amp;#039;-r&amp;#039;, &amp;#039;--recipient&amp;#039;,&lt;br /&gt;
                      type=&amp;#039;string&amp;#039;, action=&amp;#039;append&amp;#039;, metavar=&amp;#039;RECIPIENT&amp;#039;,&lt;br /&gt;
                      default=[], dest=&amp;#039;recipients&amp;#039;,&lt;br /&gt;
                      help=&amp;#039;A To: header value (at least one required)&amp;#039;)&lt;br /&gt;
    opts, args = parser.parse_args()&lt;br /&gt;
    if not opts.sender or not opts.recipients:&lt;br /&gt;
        parser.print_help()&lt;br /&gt;
        sys.exit(1)&lt;br /&gt;
    directory = opts.directory&lt;br /&gt;
    if not directory:&lt;br /&gt;
        directory = &amp;#039;.&amp;#039;&lt;br /&gt;
    # Create the enclosing (outer) message&lt;br /&gt;
    outer = MIMEMultipart()&lt;br /&gt;
    outer[&amp;#039;Subject&amp;#039;] = &amp;#039;Contents of directory %s&amp;#039; % os.path.abspath(directory)&lt;br /&gt;
    outer[&amp;#039;To&amp;#039;] = COMMASPACE.join(opts.recipients)&lt;br /&gt;
    outer[&amp;#039;From&amp;#039;] = opts.sender&lt;br /&gt;
    outer.preamble = &amp;#039;You will not see this in a MIME-aware mail reader.\n&amp;#039;&lt;br /&gt;
&lt;br /&gt;
    for filename in os.listdir(directory):&lt;br /&gt;
        path = os.path.join(directory, filename)&lt;br /&gt;
        if not os.path.isfile(path):&lt;br /&gt;
            continue&lt;br /&gt;
        # Guess the content type based on the file&amp;#039;s extension.  Encoding&lt;br /&gt;
        # will be ignored, although we should check for simple things like&lt;br /&gt;
        # gzip&amp;#039;d or compressed files.&lt;br /&gt;
        ctype, encoding = mimetypes.guess_type(path)&lt;br /&gt;
        if ctype is None or encoding is not None:&lt;br /&gt;
            # No guess could be made, or the file is encoded (compressed), so&lt;br /&gt;
            # use a generic bag-of-bits type.&lt;br /&gt;
            ctype = &amp;#039;application/octet-stream&amp;#039;&lt;br /&gt;
        maintype, subtype = ctype.split(&amp;#039;/&amp;#039;, 1)&lt;br /&gt;
        if maintype == &amp;#039;text&amp;#039;:&lt;br /&gt;
            fp = open(path)&lt;br /&gt;
            # Note: we should handle calculating the charset&lt;br /&gt;
            msg = MIMEText(fp.read(), _subtype=subtype)&lt;br /&gt;
            fp.close()&lt;br /&gt;
        elif maintype == &amp;#039;image&amp;#039;:&lt;br /&gt;
            fp = open(path, &amp;#039;rb&amp;#039;)&lt;br /&gt;
            msg = MIMEImage(fp.read(), _subtype=subtype)&lt;br /&gt;
            fp.close()&lt;br /&gt;
        elif maintype == &amp;#039;audio&amp;#039;:&lt;br /&gt;
            fp = open(path, &amp;#039;rb&amp;#039;)&lt;br /&gt;
            msg = MIMEAudio(fp.read(), _subtype=subtype)&lt;br /&gt;
            fp.close()&lt;br /&gt;
        else:&lt;br /&gt;
            fp = open(path, &amp;#039;rb&amp;#039;)&lt;br /&gt;
            msg = MIMEBase(maintype, subtype)&lt;br /&gt;
            msg.set_payload(fp.read())&lt;br /&gt;
            fp.close()&lt;br /&gt;
            # Encode the payload using Base64&lt;br /&gt;
            encoders.encode_base64(msg)&lt;br /&gt;
        # Set the filename parameter&lt;br /&gt;
        msg.add_header(&amp;#039;Content-Disposition&amp;#039;, &amp;#039;attachment&amp;#039;, filename=filename)&lt;br /&gt;
        outer.attach(msg)&lt;br /&gt;
    # Now send or store the message&lt;br /&gt;
    composed = outer.as_string()&lt;br /&gt;
    if opts.output:&lt;br /&gt;
        fp = open(opts.output, &amp;#039;w&amp;#039;)&lt;br /&gt;
        fp.write(composed)&lt;br /&gt;
        fp.close()&lt;br /&gt;
    else:&lt;br /&gt;
        s = smtplib.SMTP(&amp;#039;localhost&amp;#039;)&lt;br /&gt;
        s.sendmail(opts.sender, opts.recipients, composed)&lt;br /&gt;
        s.quit()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;#039;__main__&amp;#039;:&lt;br /&gt;
    main()&lt;br /&gt;
&lt;br /&gt;
Here’s an example of how to unpack a MIME message like the one above, into a directory of files:&lt;br /&gt;
&lt;br /&gt;
#!/usr/bin/env python&lt;br /&gt;
&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;Unpack a MIME message into a directory of files.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
import os&lt;br /&gt;
import sys&lt;br /&gt;
import email&lt;br /&gt;
import errno&lt;br /&gt;
import mimetypes&lt;br /&gt;
&lt;br /&gt;
from optparse import OptionParser&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    parser = OptionParser(usage=&amp;quot;&amp;quot;&amp;quot;\&lt;br /&gt;
Unpack a MIME message into a directory of files.&lt;br /&gt;
&lt;br /&gt;
Usage: %prog [options] msgfile&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
    parser.add_option(&amp;#039;-d&amp;#039;, &amp;#039;--directory&amp;#039;,&lt;br /&gt;
                      type=&amp;#039;string&amp;#039;, action=&amp;#039;store&amp;#039;,&lt;br /&gt;
                      help=&amp;quot;&amp;quot;&amp;quot;Unpack the MIME message into the named&lt;br /&gt;
                      directory, which will be created if it doesn&amp;#039;t already&lt;br /&gt;
                      exist.&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
    opts, args = parser.parse_args()&lt;br /&gt;
    if not opts.directory:&lt;br /&gt;
        parser.print_help()&lt;br /&gt;
        sys.exit(1)&lt;br /&gt;
&lt;br /&gt;
    try:&lt;br /&gt;
        msgfile = args[0]&lt;br /&gt;
    except IndexError:&lt;br /&gt;
        parser.print_help()&lt;br /&gt;
        sys.exit(1)&lt;br /&gt;
&lt;br /&gt;
    try:&lt;br /&gt;
        os.mkdir(opts.directory)&lt;br /&gt;
    except OSError as e:&lt;br /&gt;
        # Ignore directory exists error&lt;br /&gt;
        if e.errno != errno.EEXIST:&lt;br /&gt;
            raise&lt;br /&gt;
&lt;br /&gt;
    fp = open(msgfile)&lt;br /&gt;
    msg = email.message_from_file(fp)&lt;br /&gt;
    fp.close()&lt;br /&gt;
&lt;br /&gt;
    counter = 1&lt;br /&gt;
    for part in msg.walk():&lt;br /&gt;
        # multipart/* are just containers&lt;br /&gt;
        if part.get_content_maintype() == &amp;#039;multipart&amp;#039;:&lt;br /&gt;
            continue&lt;br /&gt;
        # Applications should really sanitize the given filename so that an&lt;br /&gt;
        # email message can&amp;#039;t be used to overwrite important files&lt;br /&gt;
        filename = part.get_filename()&lt;br /&gt;
        if not filename:&lt;br /&gt;
            ext = mimetypes.guess_extension(part.get_content_type())&lt;br /&gt;
            if not ext:&lt;br /&gt;
                # Use a generic bag-of-bits extension&lt;br /&gt;
                ext = &amp;#039;.bin&amp;#039;&lt;br /&gt;
            filename = &amp;#039;part-%03d%s&amp;#039; % (counter, ext)&lt;br /&gt;
        counter += 1&lt;br /&gt;
        fp = open(os.path.join(opts.directory, filename), &amp;#039;wb&amp;#039;)&lt;br /&gt;
        fp.write(part.get_payload(decode=True))&lt;br /&gt;
        fp.close()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;#039;__main__&amp;#039;:&lt;br /&gt;
    main()&lt;br /&gt;
&lt;br /&gt;
Here’s an example of how to create an HTML message with an alternative plain text version: [2]&lt;br /&gt;
&lt;br /&gt;
 #!/usr/bin/env python&lt;br /&gt;
 &lt;br /&gt;
 import smtplib&lt;br /&gt;
 &lt;br /&gt;
 from email.mime.multipart import MIMEMultipart&lt;br /&gt;
 from email.mime.text import MIMEText&lt;br /&gt;
 &lt;br /&gt;
 # me == my email address&lt;br /&gt;
 # you == recipient&amp;#039;s email address&lt;br /&gt;
 me = &amp;quot;my@email.com&amp;quot;&lt;br /&gt;
 you = &amp;quot;your@email.com&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 # Create message container - the correct MIME type is multipart/alternative.&lt;br /&gt;
 msg = MIMEMultipart(&amp;#039;alternative&amp;#039;)&lt;br /&gt;
 msg[&amp;#039;Subject&amp;#039;] = &amp;quot;Link&amp;quot;&lt;br /&gt;
 msg[&amp;#039;From&amp;#039;] = me&lt;br /&gt;
 msg[&amp;#039;To&amp;#039;] = you&lt;br /&gt;
 &lt;br /&gt;
 # Create the body of the message (a plain-text and an HTML version).&lt;br /&gt;
 text = &amp;quot;Hi!\nHow are you?\nHere is the link you wanted:\nhttps://www.python.org&amp;quot;&lt;br /&gt;
 html = &amp;quot;&amp;quot;&amp;quot;\&lt;br /&gt;
 &amp;lt;html&amp;gt;&lt;br /&gt;
   &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt;&lt;br /&gt;
   &amp;lt;body&amp;gt;&lt;br /&gt;
     &amp;lt;p&amp;gt;Hi!&amp;lt;br&amp;gt;&lt;br /&gt;
        How are you?&amp;lt;br&amp;gt;&lt;br /&gt;
        Here is the &amp;lt;a href=&amp;quot;https://www.python.org&amp;quot;&amp;gt;link&amp;lt;/a&amp;gt; you wanted.&lt;br /&gt;
     &amp;lt;/p&amp;gt;&lt;br /&gt;
   &amp;lt;/body&amp;gt;&lt;br /&gt;
 &amp;lt;/html&amp;gt;&lt;br /&gt;
 &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 # Record the MIME types of both parts - text/plain and text/html.&lt;br /&gt;
 part1 = MIMEText(text, &amp;#039;plain&amp;#039;)&lt;br /&gt;
 part2 = MIMEText(html, &amp;#039;html&amp;#039;)&lt;br /&gt;
 &lt;br /&gt;
 # Attach parts into message container.&lt;br /&gt;
 # According to RFC 2046, the last part of a multipart message, in this case&lt;br /&gt;
 # the HTML message, is best and preferred.&lt;br /&gt;
 msg.attach(part1)&lt;br /&gt;
 msg.attach(part2)  &lt;br /&gt;
 &lt;br /&gt;
 # Send the message via local SMTP server.&lt;br /&gt;
 s = smtplib.SMTP(&amp;#039;localhost&amp;#039;)&lt;br /&gt;
 # sendmail function takes 3 arguments: sender&amp;#039;s address, recipient&amp;#039;s address&lt;br /&gt;
 # and message to send - here it is sent as one string.&lt;br /&gt;
 s.sendmail(me, you, msg.as_string())&lt;br /&gt;
 s.quit()&lt;br /&gt;
&lt;br /&gt;
Footnotes&lt;br /&gt;
[1]	Thanks to Matthew Dixon Cowles for the original inspiration and examples.&lt;br /&gt;
[2]	Contributed by Martin Matejek.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Referensi==&lt;br /&gt;
&lt;br /&gt;
* https://docs.python.org/2/library/email-examples.html&lt;/div&gt;</summary>
		<author><name>Onnowpurbo</name></author>
	</entry>
</feed>