473,327 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Can't Get Email Interface Working

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("ro**@mail.example.com", "jo*@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/mcrssaub...mrt/direct/01/

Apr 7 '07 #1
2 1754
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("ro**@mail.example.com", "jo*@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/.../simplemail.py

that does sending mails in Python comfortably.

HTH

H.
Apr 7 '07 #2
On 07/04/07, Eric Price <so***********************@hotmail.comwrote:
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?

>>import socket
socket.gethostname()
'twilliams'
>>socket.gethostbyname(socket.gethostname())
'194.129.107.254'
>>>
Apr 7 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Zheng Da | last post by:
I don't know where should I ask the question, so send the email to this group. I choose this group, because I want to write the program with c++ :) I want to write a program which support...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
2
by: Arjen | last post by:
Hello, Let's see this sample code. Here is the main. <cut> namespace A { /// <summary> /// Singleton /// </summary>
3
by: Erik Harris | last post by:
I apologize if this is a stupid question - I'm relatively new to OOP. I have a property that must exist in a class in order to be used by another class. The property, however, does not change with...
2
by: Todd Brooks | last post by:
I have a coclass that implements a dual interface. The thing that's a little unusual is that the coclass doesn't inherit directly from the interface, rather it inherits from an implementation class...
7
by: Nadav | last post by:
Hi, 1. I am writing some kind of a CLI Linker 2. I am using the unmanaged meta-data API. I wonder browsing "cor.h" I have encountered the 'ICeeGen' this interface is retrieved by the...
4
by: Don Curtis | last post by:
If I use GDI+ within a service, what problems can I expect? Why can't GDI+ be used within a service? http://msdn2.microsoft.com/en-us/library/system.drawing.aspx "Classes within the...
11
by: cybervigilante | last post by:
I can't seem to change the include path on my local winmachine no matter what I do. It comes up as includ_path .;C:\php5\pear in phpinfo() but there is no such file. I installed the WAMP package...
18
by: =?Utf-8?B?SGVyYg==?= | last post by:
I'm using a ReportViewer in ASP.NET. From the development environment it works fine. When published and I try to open the page with the ReportViewer I get the error: An internal error occurred...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.