Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77422 > unrolled thread

Re: error while writing program to send mail.

Started byMRAB <python@mrabarnett.plus.com>
First post2014-09-02 00:59 +0100
Last post2014-09-02 00:59 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: error while writing program to send mail. MRAB <python@mrabarnett.plus.com> - 2014-09-02 00:59 +0100

#77422 — Re: error while writing program to send mail.

FromMRAB <python@mrabarnett.plus.com>
Date2014-09-02 00:59 +0100
SubjectRe: error while writing program to send mail.
Message-ID<mailman.13701.1409615992.18130.python-list@python.org>
On 2014-09-02 00:35, Om Prakash wrote:
> 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

That should be:

fp.close()

>
> me = "torque.india@gmail.com"
> you = "oomprakash@gmail.com"
>
> msg['Subject']  = 'The contents of $s' % message

You're trying to use the format operator, but:

1. You never bound the name 'message' to a value, hence:

     NameError: name 'message' is not defined

2. The format string contains '$s' instead of '%s'.

> 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] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web