tags:

views:

90

answers:

5

Hello,

I am trying to set up an email function that will email the last 15 lines of a results.txt file in python. I am not sure how to do this and was asking do I have to connect to an email server or does python have some other way of sending email. The code below is what i have got so far and any help would be appreciated. Thanks

import smtplib  

# Import the email modules we'll need
from email.mime.text import MIMEText

# Open a plain text file for reading.  For this example, assume that
# the text file contains only ASCII characters.
fp = open('/home/build/result.txt', 'r')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()

me = '[email protected]'
you = '[email protected]'
msg['Subject'] = 'The contents of %s' % '/home/build/result.txt'
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP()
s.sendmail(me, [you], msg.as_string())
s.quit()

Hello again,

When I'm trying to connect the server will not connect. I know I should not enter the email address. Can anyone suggest what way to write the host information. Thanks

smtplib.SMTPServerDisconnected: please run connect() first
+1  A: 
msg = MIMEText("\n".join(fp.read().split("\n")[-15:]))

Or if you don't need blank lines at end, do like

msg = MIMEText("\n".join(fp.read().strip().split("\n")[-15:]))
S.Mark
+1  A: 

You can have a look at this one:

http://docs.python.org/library/email-examples.html

Alex
+2  A: 
msg = MIMEText(''.join(fp.readlines()[-15:]))
Michal Čihař
+3  A: 

There is no way for your machine to send mail without connecting to a server (otherwise how would the mail get out of your machine?). Most people have a readily available SMTP server provided for them, either by their company (if this is on an intranet) or by their ISP (if a home user). You would need the host name (often something like smtp1.myispdomain.com where of course myispdomain is something else for you) and a port number, usually 25. Sometimes the host is provided as a numeric IP address, like 192.168.0.1.

The SMTP() call can take these parameters and will connect to the server automatically. If you don't supply the parameters when you create the SMTP object, you have to call connect() on it later, supplying the same info. See the documentation for more.

Note that the default is to connect to localhost and port 25. This works if you're on a Linux box running its own mail forwarder (e.g. Postfix, Sendmail, Exim) but if you're on a Windows machine generally you'll have to use the address supplied by your ISP.

Peter Hansen
A: 

You might want to have a look at my mailer module. It wraps the email modules in the standard library.

from mailer import Mailer
from mailer import Message

message = Message(From="[email protected]",
                  To="[email protected]",
                  charset="utf-8")
message.Subject = 'The contents of %s' % '/home/build/result.txt'
message.Body = ''.join(fp.readlines()[-15:])

sender = Mailer('smtp.example.com')
sender.login('username', 'password')
sender.send(message)
Ryan Ginstrom