message_text = ["text1\n", variable1, "\ntext2", variable2, "\ntext3", variable3]
u = MIMEText(message_text[0][1][2][3][4][5])
u['Subject'] = 'title of email'
s.sendmail("[email protected]", "[email protected]", u.as_string())
I got the error on this line:
u = MIMEText(message_text[0][1][2][3][4][5])
The error:
IndexError: string index out of range
What should I do?
When creating u
, I loaded the indices 0, 1, 2, 3, 4, 5
from message_text
, but that created the error.
CodePudding user response:
It looks like you are using a multi-dimensional array. Here's what python sees.
Take message_text[0]
which is "text1\n"
Now take message_text[0][1]
which is "e"
Now with message_text[0][1][2]
we try to find what is the third element in "e"
This doesn't make sense, because the string "e"
only has one element: the character 'e'
.
If you want to add them all together, you'll need to convert your variables into text, and string them together.
u = MIMEText(message_text[0] str(message_text[1]), message_text[2], str(message_text[3]), message_text[4], str(message_text[5]))
Converts all your variables into one long string before passing it the MIMEText command.
CodePudding user response:
Not entirely sure how the MIMEText function works, but I think it takes in a text string instead of a list of strings. You can try writing something like this instead and see if it works:
message_text = f"text1\n{variable1}\ntext2{variable2}\ntext3{variable3}"
u = MIMEText(message_text)