简体   繁体   中英

django send_mail 554 - relay access denied

I googled a lot about this problem, and i found this error in a plenty of locations, but no answer for my problem.

I' ve a newly installed (debian) system, which contains basically only a django (1.3.0) installation, and a very poor postfix. Postfix is working, if i do

echo sth | mail somebody@provider.com

it arrives perfectly.

I tried to use django send_mail like

from django.core.mail import send_mail

send_mail('aaa', 'bbb', 'from@localhost', ['user@external.hu'])

but I get back always an error:

Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.6/dist-packages/django/core/mail/__init__.py", line 61, in send_mail
    connection=connection).send()
  File "/usr/lib/python2.6/dist-packages/django/core/mail/message.py", line 248, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/usr/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py", line 92, in send_messages
    sent = self._send(message)
  File "/usr/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py", line 110, in _send
    email_message.message().as_string())
  File "/usr/lib/python2.6/smtplib.py", line 709, in sendmail
    raise SMTPRecipientsRefused(senderrs)
SMTPRecipientsRefused: {'user@external.hu': (554, '5.7.1 : Relay access denied')}

Certainly I tried with different addresses. The only one cases when he delivers the email is the one, when I send it to localhost (or to hosts having mx on this machine).

In the mail.log I can see this:

Apr 16 10:48:48 tkt postfix/smtpd[4886]: connect from tkt[217.112.142.235]
Apr 16 10:48:48 tkt postfix/smtpd[4886]: NOQUEUE: reject: RCPT from tkt[217.112.142.235]: 554 5.7.1 : Relay access denied; from= to= proto=ESMTP helo=
Apr 16 10:48:48 tkt postfix/smtpd[4882]: lost connection after RSET from tkt[217.112.142.235]
Apr 16 10:48:48 tkt postfix/smtpd[4882]: disconnect from tkt[217.112.142.235]

Sniffing port 25 I can see nothing.

Anyone any idea how to solve this problem (using the local postfix most prefereably)?

The machine is on the Internet site, and as I wrote, mail sending works ok from command line.

I'm not 100% sure, but I think the difference between...

echo sth | mail somebody@provider.com

...and...

send_mail('aaa', 'bbb', 'from@localhost', ['user@external.hu'])

...is that the former is connecting to postfix on IP address 127.0.0.1 , but the latter is connecting to postfix on IP address 217.112.142.235 , although I'm not sure why. It might depend on which IP address your webserver is bound to.

Postfix is typically configured by default to only allow email to be relayed when connected to on 127.0.0.1 for security reasons.

You can either change your Django settings.py to force it to use 127.0.0.1 by adding...

EMAIL_HOST='localhost'

...or reconfigure postfix to allow relaying from 217.112.142.235 by changing the mynetworks param in /etc/postfix/main.cf from something like...

mynetworks = 127.0.0.0/8

...to something list...

mynetworks = 127.0.0.0/8 217.112.142.235/32

...then do...

sudo service postfix restart

...to make it re-read the confing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM