Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #77420 > unrolled thread
| Started by | Om Prakash <torque.india@gmail.com> |
|---|---|
| First post | 2014-09-02 05:05 +0530 |
| Last post | 2014-09-02 03:28 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
error while writing program to send mail. Om Prakash <torque.india@gmail.com> - 2014-09-02 05:05 +0530
Re: error while writing program to send mail. Denis McMahon <denismfmcmahon@gmail.com> - 2014-09-02 03:28 +0000
| From | Om Prakash <torque.india@gmail.com> |
|---|---|
| Date | 2014-09-02 05:05 +0530 |
| Subject | error while writing program to send mail. |
| Message-ID | <mailman.13699.1409614555.18130.python-list@python.org> |
Hi,
I am writing this program from
https://docs.python.org/2/library/email-examples.html
but getting the error as
singhom@debian:~/pythons$ python send_email.py
Traceback (most recent call last):
File "send_email.py", line 18, in <module>
msg['Subject'] = 'The contents of $s' % message
NameError: name 'message' is not defined
i know the error would be something simple and i am overlooking it, any
help would be highly appreciated, I am sorry, but I am very new to
python programming.
code starts here.
#/usr/bin/python2.7 -tt
## sending a simple text message using python.
import smtplib
from email.mime.text import MIMEText
# Open file for reading.
fp = open("message", 'rb')
# Create a plain text message.
msg = MIMEText(fp.read())
fp.close
me = "torque.india@gmail.com"
you = "oomprakash@gmail.com"
msg['Subject'] = 'The contents of $s' % message
msg['From'] = me
msg['To'] = you
# Send message thorugh localhost but don't include the envelope headers.
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()
[toc] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-09-02 03:28 +0000 |
| Message-ID | <lu3dgo$t2s$1@dont-email.me> |
| In reply to | #77420 |
On Tue, 02 Sep 2014 05:05:41 +0530, Om Prakash wrote:
> fp = open("message", 'rb')
"message" here is a string literal
> fp.close
should be fp.close()
> msg['Subject'] = 'The contents of $s' % message
message here is a variable. The variable named message has not previously
had a value assigned to it.
$s should be %s
If you want the subject to be the filename, then what you need to do is:
1) assign the filename to some variable
2) open the file using the variable that contains the filename
3) use the variable that contains the filename to generate the subject
eg (assuming suitable imports etc) and the message is in a text file
called message.txt:
msgfile = "message.txt"
fp = open( msgfile, "r" )
msg = MIMEText(fp.read())
fp.close()
msg['Subject'] = 'The contents of %s' % msgfile
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web