简体   繁体   English

在Godaddy共享托管中使用Python脚本发送电子邮件

[英]Sending email using Python script in Godaddy shared hosting

I'm trying to send an email (via a contact form) on my Godaddy shared hosting account (I know!). 我正在尝试通过Godaddy共享托管帐户(我知道!)发送电子邮件(通过联系表格)。 I've simplified it for the purposes of getting it working. 我已对其进行了简化,以使其正常运行。 I have SSH access. 我有SSH访问权限。 The script is located in my cgi-bin folder. 该脚本位于我的cgi-bin文件夹中。 The from email address is a Godaddy domain one (required apparently). “发件人”电子邮件地址是一个Godaddy域(显然是必需的)。 I have it working on my test home server (using gmail's smtp server). 我有它在我的测试家庭服务器上工作(使用gmail的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)

I'm getting the following error: 我收到以下错误:

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

I've been told the Godaddy smtp server settings are correct and have tried a few combinations reading other similar questions on Stackoverflow. 有人告诉我Godaddy smtp服务器设置正确,并尝试了几种组合来阅读Stackoverflow上的其他类似问题。

It now works. 现在可以使用了。 The solution as far as I can tell was to set up email in the Godaddy Myaccount rather than cpanel and ensure the MX record is correct and make a couple of changes to the smtp module code. 据我所知,解决方案是在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)

You Can User SendGrid APIs with Python Scripts to send emails, just write a scripts that connect with your db and do whatever functions you want to send. 您可以使用带有Python脚本的SendGrid API用户来发送电子邮件,只需编写一个与数据库连接并执行您想发送的功能的脚本即可。 then host this python scripts on heroku and make this scripts to schedule run with heroku scheduler adds on. 然后将这个python脚本托管在heroku上,并使该脚本在带有heroku scheduler的情况下进行调度运行。

see this link for running and hosting the scripts 请参阅此链接以运行和托管脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM