Array and MIMEText - Python

0

Why can not I use Array to add inside a variable of type MIMEText (msg = MIMEText (body, 'html'))?

I explain:

for row in cursor.fetchall():
    for owner in cursor.execute("select * from orcam where Ccusto=? and anomes=?", row.Ccusto, cdate):
        print owner
    if owner.Vr_real > percFor and owner.Vr_Forecast > 0:
                    print(owner.Ccusto, owner.Grupo, owner.Anomes)
                    body.append("""a""")
                    print i
                    print body[i]
                    i =  i + 1
    try:
        msg = MIMEText(body, 'html')
        msg["From"] = emailfrom
        msg["Subject"] = "XXXXXXXX"
        msg["To"] = emailto
        server = smtplib.SMTP('server',25)
        server.starttls()
        server.sendmail(emailfrom, emailto.split(';'), msg.as_string())
        server.quit()
        print "Successfully sent email!"
        server.close()
    except Exception:
        print "Error: unable to send email"
        server.close()
        server.quit()   

My intention is to make every time that the "if" is true it will add inside the body of an email. So I tried to do it in array form, it even adds the messages inside the array array, but at the time of adding inside MIMEText it brings me the error.

['a', 'a', 'a']
Error: unable to send email
Traceback (most recent call last):
  File ".\orcam_1.py", line 71, in <module>
    server.close()
AttributeError: 'str' object has no attribute 'close'

Is there any other way to add messages in the body of the email, as you enter the IF?

    
asked by anonymous 12.09.2017 / 01:59

1 answer

1

You need to pass a string as the first MIMEText parameter, not an array. You would concatenate all the strings contained in your array, to form a single string, as follows:

msg = MIMEText(' '.join(body), 'html')

The join function concatenates the strings contained in an Array by placing the separator in which the function was called, which in this case is a blank space. You could put any other separate ones.

    
12.09.2017 / 04:47