Python Email TypeError sending: expected string or buffer

Well, guys, I looked on the Internet for a long time and just could not find the answer to this question. I have tried many suggestions, but I cannot get it to work. I am trying to send an email using python (smtplib and email modules) and gmail service. Here are my imported packages:

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

and here is my def statement for sending email:

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()

I debugged as widely as I dared, and received the following:

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

This means that he successfully logged in, but stopped when he tried to send a message. One thing that strikes me is that it does not indicate where. In addition, my coding may not be so big that there is no intimidation on the Internet.

! .

+5
2

, :

subject = 'Email from',mcusername

, , . , , , :

subject = 'Email from %s' % mcusername

, ... , , ( ). , , ? , , .

+3

,

server.sendmail(fromaddr, toaddrs, msg)

MIMEText; . [ , , , .] , :

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

MIMEText , sendmail . , @jdi ( "AttributeError: 'tuple", "lstrip" ) msg msg.as_string(), .

+6

All Articles