On 1/14/12, Chris Kavanagh <cka...@msn.com> wrote:
> I was looking at this code from the Python Docs
> (http://docs.python.org/library/email-examples.html), trying to learn
> how to send email from a Pyhton script. Anyways, part of this code
> confused me. Here's the script:
>
> 1 # Import smtplib for the actual sending function
> 2 import smtplib
> 3
> 4 # Import the email modules we'll need
> 5 from email.mime.text import MIMEText
> 6
> 7 # Open a plain text file for reading.  For this example, assume that
> 8 # the text file contains only ASCII characters.
> 9 fp = open(textfile, 'rb')
> 10 # Create a text/plain message
> 11 msg = MIMEText(fp.read())
> 12 fp.close()
> 13
> 14 # me == the sender's email address
> 15 # you == the recipient's email address
> 16 msg['Subject'] = 'The contents of %s' % textfile
> 17 msg['From'] = me
> 18 msg['To'] = you
> 19
> 20 # Send the message via our own SMTP server, but don't include the
> 21 # envelope header.
> 22 s = smtplib.SMTP('localhost')
> 23 s.sendmail(me, [you], msg.as_string())
> 24 s.quit()
>
> What I don't understand is lines 16-18, more specifically the
> msg['Subject'] format. I thought this was only done with dics??
> Obviously the variable msg isn't a dic, so how can this be done??
>
> I actually put lines 11, 16,17,18, in the interpreter, then printed out
> msg, so I get what it's doing, but my question still stands. How can one
> do this, when I thought it could only be done with dictionaries???
Just from looking, I can pretty much guarantee that MIMEText, when you
create an instance of it by giving it that file pointer (in other
words, you give it a text file), returns a dictionary automatically. I
have not looked up the module, but just reading that code, I don't
think I have to. Clearly, msg is a dictionary-like object, and since
msg is what the constructor of MIMEText() returns when passed the
content of a file, the constructor must return a dictionary-like
object.
>
> Thanks for any help! BTW, using Python27, WinXP.
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to