Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set up a localhost in python?

The project is to be able to send email from within Python. I can successfully send to, for example, "smtp.gmail.com", but using a localhost or 127.0.0.1 returns an "errno 111, connection was refused." The statement is

    server = smtplib.SMTP('127.0.0.1', 8025)

Notes: I am running Ubuntu, Python 3.2 or Python 2.7 depending on what the wonderful people here know best, and my programming knowledge could be charitably described as limited.

like image 315
Celer Avatar asked Dec 20 '25 21:12

Celer


2 Answers

First, test your local SMTP connection using Telnet. (Alternate instructions here) This will tell if you can connect to your local SMTP server in the way you think you can.

You probably have one of several problems:

  1. No SMTP server on your machine or;
  2. You have SMTP but it requires a password and/or;
  3. You have SMTP on your local machine but it is not configured properly or configured to support SMTP locally -> Internet or;
  4. The machine's current IP or DNS setup or;
  5. The way you are calling the local SMTP server from Python.

Python SMTPlib supports authorized connections using LMTP for issue 2.

Try using port 25 for issue 5.

For the other issues, you will need to find or setup a cooperative SMTP server.

BTW: properly configuring and securing an SMTP server is not trivial...

like image 80
the wolf Avatar answered Dec 22 '25 10:12

the wolf


What you're attempting to do, is using the local machine as the smtp-server. The simplest way to achieve this in Ubuntu, would be to install one of the exim4 or postfix-packages in apt / synaptic ...

Make sure that when prompted, that you don't allow machines you don't trust to use your host as a mail-relay. The defaults should be sufficient to avoid this - yet still allow mail to be sent from your machine.

Also note that the default port-number for smtp is port 25, and not 8025 as in your code example.

like image 20
Kvisle Avatar answered Dec 22 '25 11:12

Kvisle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!