我正在尝试使用python向我的服务器发送电子邮件,我问我的服务器提供商使用哪个端口,他们说“选择SMTP端口465(安全SSL / TLS传出服务器),25(非SSL传出)服务器)。”不知道这究竟意味着什么,但目前我使用的是25,这是我的代码
#! /usr/bin/python
import smtplib
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 25)
#Next, log in to the server
server.login("youremailusername", "password")
#Send the mail
msg = "\nHello!" # The /n separates the message from the headers
server.sendmail("you@gmail.com", "target@example.com", msg)
我填写了我的用户名(这是我的电子邮件地址权限)和密码,dn是目标但是它不起作用,当我尝试导航到我的py脚本所在的url时,它只是不加载。我有一个互联网连接,因为我喜欢其他的东西,并转到我的服务器上的其他页面。我也试过用cron作业运行,但这也行不通。
脚本的权限是0755,脚本有问题吗?
当我使用cron作业运行时,这里是追溯
Traceback (most recent call last):
File "/home/spencerf/public_html/cgi-bin/send_email.py", line 6, in <module>
server = smtplib.SMTP('smtp.gmail.com', 25)
File "/usr/lib64/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib64/python2.6/socket.py", line 567, in create_connection
raise error, msg
socket.error: [Errno 101] Network is unreachable
感谢您的帮助。
修改
这是更新的错误日志,端口为587
Traceback (most recent call last):
File "/home/spencerf/public_html/cgi-bin/send_email.py", line 7, in <module>
server = smtplib.SMTP('smtp.gmail.com', 587)
File "/usr/lib64/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib64/python2.6/socket.py", line 567, in create_connection
raise error, msg
socket.error: [Errno 101] Network is unreachable
编辑2
当我有server = smtplib.SMTP('telnet smtp.gmail.com', 587)
我收到了这个错误
Traceback (most recent call last):
File "/home/spencerf/public_html/cgi-bin/send_email.py", line 8, in <module>
server = smtplib.SMTP('telnet smtp.gmail.com', 587)
File "/usr/lib64/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib64/python2.6/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
答案 0 :(得分:0)
<强> BUG:强>
首先,您使用gmail服务器而不是公司服务器发送邮件。对于gmail服务器,输出端口为587
代码:
Due to security issues gmail blocks accessing mail via code or program
但是,如果您执行以下操作,仍然可以使用gmail通过代码发送邮件
我在代码中做了什么:
1.添加了一个错误对象以获取错误消息
import smtplib
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
#Next, log in to the server
server.login("youremailusername", "password")
#Send the mail
msg = "\nHello!" # The /n separates the message from the headers
server.sendmail("you@gmail.com", "target@example.com", msg)
print "Successfully sent email"
except smtplib.SMTPException,error:
print str(error)
print "Error: unable to send email"
如果您运行此代码,您会看到一条错误消息,说明谷歌不允许您通过代码登录
gmail中要改变的事情:
1.登录gmail
2.转到此链接https://www.google.com/settings/security/lesssecureapps
3.单击“启用”,然后重试代码
希望有所帮助:)
但如果你启用它就会有安全威胁
<强>更新强>
import smtplib
try:
content = 'test'
mail = smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login("ABC@gmail.com", "password")
mail.sendmail("ABC@gmail.com", "recivermailaddress", content)
mail.quit
print "Successfully sent email"
except smtplib.SMTPException,error:
print str(error)
答案 1 :(得分:0)