Skip to content

Instantly share code, notes, and snippets.

@abhishek-shukla21
Last active July 14, 2023 12:33
Show Gist options
  • Save abhishek-shukla21/cf3aab0ab513ffa725b588252aad64cf to your computer and use it in GitHub Desktop.
Save abhishek-shukla21/cf3aab0ab513ffa725b588252aad64cf to your computer and use it in GitHub Desktop.
Send Email using Gmail SMTP in Python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Set up your Gmail SMTP credentials
sender_email = 'YOUR_EMAIL'
sender_password = 'YOUR_PASSWORD'
# Set up the email details
recipient_email = 'RECIPIENT_EMAIL'
subject = 'Email subject'
message = 'Email body'
# Create the email message
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = recipient_email
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
# Set up the SMTP server and send the email
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
smtp.starttls()
smtp.login(sender_email, sender_password)
smtp.send_message(msg)
import re
def validate_email(email):
# Regular expression pattern for email validation
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
# Use the pattern to match the email
if re.match(pattern, email):
return True
else:
return False
# Example usage
email = input("Enter an email address: ")
if validate_email(email):
print("Valid email")
else:
print("Invalid email")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment