0

I am trying to develop a small python application that allows it's user to send multiple mails through python, I am using gmail, I have allowed Access for less secure apps, it doesn't send. I have searched stackoverflow for similar problems, but it seemed that most of the used codes are completely different in implementation, even after trying many of hem, they all through exception

import smtplib 
from telnetlib import Telnet

def addSenders(message):
    num = input("enter number of recievers : ")
    num = int (num)
    i = 0
    emailList = []
    while i < num :
        nameStr = input("enter name")
        mailStr = input("enter e-mail")
        emailList.append(mailStr)
        if i == 0:
            message += nameStr + " <" + mailStr + ">"
            print(message)
        else:
            message += "," + nameStr + " <" + mailStr + ">"
        i = i + 1
    return emailList, message


sender = 'xxxx@gmail.com'
password = "xxxx"


message = """From: xxxx xxxx <xxxxx@gmail.com>
To: To """
to, message = addSenders(message)

message += """
MIME-Version: 1.0
Content-type: text/html
Subject: Any Subject!

<h1> Good Morning :D <h1>
"""


server =  smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()

server.login(sender, password)
try:
   server.sendmail(sender, [to], message)         
   print ("Successfully sent email")
except:
   print ("Error: unable to send email")
server.quit()

output : Error: unable to send email

Andrew Meleka
  • 93
  • 1
  • 10
  • 1
    I think it is more informative not to catch the exception (during debug), then we will be able to see what kind of exception was thrown by `server.sendmail`. Could you remove try-except statement, re-run your code and provide traceback? – Ilya V. Schurov Dec 21 '15 at 20:35
  • why don't you catch the exception by except Exception as e: newline print str(e) – PyNEwbie Dec 21 '15 at 20:36
  • i removed the try-catch, and that was the exception : unhashable type: 'list' – Andrew Meleka Dec 21 '15 at 20:40
  • The `[to]` bit seems odd: you are wrapping the list of receivers (`to`, as returned by `addSenders`) in another list. Can you paste the full traceback? – Jasper Dec 21 '15 at 20:44
  • i tried to add some elements to a list, then printed both the new one and [to], they both are the same – Andrew Meleka Dec 21 '15 at 20:51
  • problem was solved, i just removed the [] from "to", i was inserting a list into other list – Andrew Meleka Dec 22 '15 at 14:23

0 Answers0