简体   繁体   中英

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!). I've simplified it for the purposes of getting it working. I have SSH access. The script is located in my cgi-bin folder. The from email address is a Godaddy domain one (required apparently). I have it working on my test home server (using gmail's smtp server).

#! /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.

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.

#!/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. then host this python scripts on heroku and make this scripts to schedule run with heroku scheduler adds on.

see this link for running and hosting the scripts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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