まあまあ、私は年齢のインターネットを見て、これに対する答えを見つけることができませんでした。私は多くの提案を試みましたが、うまく動作しないようです。私はpython(smtplibとemailモジュール)とgmailサービスを使って電子メールを送信しようとしています。ここに私のインポートされたパッケージは、次のとおりです。Python電子メールの送信TypeError:予想される文字列またはバッファー
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 = '[email protected]'
toaddrs = '[email protected]'
username = '[email protected]'
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
をこれは、ログインに成功したが、メッセージを送信しようとしたときに停止したことを意味します。 私の話は、どこを指さないかということです。また、私のコーディングはそれほど大きくないので、サイバーのいじめはありません。
ご協力いただければ幸いです!ありがとう。
subject = 'Email from',mcusername
あなたは2つの値を渡しているので、そのタプルが実際に行われて、文字列として主題を作成するために期待している場合: