Sending mails via python

做~自己de王妃 提交于 2019-12-10 22:39:00

问题


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?

来源:https://stackoverflow.com/questions/34830169/sending-mails-via-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!