Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - smtp requires authentication [closed]

I am trying to send an email using python but despite I am using the local SMTP server it seems that it needs authentication. The code I run and the error I get can be seen below. I use port 587, because port 25 cannot be opened on my server. Could you please help me on setting up the local SMTP server using python on port 587?

>>> import smtplib
>>> from email.mime.text import MIMEText
>>> msg = MIMEText('Test body')
>>> me = '[email protected]'
>>> to = '[email protected]'
>>> msg['Subject'] = 'My Subject'
>>> msg['From'] = me
>>> msg['To'] = to
>>> s = smtplib.SMTP('localhost', 587)
>>> s.sendmail(me, [to], msg.as_string())

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/smtplib.py", line 722, in sendmail
    raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (530, '5.7.0 Authentication required', '[email protected]')
like image 566
Paris Avatar asked Jan 07 '13 13:01

Paris


People also ask

How does SMTP work in Python?

Simple Mail Transfer Protocol (SMTP) is a protocol, which handles sending e-mail and routing e-mail between mail servers. Python provides smtplib module, which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.

Is Smtplib part of Python?

smtplib is Python's built-in module for sending emails to any Internet machine with an SMTP or ESMTP listener daemon. I'll show you how to use SMTP_SSL() first, as it instantiates a connection that is secure from the outset and is slightly more concise than the . starttls() alternative.


1 Answers

Then before you attempt to use s.sendmail, you use s.login('user', 'password') - http://docs.python.org/2/library/smtplib.html#smtplib.SMTP.login

If you don't have login details - then consult your system admin.

like image 196
Jon Clements Avatar answered Sep 27 '22 22:09

Jon Clements