使用Python 3发送电子邮件

时间:2017-05-13 14:30:05

标签: python python-3.x smtplib

我正在制作有大量角色的猜谜游戏,它会为您提供猜测角色的提示。还有一个可以提交角色的东西。我想这样做,如果某人提交了一个角色,它会发给我一封电子邮件,但它不起作用。这是我的代码:

import smtplib

FROM = 'Guess@Character.com'
TO = ["mikpe120@gmail.com"] # must be a list
SUBJECT = "Somebody submitted a character!"
TEXT = "Name: "  + name + "NameType: " + nameType

# Prepare actual message
message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

# Send the mail
server = smtplib.SMTP('myserver')
server.sendmail(FROM, TO, message)
server.quit()

这是我得到的错误:

Traceback (most recent call last):
  File "C:/Users/Mikolaj Perzyna/Desktop/addCharacter.py", line 106, in <module>
    server = smtplib.SMTP('myserver')
  File "C:\Users\Mikolaj Perzyna\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Users\Mikolaj Perzyna\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 335, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Users\Mikolaj Perzyna\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 306, in _get_socket
    self.source_address)
  File "C:\Users\Mikolaj Perzyna\AppData\Local\Programs\Python\Python36\lib\socket.py", line 704, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Users\Mikolaj Perzyna\AppData\Local\Programs\Python\Python36\lib\socket.py", line 743, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

对不起,如果我将这个帖子格式化错误,我可能就是这样,但这是我在网站上的第一个问题。

2 个答案:

答案 0 :(得分:0)

getaddrinfo failed表示套接字连接smptlib.SMTP使用失败,无法解析服务器的主机名。尝试使用其他SMTP服务器或其他IP /主机名。

答案 1 :(得分:0)

这条线几乎肯定是错的。

server = smtplib.SMTP('myserver')

您的SMTP服务器是否真的名为myserver?我希望在那里看到类似mail.mydomain.com的内容。