0
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.

Example Error Screen

2
  • Please include the traceback as formatted text instead of a screenshot Commented Feb 17, 2022 at 18:54
  • message_text[0] is 'text1\n', then 'text1\n'[1] is 'e', and then 'e'[2] doesn't exist because you are trying to access the 3rd character of the one-character string e - oops. Commented Feb 17, 2022 at 18:57

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.