How to send email in python codes?

Question: How to send email in python codes?

I have tried several codes from google and python codes that are made by other coders but I can’t seem to know how to use them :cry:

The code I’m using currently the simplest one but I still couldn’t use it, help me, please?

Repl link:

code snippet:
#the if is for my use hehe

email = input("send help?")
if email ==1:
  import smtplib

email = '' # Your email
password = 'mypass123' # Your email account password
send_to_email = '' # Who you are sending the message to
message = 'This is my message' # The message in the email

server = smtplib.SMTP('', 587) # Connect to the server
server.starttls() # Use TLS
server.login(email, password) # Login to the email server
server.sendmail(email, send_to_email , message) # Send the email
server.quit() # Logout of the email server
the error:
Traceback (most recent call last)
  File "", line 97, in <module>
Server = smtplib.SMTP('', 587) 
Connect to the server
NameError: name 'smtplib' not defined

I wrote the error manually, but it’s exactly the same as the error message I received.

smtplib is a python library, you need to import libraries before using them like such:
import smtplib

Just don’t forget to write this on the top of your code.


it should be email=="1" so you’re comparing a string and not an integer. It’s not like JS where it’ll automatically convert the 1 to string.


yes! Just now, I add the import smtplib but another error occurred D:

I am sure I input the correct email and password tho
Do you think I should use another code instead? this is where i took the example from.


Thank you for the correction! I fixed it right away, but I turn it into yes no instead.


The error “Username and Password not accepted” indicates that your credentials are invalid.


I’m afraid this is no longer possible.

There used to be a Google Account feature called Less secure apps that allowed you to toggle between allowing secure and less secure apps. What you’re trying to do would be classified as “less secure”. However, Google recently removed support for less secure apps as you can see in the above image. This means that you won’t be able to log in to the account from your program and thus are unable to send emails with this method.


OH NOOOOOO is there any way to send email without SMTP? I’ve googled some of it, including google cloud console, loom, and else. But those are way too complicated. I’ve tried using google cloud console, but I got stuck with the redirect URL. I don’t understand what URL I should put it in.

I found it a little odd because it’s correct :’ But I appreciate the feedback!

There are various services you can use (like mailjet) with very simple API and free tiers.

1 Like

Can’t you use an app password now?

I made a template for just such an occasion as this a few months ago, Here ya go: (look in the


And yes it still works, you have to use an App password since google removed Allow Access from Less Secure apps, just read through the readme I explained it there as well as how to get an app password


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.