I used a simple program copied from a course book as follows:
msg = MIMEText(“Hallo daar!”)
msg[‘Subject’] = ‘Een testbericht’
msg[‘From’]= ‘pejamide@xs4all.nl’
msg[‘To’] = ‘jamie9ze@xs4all.nl’
s = smtplib.SMTP(‘localhost’)
s.sendmail(‘pejamide@xs4all.nl’, [‘jamie9ze@xs4all.nl’], msg.as_string())
print(“Bericht verstuurd!”)
After run an wait about 15 seconds I gor the errors as follows:
Traceback (most recent call last):
File “…\jan\testsendmail.py”, line 7, in
s = smtplib.SMTP(‘localhost’)
File “C:\Program Files\Python\lib\smtplib.py”, line 255, in init
(code, msg) = self.connect(host, port)
File “C:\Program Files\Python\lib\smtplib.py”, line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File “C:\Program Files\Python\lib\smtplib.py”, line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File “C:\Program Files\Python\lib\socket.py”, line 843, in create_connection
raise err
File “C:\Program Files\Python\lib\socket.py”, line 831, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] Kan geen verbinding maken omdat de doelcomputer de verbinding actief heeft geweigerd
What should I have forgotten to do anything extra?
After the correction for SMTP, I got a new error as follows:
<class ‘smtplib.SMTPSenderRefused’>: (550, b’5.1.0 Please authenticate using your username and password [1465]’, ‘pejamide@xs4all.nl’)
I wonder me why I have to give the password for that user. And how and where I can give that password?
That depends on the server. Usually SMTP servers allow unauthenticated
delivery for some trusted networks (localhost, the local LAN network,
for an ISP often the networks of its users) but require authentication
for others if the target of the message is not their own domain i.e.
you can deliver email to the server for local delivery, but not ask the
server to deliver to other places without authentication.
BUT… be VERY sure you’re supplying a password to the right server and
DO NOT post the password in any script you post here! You’d be telling
the world your password! Usually we do not like to put passwords in
scripts at all - put it in a special file if you must and read it from
that file.
I am surprised, that the password is required for the server ‘smtp.xs4all.nl’, because my PHP program running under the same Apache on my laptop donot need the password for sending an e-mail.
Is there maybe another python problem?
I am surprised, that the password is required for the server
‘smtp.xs4all.nl’, because my PHP program running under the same Apache
on my laptop donot need the password for sending an e-mail.
The error plainly asks for authentication. How you investigated the
mechanism used by PHP to send the email - maybe it isn’t using SMTP,
maybe it is using a different server.
Is there maybe another python problem?
Not at this point. The error plainly asks for authentication. Why,
depends on exactly what you’re doing. You haven’t said:
Observe this direct conversation with the SMTP server:
[~]fleet2*> telnet smtp.xs4all.nl smtp
Trying 194.109.6.51...
Connected to smtp.xs4all.nl.
Escape character is '^]'.
220 smtp-cloud8.xs4all.net smtp-cloud8.xs4all.net ESMTP server ready
helo fleet
250 smtp-cloud8.xs4all.net hello [1.144.244.145], pleased to meet you
mail from:<pejamide@xs4all.nl>
550 5.1.0 Please authenticate using your username and password [1639]
Connection closed by foreign host.
Have you read the whole page at the URL you cite above? The discussion
section talks about configuring the sendmail.ini file here:
which says that there is a PHP configuration section for specifying the
authentication credentials, so that the call code does not need to
bother. You will need to do something equivalent in your programme,
probably using the smtplib.login() method.
You are right to not want to put credentials in the code.
Personally I keep the username/password in my .netrc file. Python has a
“netrc” module which provides access to this file. It is a simple way to
store login/pasword for systems where you want it. I would not use it
for banking access etc because the password is kept in the clear in a
text file.
But I do use it to fetch my email, and for a few other services.
The file itself lives at ~/.netrc, and should be readable ONY BY YOU (it
contails credentials).
This is fine for simple things; I would not use it for my banking
credentials etc etc.
from netrc import netrc
........
entry = netrc().hosts.get("username@service")
if entry is None:
... no such entry ...
else:
login, account, password = entry
The key for hosts.get() is an arbitrary “hostname” (the netrc was
designed around FTP servers) but anything will do. For example, you
might make a clause like: