Can't Get Email Interface Working

Eric Price soyouthinkimgonnalikethis at hotmail.com
Sat Apr 7 15:29:27 CEST 2007


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:

>>>def createMail(sender, recipient, subject, html, text):
...     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()
...
>>>if __name__=="__main__":
...     sendMail("root at mail.example.com", "joe at yahoo.com", "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/




More information about the Python-list mailing list