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?
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.
– user2287892
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.
– luislhl
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
– jsbueno
the real error. My suggestion is to use the variation
try/finally
command, with the callsserver.close
andserver.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, useexcept Exception as exc:
and print or print the variableexc
inside the Except block.– jsbueno
@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?
– user2287892
@jsbueno, thanks for the tip. I’ll do it!
– user2287892