Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'subject:error': 0.03; 'value,': 0.04; 'string': 0.09; 'msg': 0.09; 'operator,': 0.09; 'subject:while': 0.09; 'python': 0.11; "'rb')": 0.16; 'be:': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'headers.': 0.16; 'localhost': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'nameerror:': 0.16; 'prakash': 0.16; 'received:192.168.1.4': 0.16; 'subject:program': 0.16; 'subject:send': 0.16; 'subject:writing': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'starts': 0.20; 'import': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'sorry,': 0.24; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'programming.': 0.30; 'code': 0.31; 'getting': 0.31; 'file': 0.32; 'text': 0.33; 'open': 0.33; 'url:python': 0.33; '(most': 0.33; 'plain': 0.33; 'skip:# 10': 0.33; 'message.': 0.35; 'something': 0.35; 'but': 0.35; 'hi,': 0.36; 'url:org': 0.36; 'should': 0.36; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'new': 0.61; 'simple': 0.61; "you're": 0.61; 'name': 0.63; 'email addr:gmail.com"': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=Uv7tNoAB c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=u9EReRu7m0cA:10 a=3Kf9FMe-JDoA:10 a=4TFnWSbgze8A:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=pGLkceISAAAA:8 a=6QYeqS8Fv_M1tCZi68UA:9 a=QEXdDO2ut3YA:10 a=egxqZzf5gs0A:10 a=MSl-tDqOz04A:10 X-AUTH: mrabarnett:2500 Date: Tue, 02 Sep 2014 00:59:49 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: error while writing program to send mail. References: <540502CD.8030209@gmail.com> In-Reply-To: <540502CD.8030209@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409615992 news.xs4all.nl 2917 [2001:888:2000:d::a6]:47559 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77422 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 > 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() >