This Python script demonstrates how to send a one-time password (OTP) email using Gmail's SMTP server. The script utilizes the smtplib
library to establish a secure connection with the Gmail server and sends an OTP to the specified recipient's email address.
-
Import required libraries:
random
for generating random OTP andsmtplib
for sending emails using the Simple Mail Transfer Protocol. -
Establish a connection with Gmail's SMTP server on port 587.
-
Start TLS (Transport Layer Security) security for secure communication.
-
Login to the Gmail account using the sender's email address and an app password.
-
Input the recipient's email address.
-
Generate a 4-digit OTP using the
random
library. -
Compose the email message containing the OTP.
-
Send the email from the sender's email address to the recipient's email address.
-
Close the server connection.
-
Make sure you have Python installed on your system.
-
Replace
'[email protected]'
with your Gmail email address and'ynstcblzldrxbvaf'
with your Gmail app password. -
Run the script.
-
Input the recipient's email address when prompted.
-
An OTP email will be sent to the recipient's email address.
-
Ensure that you have enabled "Less secure app access" for the sender Gmail account if you're using your main Gmail account. Alternatively, you can use a separate Gmail account for this purpose.
-
Make sure to keep your app password secure.