dimanche 1 février 2015

smtplib sometimes can't send a string variable from sqlite concatenated to a string literal

I am trying to send a message in Python with smtplib. I have a sendEmail(message) function:



def sendEmail(message)
server = smtplib.SMTP_SSL('mailserver.example.com', 465)
server.ehlo()
server.login("username", "password")
print message
msg = message
fromaddr = "myaddr@someplace.com"
toaddr = "theiraddr@someotherplace.com"
server.sendmail(fromaddr, toaddr, msg)
server.quit()


I am trying to do something like this:



myMessage = "A String Literal" + someString
sendEmail(myMessage)


But when I do that I sometimes receive an empty email message. someString is a unicode string pulled from sqlite. I have tried various string concatenations combining literals with literals, sqlite strings with sqlite strings, sqlite strings with literals, each by themselves. I have used + and .join and anything else I can think of to join the strings. It seems like everything works sometimes but nothing works every time.


The "print message" inside sendEmail function always prints the string I expect. smtplib always sends the email message to the correct email address without complaining, it is just sometimes an empty message.


Since I have had everything work and also not work, I don't really trust any solution I come up with if I don't fully understand what is going on. Can anybody help me understand what is happening with these strings so I can construct my message in the most appropriate way and be confident that my application will work reliably?


Aucun commentaire:

Enregistrer un commentaire