Can't Get Email Interface Working

E

Eric Price

Hi;
I'm writing a script that includes an email function. So I went to the
cookbook and dug up this, and tweaked it just a bit to make it easier to get
it to work, but it throws an error:
.... import MimeWriter, mimetools, cStringIO
.... out = cStringIO.StringIO()
.... # txtin = cStringIO.StringIO(msg)
.... writer = MimeWriter.MimeWriter(out)
.... writer.addheader("From", sender)
.... writer.addheader("To", recipient)
.... writer.addheader("Subject", subject)
.... writer.addheader("MIME-Version", "1.0")
.... writer.startmultipartbody("alternative")
.... writer.flushheaders()
.... subpart = writer.nextpart()
.... subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
.... pout = subpart.startbody("text/plain", [("charset", 'us-ascii')])
.... mimetools.encode(txtin, pout, 'quoted-printable')
.... txtin.close()
.... subpart = writer.nextpart()
.... subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
.... pout = subpart.startbody("text/html", [("charset", 'us-ascii')])
.... mimetools.encode(htmlin, pout, 'quoted-printable')
.... htmlin.close()
.... writer.lastpart()
.... msg = "test"
.... out.close()
.... return msg
........ def sendMail(sender, recipient, subject, html, text):
.... import smtplib
.... message = createMail(sender, recipient, subject, html, text)
.... server = smtplib.SMTP("localhost")
.... server.sendmail(sender, recipient, message)
.... server.quit()
........ sendMail("(e-mail address removed)", "(e-mail address removed)", "Web Stie(s)
Down!!!", "", "text")
....
Traceback (most recent call last):
File "<stdin>", line 2, in ?
File "<stdin>", line 4, in sendMail
File "<stdin>", line 10, in createMail
File "/usr/local/lib/python2.4/MimeWriter.py", line 153, in
startmultipartbody
self._boundary = boundary or mimetools.choose_boundary()
File "/usr/local/lib/python2.4/mimetools.py", line 130, in choose_boundary
hostid = socket.gethostbyname(socket.gethostname())
socket.gaierror: (8, 'hostname nor servname provided, or not known')
Now, I can send email from my server no problem. In fact, I have my script
working already...but with a shell script instead of this python code.
Please advise.
TIA,
Eric

_________________________________________________________________
Get a FREE Web site, company branded e-mail and more from Microsoft Office
Live! http://clk.atdmt.com/MRT/go/mcrssaub0050001411mrt/direct/01/
 
H

hlubenow

Eric said:
Hi;
I'm writing a script that includes an email function. So I went to the
cookbook and dug up this, and tweaked it just a bit to make it easier to
get it to work, but it throws an error:
... import MimeWriter, mimetools, cStringIO
... out = cStringIO.StringIO()
... # txtin = cStringIO.StringIO(msg)
... writer = MimeWriter.MimeWriter(out)
... writer.addheader("From", sender)
... writer.addheader("To", recipient)
... writer.addheader("Subject", subject)
... writer.addheader("MIME-Version", "1.0")
... writer.startmultipartbody("alternative")
... writer.flushheaders()
... subpart = writer.nextpart()
... subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
... pout = subpart.startbody("text/plain", [("charset", 'us-ascii')])
... mimetools.encode(txtin, pout, 'quoted-printable')
... txtin.close()
... subpart = writer.nextpart()
... subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
... pout = subpart.startbody("text/html", [("charset", 'us-ascii')])
... mimetools.encode(htmlin, pout, 'quoted-printable')
... htmlin.close()
... writer.lastpart()
... msg = "test"
... out.close()
... return msg
...... def sendMail(sender, recipient, subject, html, text):
... import smtplib
... message = createMail(sender, recipient, subject, html, text)
... server = smtplib.SMTP("localhost")
... server.sendmail(sender, recipient, message)
... server.quit()
...... sendMail("(e-mail address removed)", "(e-mail address removed)", "Web Stie(s)
Down!!!", "", "text")
...
Traceback (most recent call last):
File "<stdin>", line 2, in ?
File "<stdin>", line 4, in sendMail
File "<stdin>", line 10, in createMail
File "/usr/local/lib/python2.4/MimeWriter.py", line 153, in
startmultipartbody
self._boundary = boundary or mimetools.choose_boundary()
File "/usr/local/lib/python2.4/mimetools.py", line 130, in
choose_boundary
hostid = socket.gethostbyname(socket.gethostname())
socket.gaierror: (8, 'hostname nor servname provided, or not known')
Now, I can send email from my server no problem. In fact, I have my script
working already...but with a shell script instead of this python code.
:lol:

Please advise.
TIA,
Eric

You may want to take a look at simplemail.py:

http://gelb.bcom.at/trac/simplemail/browser/trunk/simplemail.py

that does sending mails in Python comfortably.

HTH

H.
 
T

Tim Williams

Good grief! And they call a 722-line program "simple"?! LOL!
I did what I need to do with a __one_line_shell_script__ LOL!
Naw, if I have to go through all that, I'll skip on python this time around,
thank you very much!
Eric

Yup, its not so simple

Your problem is :

File "/usr/local/lib/python2.4/mimetools.py", line 130, in choose_boundary
hostid = socket.gethostbyname(socket.gethostname())
socket.gaierror: (8, 'hostname nor servname provided, or not known')

What do you get if you do the following in your interpreter?

 
D

Dennis Lee Bieber

Good grief! And they call a 722-line program "simple"?! LOL!
I did what I need to do with a __one_line_shell_script__ LOL!
Naw, if I have to go through all that, I'll skip on python this time around,
thank you very much!

Ah, but that "one line shell script" likely is just passing some
text file to a pre-supplied mail spooling utility. Whereas the Python
code you are writing IS the spooling utility.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top