Sending emails via python

I tried to install and configure the SMTP server. and it seems to me that this happened. And when I send test mail from linux command line, I get mail, for example:

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

But when I tried to do the same through python, I got an 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?

+1
source share

Source: https://habr.com/ru/post/1683681/


All Articles