Python-发送电子邮件时出错(拒绝中继访问) [英] Python - Error (Relay access denied) while sending email

查看:51
本文介绍了Python-发送电子邮件时出错(拒绝中继访问)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用python sendmail发送电子邮件时出现错误.这是我的python代码:(目标)

I am having error when trying to send an email with python sendmail. Here is my python code: (Pas d’objet)

#! /usr/bin/python

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# me == my email address
# you == recipient's email address
me = "my@email.com"
you = "email@gmail.com"

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
  <head></head>
  <body>
    <p>Hi!<br>
       How are you?<br>
       Here is the <a href="[http://www.python.org">link</a]http://www.python.org">link</a> you wanted.
    </p>
  </body>
</html>
"""

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)

# Send the message via local SMTP server.
s = smtplib.SMTP('localhost')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()

发送此电子邮件时,出现此错误:

When I send this email, I have this error:

Traceback (most recent call last):
  File "./mail.py", line 47, in <module>
    s.sendmail(me, you, msg.as_string())
  File "/usr/lib/python2.7/smtplib.py", line 747, in sendmail
    raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'email@gmail.com': (454, '4.7.1 <email@gmail.com>: Relay access denied')}

我真的不知道为什么这在生产环境中不起作用,因为它在测试环境中起作用.您能帮忙了解一下吗?

I really don't know why this is not working in production since it is working in a test environment. Can you help understand, please?

推荐答案

拒绝中继访问消息很重要:听起来好像您已成功建立SMTP连接(与 localhost 中的示例代码),但该计算机拒绝接受该消息:您可能需要进行调整,

The Relay access denied message is important: It sounds like you're successfully making an SMTP connection (to localhost in your sample code), but that machine is refusing to accept the message: You likely need to adjust either,

  • 您在 localhost
  • 上的邮件服务器配置
  • 您的代码以您的 localhost 邮件服务器将接受它的方式发送电子邮件.
  • your mail server configuration on localhost
  • your code to send email in such a way that your localhost mail server will accept it.

请特别注意,发送到端口25(SMTP的默认端口)的邮件通常用于传入电子邮件.在您的情况下,您希望邮件服务器接受该消息并将其发送到其他地方(中继",因此是错误消息).看来您的邮件服务器(此处为 localhost )未设置为接受非本地域的邮件……至少不在端口25上.

In particular note that mail to port 25 (the default port for SMTP) is typically for incoming email. In your case you want the mail server to accept the message and send it on elsewhere ("relaying", thus the error message). It seems that your mail server (localhost here) isn't setup to accept messages for non-local domains... at least not on port 25.

SMTP的另一个重要用途是邮件提交",这通常是在不同的端口上设置的(通常是587,如果是SSL加密的,则是465).通过在命令行上使用telnet来查看是否是这种情况;如果服务器响应,还可以使用 EHLO< some_hostname> 查看服务器提供的功能(有趣的是 AUTH STARTTLS ).端口,例如

The other important use of SMTP is "mail submission", and this is typically setup on a different port (normally 587, or 465 if SSL encrypted). See if this is the case by using telnet on the command line; also use EHLO <some_hostname> to see what the server offers (interesting for your issue are AUTH and STARTTLS), if it responds on the port, for example,

$ telnet localhost 587
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 localhost.example.org ESMTP Postfix (Ubuntu)
ehlo localhost.example.org
250-localhost.example.org
250-VARIOUS_FEATURES_ON_LINES_LIKE_THIS
250-STARTTLS
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
QUIT
221 2.0.0 Bye
Connection closed by foreign host.

也对端口25进行此操作并比较结果:这应该为您提供进行操作的线索-例如,您可能需要 AUTH ,或使用端口587,或者您可能需要进行调整您的( localhost )邮件服务器配置,使其充当智能主机";将电子邮件转发到网络".

Do this for port 25 too and compare the results: That should give you clues on how to proceed - maybe you need to AUTH for example, or use port 587, or you may need to adjust your (localhost) mail server configuration so it acts as a "smarthost" to forward email out to the 'net.

这篇关于Python-发送电子邮件时出错(拒绝中继访问)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆