W porządku, szukałem w internecie od wieków i po prostu nie mogłem znaleźć odpowiedzi na to pytanie. Próbowałem wielu sugestii, ale nie mogę tego zmusić do działania. Próbuję wysłać e-mail przy użyciu Pythona (moduły smtplib i e-mail) i usługi Gmail. Oto moje importowane pakiety:Wysyłanie e-maili w języku Python TypeError: Oczekiwany ciąg lub bufor
import time, math, urllib2, urllib, os, shutil, zipfile, smtplib, sys
from email.mime.text import MIMEText
i tutaj jest moje oświadczenie def do wysyłania e-mail:
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()
Mam debugowany tak obszernie jak śmiem i otrzymaj:
>> Login succeeded! Attempting to send message...
TypeError: expected string or buffer
Co oznacza, że udało się zalogować, ale zatrzymał, gdy próbował wysłać wiadomość. Jedną z rzeczy, która mnie dręczy, jest to, że nie wskazuje ona gdzie. Również moje kodowanie może nie być tak wspaniałe, więc nie będzie szykanowania.
Każda pomoc będzie bardzo ceniona! Dzięki.