Can't Get Email Interface Working

hlubenow hlubenow2 at gmx.net
Sat Apr 7 11:33:38 EDT 2007


Eric Price wrote:

> 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.

: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.



More information about the Python-list mailing list