-2
from asyncio import constants
import pandas as pd
import smtplib as smt
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from getpass import getpass

print("Reading mails from Excel")
data = pd.read_excel("info.xlsx")
emailData = data.get("Email")
emailList = list(emailData)

try:
    # SMTP Object
    print("In process...")
    mailServer = smt.SMTP("smtp.gmail.com", 587)
    mailServer.starttls()
    # Inputting email and password
    senderMail = input("Enter your Email address: ")
    senderPass = getpass("Input your password: ")
    # Setting up the email subject, to, from, and message
    fromEmail = senderMail
    toEmail = emailList
    Emailmessage = MIMEMultipart("alternative")
    Emailmessage['Subject'] = "Testing for Python Project"
    Emailmessage['from'] = senderMail

    # Creating the text via HTML
    messageHTML = '''
    <html>
        <head>
            <style>
                .btn{
                  background:red;
                }
            </style>
        </head>
        <body>
            <h1>HEading 1</h1>
            <button id="btn">Test</button>
        </body>
    </html>
    '''

    # login via email
    mailServer.login(senderMail, senderPass)
    print("Login Successful")

    # Attach the message and Multipart
    textMsg = MIMEText(messageHTML, "html")
    Emailmessage.attach(textMsg)

    # Sending the email
    mailServer.sendmail(fromEmail, toEmail, Emailmessage.as_string())

    for email in emailList:
        print("Message has been sent to ", emailList[email])

    mailServer.quit()


except Exception as error:
    print(error)

When I run this code, I get an error while sending the mail. My login is successful also it's successfully fetched the data from xlsx file but I don't know why this error occurred. I think while sending the mail mailserver.sendmail(fromEmail, toEmail, Emailmessage.as_string()) here third argument i.e, Emailmessage.as_string() is throwing error, Still not sure

It's raising an exception as:

list indices must be integers or slices, not str

Please help me to solve this problem.

Abhi
  • 1
  • Are you sure `print("Message has been sent to ", emailList[email])` isn't raising the error? Please include the full traceback. Remove the blanket `try`/`except`. It's hiding the full traceback from you. – Axe319 Aug 28 '22 at 15:31
  • 1
    @Axe319 I tried and it worked thank you so much, I didn't expect that it would be an error. It was a silly one my bad... – Abhi Aug 28 '22 at 15:59
  • As an aside, it looks like your `email` code was written for an older Python version. The `email` module in the standard library was overhauled in Python 3.6 to be more logical, versatile, and succinct; new code should target the (no longer very) new `EmailMessage` API. Probably throw away this code and start over with modern code from [the Python `email` examples documentation.](https://docs.python.org/3/library/email.examples.html) – tripleee Aug 28 '22 at 19:16

1 Answers1

0

TypeError exception of list indices must be integers or slices, not str is on this part emailList[email]. Hence, change it to:

for email in range(len(emailList)):
    print("Message has been sent to ", emailList[email])

Or:

for email in emailList:
    print("Message has been sent to ", email)
Arifa Chan
  • 947
  • 2
  • 6
  • 23