#!/usr/bin/env python
import sys, os
import subprocess
import re
import datetime
# simple class with write method to redirect print output to a file
class WritableObject:
def __init__(self):
self.content = []
def write(self, string):
self.content.append(string)
x=datetime.date.today()
yyyy, dd = x.year, x.day
mail = WritableObject()
print >>mail, "To: %s" % (mail1)
print >>mail, "Subject: %s day %s " % (yyyy, dd)
print >>mail, "Content-Type: text/html; charset=\"us-ascii\""
subprocess.call("php $HOME/report.php > temp.txt", shell=True)
body = subprocess.call("cat $HOME/.txt", shell=True)
print >>mail, ""
print >>mail, "<html>"
print >>mail, "<body>"
#copies common header in the Problem html to the OK one
subprocess.call("cp $HOME/mail $HOME/OK.html", shell=True)
input_file = open('mail', 'r+')
lines = input_file.readlines()
input_file.close()
output_file = open("mail", "w+")
for line in lines:
if line.startswith('<p>'):
line = '<p>' + line.rstrip('\n') + '</p>' + '\n'
output_file.write(line)
if not body:
print >>mail, '''</p> <b>[No problems detected ].<b /> All monitored
metrics
<p><font size=\"4\">
<font color=\"darkgreen\">
<b>OK<b>
</p>
</font>
</body>
</html>'''
else:
print >>mail, '''
<table border=\"1\"> <tr>
<th> Table</th>
<th> Table2</th>
<th> Table3</th>
</tr>
</table>
%s
</body>
</html>''' % (body)
subprocess.call("sendmail m...@my.mail < mail", shell=True)
output_file.close()
You're storing the text in the 'mail' object (an instance of
WritableObject), but at no point do you use what you have stored.
I can also see that you're writing to a file call "mail" via
output_file, but you're not closing the file before passing it to
"sendmail".
Python comes with a stringio class that does this *AND* has a method to
retrieve the result. StringIO in 2.x, io module in 3.x
One method: install ipython (another python shell, but with some
useful extra features)
and then run the program inside ipython in debug mode:
---------
$ ipython
In [1]: run -d filename.py
Breakpoint 1 at /path/to/filename.py:3
NOTE: Enter 'c' at the ipdb> prompt to start your script.
> <string>(1)<module>()
ipdb>
--------
(You type c to continue)
--------
ipdb> c
> /path/to/filename.py(3)<module>()
2
1---> 3 import sys, os
4 import subprocess
--------
and you can see you're now on line 3.
Press h for help to see what commands you can type.
Press n for next line if you want to step.
Press s to step into a function when it's being called. Etc.
--
Anita
Terry,
from what I've read stringIO allows us to store strings in a 'virtual'
file.
Can you please write just 2 lines exemplifying a write to and a read
from an OS level file?
MRAB, that 'mail' object should've been the os level 'mail' file. But
as I'm a beginner I was using it wrong..
Thanks
it's actually easier to use real files than creating a virtual file:
f = open('mail.txt', 'w')
f.write("foo\n") # or print >> f, "foo"
and, is there any reason why you're not using the email and smtplib?
http://docs.python.org/library/email-examples.html
Mainly because I was unaware of them :(
I just read about them and I found all the Subject, From, To classes,
but what about Content-Type?
I need Content-Type: text/html and charset="utf-8" so that I can pass
a basic CSS formatting.
How can I do that? With email.mime.base.MIMEBase?
I'll have to update that in my code later, as I already got it to work
and my current RHEL5.3 default
Python 2.4.3 (#1, Sep 17 2008) install is complaining with
"ImportError: No module named mime.multipart"
Thank you for your patience and feedback
You can set MIME type and encoding from the MIME constructor
email.mime.Text.MIMEText("<b>Bold Text</b>", "html", "utf-8")
> I'll have to update that in my code later, as I already got it to work
> and my current RHEL5.3 default
> Python 2.4.3 (#1, Sep 17 2008) install is complaining with
> "ImportError: No module named mime.multipart"
are you importing "import mime" or "import email.mime" or "import
email.MIMEMultipart"?
> are you importing "import mime" or "import email.mime" or "import
> email.MIMEMultipart"?
Hi Lie.
I was importing as,
'from email.mime.text import MIMEText' which was returning that error
because I don't have
a mime subdir in my /usr/lib64/python2.4/email/ as I was expecting.
'import email.MIMEMultipart' worked perfectly (the mime methods are in
the /email/ root).
I'll try to redo my script right after I get some urgent things done.
Thanks :)