Array and Mimetext - Python

Asked

Viewed 244 times

0

Why it is not possible to use Array to add inside a Mimetext type variable (msg = Mimetext(body, 'html'))?

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 that every time the "if" is true it will be adding inside the body of an email. Therefore, I tried to do in array form, it even adds the messages inside the body 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?

1 answer

1

You need to pass a string as the first Mimetext parameter, not an array. You could 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, placing between them the separator on which the function was called, which in this case is a blank space. You could put any other apart.

  • It worked Luis! Just one last question, is there no way to pass more than 1 Mimetext as an argument for msg.as_string()? My intention was to divide the email into two parts. One inside the if and the other outside.

  • I’m not sure I understand. If you want to create two email snippets in parallel, just create another array, a body2 for example, and append it to build the second snippet. Then turn the two into strings, using . Join() in each, and add the two resulting strings.

  • Another thing there is that your Try/except construction is masking the real error message: the "string has no attribute close" error Voce has suggests that there is something very wrong with what you imagine your "server" object would be. - but since Voce uses the `except Exception`` and does nothing with the error of this first block it is impossible to know what is

  • the real error. My suggestion is to use the variation try/finally command, with the calls server.close and server.quit within the Finally block - and let the exception happen, without an except, at least until you have debugged that stretch. Once you have tidied up, use except Exception as exc: and print or print the variable exc inside the Except block.

  • @luislhl, basically, I would make another Join (string) and then join these two strings into one, and then move on to Mimetext, that’s right?

  • @jsbueno, thanks for the tip. I’ll do it!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.