Search Text File and Email Results
<p>Working with a text file to search for a string and then email the nex few lines. </p>

<p>I have the search working and it prints the correct lines 
The email sends successfully, however it only contains the last line of the output </p>

<p>Any thoughts? </p>

<p>File.txt </p>

<pre><code>first
second
thrid
------------------------------------------------------------------------------
after first
after second 
after thrid 
after forth 
after fifth
after sixth
</code></pre>

<p>my code</p>

<pre><code>import smtplib

from email.mime.text import MIMEText

from_address = "fromaddress@domain.com"
to_address = "toaddress@domain.com"


with open("file.txt", "r") as f:
    searchlines = f.readlines()
    for i, line in enumerate(searchlines):
        if "------------------------------------------------------------------------------" in line:
            for l in searchlines[i:i+6]: print l,
            output = l

            msg = MIMEText(output)
            msg['Subject'] = "Test email"
            msg['From'] = from_address
            msg['To'] = to_address

            body = output

            # Send the message via local SMTP server.
            s = smtplib.SMTP('smtp.domain.com', '8025')

            s.sendmail(from_address, to_address, msg.as_string())

            s.quit()
</code></pre>

<p>Output via print: </p>

<pre><code>------------------------------------------------------------------------------
after first
after second 
after thrid 
after forth 
after fifth
</code></pre>

<p>The email only contains in the body</p>

<pre><code>after fifth
</code></pre>

<p>Your indentation is wonky.  The loop should collect the lines, then you should send an email when you have collected them all.</p>

<pre><code>with open("file.txt", "r") as f:
    searchlines = f.readlines()
# &lt;-- unindent; we don't need this in the "with"
for i, line in enumerate(searchlines):
    # Aesthetics: use "-" * 78
    if "-" * 78 in line:
        for l in searchlines[i:i+6]:
            print l,
        # collect all the lines in output, not just the last one
        output = ''.join(searchlines[i:i+6])
        # if you expect more than one result, this is wrong
        break

# &lt;-- unindent: we are done when the for loop is done
# Add an explicit encoding -- guessing here which one to use
msg = MIMEText(output, 'plain', 'utf-8')
msg['Subject'] = "Test email"
msg['From'] = from_address
msg['To'] = to_address

# no point in assigning body and not using it for anything

# Send the message via local SMTP server.
s = smtplib.SMTP('smtp.domain.com', '8025')
s.sendmail(from_address, to_address, msg.as_string())
s.quit()
</code></pre>

<p>The real bug was that <code>output = l</code> would only collect the last value of <code>l</code> after the innermost <code>for l</code> loop, but restructuring the program to not do things in a loop when they should only happen once (and vice versa!) hopefully makes it clearer as well.</p>

<p>If there can be more than one result, just removing the <code>break</code> will not suffice -- this would reinstate your original bug in a different form, where only the last match of six lines will be sent.  If you need to support multiple results, your code needs to combine them into one message somehow.</p>

python