我试图在我的Godaddy共享主机帐户上发送电子邮件(通过联系表单)(我知道!)。为了让它正常工作,我已对其进行了简化。我有SSH访问权限。该脚本位于我的cgi-bin文件夹中。来自电子邮件地址是Godaddy域名(显然需要)。我让它在我的测试家庭服务器上工作(使用gmail&#s; smtp服务器)。
#! /usr/bin/python
import smtplib
import string, sys
import cgitb
cgitb.enable()
sys.stderr = sys.stdout
print 'Content-Type: text/plain'
print
HOST = "relay-hosting.secureserver.net"
FROM = "myemail@mygodaddydomain.co.uk"
TO = "mygmail@gmail.com"
SUBJECT = "Test"
BODY = "Hello"
body = string.join((
"From: %s" % FROM,
"To: %s" % TO,
"Subject: %s" % SUBJECT,
"",
BODY), "\r\n")
print body
server = smtplib.SMTP(HOST, 25)
server.sendmail(FROM, [TO], body)
我收到以下错误:
Traceback (most recent call last):
File "test2.py", line 28, in <module>
server = smtplib.SMTP(HOST, 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
error: [Errno 110] Connection timed out
我被告知Godaddy smtp服务器设置是正确的,并尝试了一些组合阅读Stackoverflow上的其他类似问题。
答案 0 :(得分:0)
它现在有效。据我所知,解决方案是在Godaddy Myaccount而不是cpanel中设置电子邮件,并确保MX记录正确并对smtp模块代码进行一些更改。
#!/usr/bin/python
import smtplib
import string, sys
import cgitb
cgitb.enable()
sys.stderr = sys.stdout
print 'Content-Type: text/plain'
print
FROM = "email@mygodaddydomain.com"
TO = "myemail@gmail.com"
SUBJECT = "Test"
BODY = "Hello"
body = string.join((
"From: %s" % FROM,
"To: %s" % TO,
"Subject: %s" % SUBJECT,
"",
BODY), "\r\n")
print body
server = smtplib.SMTP()
server.connect()
server.sendmail(FROM, TO, body)
答案 1 :(得分:-1)
您可以使用Python脚本用户SendGrid API发送电子邮件,只需编写与您的数据库连接的脚本并执行您要发送的任何功能。然后在heroku上托管这个python脚本,并使这个脚本安排运行heroku scheduler add。
请参阅this link以了解如何运行和托管脚本