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 for loop prints all the lines, but then the loop ends, and l is set to the last item in the loop: after fifth</p>

<p>then you email that last l</p>

python