Search code examples
pythonstringemailsmtplib

How to get instance variable recognized as string? For an email?


A class I created has a function .email_template that produces a string.

In my code, I have stored instance variables in a list, inst. As such, inst[randint(1,100)].email_template() produces a string.

I'm using smtplib in Python to send an email containing the string produced by inst[randint(1,100)].email_template(). However, this piece of code that determines the content of the email is tripping me up:

msg = str(inst[randint(1,100)].email_template()) server.sendmail(From, Recipient, msg)

The msg argument in the second line has to be a string, which is why I used the str()method in a beginners attempt to make this go through. The email works, but the body simply says "None" when there should be a string appearing (and it does appear successfully on the console).

Going back to the title of this question, how can I get my instance variable code recognized as a string so that it successfully gets printed on an email?


Solution

  • As thefourtheye put forward, my issue was printing the text in email_template, not returning it. Problem solved!