2

I tried to install and configure SMTP server. and it seems to me it happened. And when I send test mail from linux command line I receive mail, ex :

echo "Test mail from postfix4" | mail -s "Test Postfix4" test@gmail.com

But when i tried to do the same via python i got error :

>>> from email.MIMEText import MIMEText
>>> import smtplib
>>> sender = 'test@mail.com'
>>> to = 'test@gmail.com'
>>> subj = "atata"
>>> body = "ololo"
>>> server = 'hostname'
>>> msg = MIMEText(body, 'plain', 'utf-8')
>>> msg['Subject'] = subj
>>> msg['To'] = to
>>> msg['Importance'] = 'high'
>>> s = smtplib.SMTP(server)
>>> s.sendmail(sender, [to], msg.as_string())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/smtplib.py", line 742, in sendmail
    raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'test@gmail.com': (454, '4.7.1 <test@gmail.com>: Relay access denied')}
>>> s.quit()
(221, '2.0.0 Bye')

Can someone help me with it?

vaultah
  • 44,105
  • 12
  • 114
  • 143
Just Me
  • 133
  • 1
  • 11
  • 2
    Is `server = 'hostname'` your real SMTP server address? Have you tried `localhost`? Or, if this SMTP relay requires authentication (and it must!), try to use `server.login(email_from, 'your_password_here')` before `server.sendmail(...)` call. – Ivan Velichko Jan 16 '16 at 18:01
  • hello, thx for the help. Yes hostname is real, and yes i tried with localhost. the same error – Just Me Jan 16 '16 at 18:17
  • 1
    And have you tried `server.login(...)`? – Ivan Velichko Jan 16 '16 at 18:19
  • I also think `server.login(...)` should fix it, but you should also check your postfix configuration (as shown at http://unix.stackexchange.com/a/37021) to see if it has a login/password for the mail relay. Note that if you're trying to send from Google's SMTP server, it needs authentication. – Steve Jan 16 '16 at 18:21

1 Answers1

1

I asnwered a similar question here: In particular note that calling the mail command on the command-line is doing something significantly different from using SMTP.sendmail() from smtplib:

  • The mail command bypasses SMTP, instead injecting mail locally; the machine is typically configured to do something sensible with the injected email according to its mail configuration. In a setup such as you've described you could think of that as being a back-door into the mail server (Postfix here): It doesn't need SMTP to deposit the message with Postfix; Postfix takes over from there.

  • SMTP.sendmail() by contrast doesn't have that back-door access, instead your Python program is setting up an SMTP connection to Postfix on your local machine (localhost): Your Postfix setup needs to be persuaded that this incoming SMTP connection is trustworthy enough to accept it and then do something useful with the message. This is typically by having your Python program authenticate (see the server.login() comments to your question above), and/or using a different port/service: "mail submission", often on port 587, and/or having a very lax Postfix configuration that accepts anything - or perhaps anything originating from localhost.

Also worth noting is that the error message you're seeing, 4.7.1 <test@gmail.com>: Relay access denied' will be coming from your local system - and not from Gmail.

For issues like this it's really helpful to understand what "mail relaying" is, and to be very aware of which machine any error message is coming from: It's easy to jump to the wrong conclusion here if you think the error message is coming from the remote system.

Andrew Richards
  • 1,392
  • 11
  • 18