0

When I write the script below I get the error IndexError: list index out of range:

import smtplib

from string import Template

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

MY_ADDRESS = 'joaosoundcloudpython@gmail.com'
PASSWORD = 'XXXX'

def get_contacts(test1):
"""
Return two lists names, emails containing names and email addresses
read from a file specified by filename.
"""

names = []
emails = []
with open('/Users/joaopimenta/Documents/joaoPython/soundcloud_email.txt', mode='r', encoding='utf-8') as contacts_file:
    for a_contact in contacts_file:
        names.append(a_contact.split()[0])
        emails.append(a_contact.split()[100])
return names, emails

def read_template():
"""
Returns a Template object comprising the contents of the 
file specified by filename.
"""

with open(soundcloud_email_message.txt, 'r', encoding='utf-8') as template_file:
    template_file_content = template_file.read()
return Template(template_file_content)

def main():
names, emails = get_contacts('/Users/joaopimenta/Documents/joaoPython/soundcloud_email.rtf') # read contacts
message_template = read_template('/Users/joaopimenta/Documents/joaoPython/soundcloud_email_message.txt')

# set up the SMTP server
s = smtplib.SMTP(host='smtp.gmail.com', port=587) 
s.starttls()
s.login(joaosoundcloudpython@gmail.com, XXXX)

# For each contact, send the email:
for name, email in zip(names, emails):
    msg = MIMEMultipart()       # create a message

    # add in the actual person name to the message template
    message = message_template.substitute(PERSON_NAME=name.title())

    # Prints out the message body for our sake
    print(message)

    # setup the parameters of the message
    msg['From']=MY_ADDRESS
    msg['To']=email
    msg['Subject']="This is TEST"

    # add in the message body
    msg.attach(MIMEText(message, 'plain'))

    # send the message via the server set up earlier.
    s.send_message(msg)
    del msg

# Terminate the SMTP session and close the connection
s.quit()

if __name__ == '__main__':
main()
ralf htp
  • 9,149
  • 4
  • 22
  • 34
pepperjohn
  • 67
  • 1
  • 3
  • 11
  • Can you share the full traceback so that we can see which line of code is raising that error? – myrtlecat Jan 07 '18 at 21:22
  • The error means you are trying to access a list element by an index that does not exist. Could be this line: `a_contact.split()[100]`. The error should give you the exact line number, so you can then update the question. – tread Jan 07 '18 at 21:37
  • Thanks surfer190!! TBH I used this code from a tutorial, I don't understand it 100% as I am a beginner with Python. Which index doesn't exist? I wrote test 1 in here : def get_contacts(test1) -> If the () are empty I get an error, when I wrote something it disappeared. – pepperjohn Jan 07 '18 at 21:51
  • @myrtlecat Traceback (most recent call last): File "Email_SeveralRecipients.py", line 70, in main() File "Email_SeveralRecipients.py", line 36, in main names, emails = get_contacts('/Users/joaopimenta/Documents/joaoPython/soundcloud_email.rtf') # read contacts File "Email_SeveralRecipients.py", line 22, in get_contacts emails.append(a_contact.split()[100]) IndexError: list index out of range – pepperjohn Jan 07 '18 at 22:00

0 Answers0