6

わかりました、私は何年もの間インターネットを見てきましたが、これに対する答えを見つけることができませんでした. 私は多くの提案を試みましたが、うまくいかないようです。Python (smtplib および email モジュール) と gmail サービスを使用して電子メールを送信しようとしています。インポートしたパッケージは次のとおりです。

import time, math, urllib2, urllib, os, shutil, zipfile, smtplib, sys
from email.mime.text import MIMEText

そして、これが電子メールを送信するための私のdefステートメントです:

def sendmessage():
print('== You are now sending an email to Hoxie. Please write your username below. ==')
mcusername = str(raw_input('>> Username: '))
print('>> Now your message.')
message = str(raw_input('>> Message: '))
print('>> Attempting connection to email host...')
fromaddr = 'x@gmail.com'
toaddrs = 'xx@gmail.com'
username = 'x@gmail.com'
password = '1013513403'
server = smtplib.SMTP('smtp.gmail.com:587')
subject = 'Email from',mcusername
content = message
msg = MIMEText(content)
msg['From'] = fromaddr
msg['To'] = toaddrs
msg['Subject'] = subject
try:
    server.ehlo()
    server.starttls()
    server.ehlo()
except:
    print('!! Could not connect to email host! Check internet connection! !!')
    os.system('pause')
    main()
else:
    print('>> Connected to email host! Attempting secure login via SMTP...')
    try:
        server.login(username,password)
    except:
        print('!! Could not secure connection! Stopping! !!')
        os.system('pause')
        main()
    else:
        print('>> Login succeeded! Attempting to send message...')
        try:
            server.sendmail(fromaddr, toaddrs, msg)
        except TypeError as e:
            print e
            print('Error!:', sys.exc_info()[0])
            print('!! Could not send message! Check internet connection! !!')
            os.system('pause')
            main()
        else:
            server.quit()
            print('>> Message successfully sent! I will respond as soon as possible!')
            os.system('pause')
            main()

私はあえてこれを得るのと同じくらい広範囲にデバッグしました:

>> Login succeeded! Attempting to send message...
TypeError: expected string or buffer

つまり、ログインには成功しましたが、メッセージの送信を試みたときに停止しました。私を困惑させることの 1 つは、それがどこを指していないかということです。また、私のコーディングはそれほど優れていない可能性があるため、サイバーいじめはありません.

どんな助けでも大歓迎です!ありがとう。

4

2 に答える 2

7

クラッシュしている行は

server.sendmail(fromaddr, toaddrs, msg)

2 つの文字列と MIMEText インスタンスを指定しています。文字列の形式でメッセージが必要です。[リストの形式のアドレスも必要だと思いますが、1 つの文字列を特別に扱います。] たとえば、ドキュメントの例を見ることができます。

s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()

sendmail を満足させるには、MIMEText を文字列に変換する必要があります。@jdi が指摘した件名のバグ (「AttributeError: 'tuple' object has no attribute 'lstrip'」メッセージが生成される) を修正し、msg をmsg.as_string()に変更すると、コードが機能します。

于 2012-01-30T07:17:18.280 に答える
3

My guess is the culprit is this line:

subject = 'Email from',mcusername

If you are expecting to create subject as a string, its actually being made into a tuple because you are passing two values. What you probably wanted to do is:

subject = 'Email from %s' % mcusername

Also, for the debugging aspect... The way you are wrapping all of your exceptions and just printing the exception message is throwing away the helpful traceback (if there is one). Have you tried not wrapping everything until you really know the specific exception you are trying to handle? Doing blanket catch-all exception handling like that makes debugging harder when you have syntax bugs.

于 2012-01-30T07:04:35.110 に答える