Search code examples
pythonemailhtml-email

How to supply some security access?


Hello I have this code:

username = input("Target Mail: ")
password = input("Password: ")

server=e.connect("imap.gmail.com",username,password)
server.listids()
email=server.mail(server.listids()[0])

print("\n<|From|> " + email.from_addr + " <|From|>")
print("<|Title|> " + email.title + " <|Title|>\n")
print("\n<|Title|> MESSAGES <|Title|>\n")
print(email.body)

but problem is it doesn't work if i don't enable less secure app access on my google account's settings.

enter image description here

So how can I run my code without enable this? Cause it's really annoying.


Solution

  • If you just want to send an email from a gmail account, you would want to register your app and go through an oauth flow. Documentation for getting started with that is here:

    oauth flow here:

    https://developers.google.com/gmail/api/quickstart/python

    email example through gmail python module/package here:

    https://developers.google.com/gmail/api/guides/sending

    IMAP is a mail protocol which is less secure than a properly setup OAUTH flow. That is why you are receiving the message and forced configuration for it to work.