简体   繁体   中英

How can I send e-mails from python without having to enable less secure apps?

I am having trouble sending an e-mail without having to enable google's less secure apps: https://myaccount.google.com/lesssecureapps . How can I modify the code in Python so that I can send e-mail without having to enable this? I need to disable this option, because I am building an application in which the user can send a feedback e-mail to the developer, so I cannot force the user to enable less secure apps. My code in Python:

    def send_email(self):
        print("send e-mail to developer")
        sender_email = self.email.text
        sender_password = self.password.text
        sender_feedback = self.feedback_message.text
        print("email is: ", type(sender_email))
        print("pass is: ", sender_password)
        print("feedback message is: ", sender_feedback)

        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()  # encrypt connection
        server.login(sender_email, sender_password)

        subject = 'Feedback'
        msg = sender_feedback

        msg = f"Subject: {subject} \n\n {msg}"
        server.sendmail(
            # from,to,message
            sender_email,
            'my_email@gmail.com',
            msg
        )
        print("E-MAIL HAS BEEN SENT!")
        server.quit()

According to POP3/SMTP compatible with 2 factor authentication?- Help - Gmail , you need to set up an "app password" to be used instead of your regular password to be able to use an SMTP client when "2-step verification" is turned on for your Google account.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM