Sendmail problem

I used a simple program copied from a course book as follows:
msg = MIMEText(“Hallo daar!”)
msg[‘Subject’] = ‘Een testbericht’
msg[‘From’]= ‘’
msg[‘To’] = ‘’
s = smtplib.SMTP(‘localhost’)
s.sendmail(‘’, [‘’], 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\”, line 7, in
s = smtplib.SMTP(‘localhost’)
File “C:\Program Files\Python\lib\”, line 255, in init
(code, msg) = self.connect(host, port)
File “C:\Program Files\Python\lib\”, line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File “C:\Program Files\Python\lib\”, line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File “C:\Program Files\Python\lib\”, line 843, in create_connection
raise err
File “C:\Program Files\Python\lib\”, line 831, in create_connection
ConnectionRefusedError: [WinError 10061] Kan geen verbinding maken omdat de doelcomputer de verbinding actief heeft geweigerd

What should I have forgotten to do anything extra?

I am using localhost running under XAMPP Apache.

ConnectionRefusedError means that there is no service listening on
‘localhost’ for SMTP. Probably this line:

s = smtplib.SMTP('localhost')

needs to be adjusted to point at a server which will access an SMTP

Cameron Simpson

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]’, ‘’)

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.

The smtplib module has a login method:

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.

Cameron Simpson

I am surprised, that the password is required for the server ‘’, 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
‘’, 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:

  • exactly what server you’re talking to
  • which smtplib call is producing the error message

Cameron Simpson

  • Concerning PHP command ‘mail’: it is indeed using SMTP server.

  • I use the specified one ‘’.

  • The smtplib.sendmail command (see at the top for my mini code.

Hopefully you solve the code with the specified SMTP server.

Observe this direct conversation with the SMTP server:

[~]fleet2*> telnet smtp
Connected to
Escape character is '^]'.
220 ESMTP server ready
helo fleet
250 hello [], pleased to meet you
mail from:<>
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.

Cameron Simpson

Yeah, with login method it does work fine!
Now I wonder how I can pass the login like in PHP, because I dislike to keep the password in my program.

Very good idea.Keeping this information in a configuration is always to be preferred. Maybe you are spoiled for choice? :wink:
For a .ini file:
For a yaml file:

For a toml file:

This format may appear in the Standard Library, it is being used by pypi packaging.

For json:

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.

The use is outlines in the netrc modue docs:

but basicly:

from netrc import netrc
entry = netrc().hosts.get("username@service")
if entry is None:
    ... no such entry ...
    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:

machine xs4all
    login ************
    password *************

and look this up from your programme as “xs4all”, then use the resulting
login/password in your SMTP login() call.

Cameron Simpson