-1

重複の可能性:
Python でメールを送信する

Pythonでメールを送信しようとしていますが、スクリプトを実行すると1〜2分かかり、次のエラーが表示されます:

Traceback (most recent call last):
File "./emailer", line 19, in <module>
server = smtplib.SMTP(SERVER)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 512, in create_connection
raise error, msg
socket.error: [Errno 60] Operation timed out

これはスクリプトです:

#!/usr/bin/env python

import smtplib

SERVER = 'addisonbean.com'

FROM = 'myemail@gmail.com'
TO = ['myemail@gmail.com']

SUBJECT = 'Hello!'

message = """\
Bla
Bla Bla 
Bla Bla Bla
Bla Bla Bla Bla
"""

server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()

サイトの IP アドレスもサーバーとして試しましたが、同じことが起こりました。

誰かがこれを行う理由とこれを修正する方法を教えてもらえますか? ありがとう!

4

3 に答える 3

2

キービットは次のとおりです。

return socket.create_connection((port, host), timeout)
socket.error: [Errno 60] Operation timed out

Python のことわざ: I can't connect to that server, I've started but it doesn't like to response .

2 番目の重要なビットは次のとおりです。

SERVER = 'addisonbean.com'

それはメールサーバーではありませんね。

于 2012-08-01T23:45:01.957 に答える
1

25 ポートと応答をaddisonbean.comリッスン220 accra.dreamhost.com ESMTPしますが、プロキシまたは何らかのファイアウォールの背後にいるようです。コンソールからできますtelnet addisonbean.com 25か?

于 2012-08-01T23:52:00.740 に答える
0

It looks like you're hosting your page in dreamhost.com, a hosting provider.

When you set up your account, they probably gave you the chance to create email accounts ending with your domain (yaddayadda@addisonbean.com)

You may want to create one, get the information: host where the SMTP (the "mail server") is located, username, password... And you'll have to fill all that in your script.

I would recommend you start testing with another regular account (Gmail.com, Hotmail Outlook.com) and that you read (quite a bit) about what an SMTP server is (which is the server you'll have to talk to in order to have your email sent)

Here's a simple script that should send emails using a gmail account. Fill the information that is shown with asterisks with your data, see if it works:

#!/usr/bin/env python

import traceback
from smtplib import SMTP
from email.MIMEText import MIMEText

smtpHost = "smtp.gmail.com"
smtpPort = 587
smtpUsername = "***@gmail.com"
smtpPassword = "***"
sender = "***@gmail.com"

def sendEmail(to, subject, content):
    retval = 1
    if not(hasattr(to, "__iter__")):
        to = [to]
    destination = to

    text_subtype = 'plain'
    try:
        msg = MIMEText(content, text_subtype)
        msg['Subject'] = subject
        msg['From'] = sender # some SMTP servers will do this automatically, not all

        conn = SMTP(host=smtpHost, port=smtpPort)
        conn.set_debuglevel(True)
        #conn.login(smtpUsername, smtpPassword)
        try:
            if smtpUsername is not False:
                conn.ehlo()
                if smtpPort != 25:
                    conn.starttls()
                    conn.ehlo()
                if smtpUsername and smtpPassword:
                    conn.login(smtpUsername, smtpPassword)
                else:
                    print("::sendEmail > Skipping authentication information because smtpUsername: %s, smtpPassword: %s" % (smtpUsername, smtpPassword))
            conn.sendmail(sender, destination, msg.as_string())
            retval = 0
        except Exception, e:
            print("::sendEmail > Got %s %s. Showing traceback:\n%s" % (type(e), e, traceback.format_exc()))
            retval = 1
        finally:
            conn.close()

    except Exception, e:
        print("::sendEmail > Got %s %s. Showing traceback:\n%s" % (type(e), e, traceback.format_exc()))
        retval = 1
    return retval

if __name__ == "__main__":
    sendEmail("***@gmail.com", "Subject: Test", "This is a simple test")

Once you have the equivalent information for your domain (smtpHost, smtpPort, smtpUsername...) it MAY work as well (depends on the port they're using, it may be 25, which is the default for non-encrypted connections... or not... You'll have to check with dreamhost.com for that)

Be aware that (since you're using a hosting that probably shares its SMTP server with other people) your "sender" may be yaddayadda@addisonbean.com but the actual information to connect to the dreamhost.com SMTP servers may be different: I'm guessing the 'smtpUsername' may be the username you use to login in your site admin, the 'smtpHost' may change to something like smtp.dreamhost.com or such... That I don't really know.

You have a lot of resources on how to do that.

You also seem to be a designer or photographer... One of those dudes people concern on how things look on the screen and all... Then you may wanna investigate what MiME emails are. You know... so the email is not sent with text only, but you can put fancy HTML in it... You know what I'm sayin'?

于 2012-08-02T00:39:28.630 に答える