#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    TypeError: coercing to Unicode: need string or buffer, tuple found


    This is my very first try at PYTHON!

    I downloaded a PYTHON script from the internet to send email using Gmail.

    The code as downloaded works perfectly but I replaced the raw_input from console by code to read the data from an XML file.

    This throws an error:

    Traceback (most recent call last):
    File "C:\SendGmail.py", line 77, in <module>
    newmail.mailprep(mailto,mailsubj,mailmsg)
    File "C:\SendGmail.py", line 44, in mailprep
    part.set_payload(open(attach, 'rb').read())
    TypeError: coercing to Unicode: need string or buffer, tuple found
    AttachFile
    {} E:\Emailer\AppKeys.dat
    AttachFile
    {} E:\Emailer\EmailerTest.xml
    ['', ({}, 'E:\\Emailer\\AppKeys.dat'), ({}, 'E:\\Emailer\\EmailerTest.xml')]

    The XML file:

    <?xml version="1.0" encoding="UTF-8" ?>
    <Email>
    <SendTo>me@gmail.com</SendTo>
    <Subject>Test Emailer program</Subject>
    <Body>Testing sending an email using PYTHON with 2 attachments.</Body>
    <Attachments>
    <AttachFile>E:\Emailer\AppKeys.dat</AttachFile>
    <AttachFile>E:\Emailer\EmailerTest.xml</AttachFile>
    </Attachments>
    </Email>

    This is the code(The changed code is towards the bottom):

    import smtplib

    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEBase import MIMEBase
    from email.MIMEText import MIMEText
    from email import Encoders
    import os

    ver = "v1.0"
    gmail_user = "me@gmail.com" #Username
    gmail_pwd = "pwpwpw" #Password
    gmail_alt = " " #Alias ID
    gmail_alias = ""+gmail_alt #nickname
    mailing_list = ""
    class mailz:
    def __init__(self):
    self.files=[""]
    self.mail = MIMEMultipart()

    def attach(self,fil):
    self.files.append(fil)

    def mailprep(self,to, subject, text):
    self.to = to
    self.mail['From'] = gmail_alias
    self.mail['To'] = to
    self.mail['Subject'] = subject
    if (mailtoBcc !="") :
    self.mail['BCC'] = mailtoBcc

    self.mail.attach(MIMEText(text))
    print self.files

    for attach in self.files:
    if (attach != ""):
    part = MIMEBase('application', 'octet-stream')
    part.set_payload(open(attach, 'rb').read())
    Encoders.encode_base64(part)
    part.add_header('Content-Disposition','attachment; filename="%s"' % os.path.basename(attach))
    self.mail.attach(part)

    def sendmail(self):
    self.mailServer = smtplib.SMTP("smtp.gmail.com", 587)
    self.mailServer.ehlo()
    self.mailServer.starttls()
    self.mailServer.ehlo()
    self.mailServer.login(gmail_user, gmail_pwd)
    self.mailServer.sendmail(gmail_user, self.to, self.mail.as_string())
    self.mailServer.close()

    import xml.etree.ElementTree as ET
    tree = ET.ElementTree(file="E:\Emailer\Emailer.xml")


    mailto = tree.findtext("SendTo")
    mailsubj = tree.findtext("Subject")
    mailmsg = tree.findtext("Body")
    mailtoBcc = tree.findtext("SendToBcc")
    mailattch = ""
    newmail = mailz()

    #This code was replaced - see lower down

    #while 1:
    # mailattch = ""
    # c = raw_input("Attach a File ? (y/n)")
    # if c == 'n':
    # break
    # if c != 'y':
    # continue
    # mailattch = raw_input("Enter Path of file: ")
    # newmail.attach(mailattch)

    #The above code was replaced by the following:

    for elem in tree.iterfind("Attachments/AttachFile"):
    print elem.tag
    print elem.attrib,elem.text
    mailattch = elem.attrib,elem.text
    if mailattch == None: break
    newmail.attach(mailattch)

    newmail.mailprep(mailto,mailsubj,mailmsg)

    newmail.sendmail()


    Will you please help me to get this working?

    Era.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    Maybe this would work?
    Code:
    for elem in tree.iterfind("Attachments/AttachFile"):
        print elem.tag
        print elem.attrib,elem.text
        print('attaching %s'%elem.text)
        mailattch = elem.text
        if mailattch == None: break
        newmail.attach(mailattch)
    1) please read the link at my signature to learn about preserving critical white space in posts.
    2) 1,2 is a tuple. newmail.attach expects a string. Your "1,2" is "elem.attrib,alem.text" in
    mailattch = elem.attrib,elem.text
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0
    Bingo!

    You're a star!

    Thank you!

    Era.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    I didn't realize xml was that easy to search. whoa!
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo