Python Send Email
Python Send Email:
To send an email using Python, you can utilize the built-in smtplib
library.
Here’s an example code snippet that demonstrates how to send an email using a Gmail account:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_email(sender_email, sender_password, receiver_email, subject, message):
# Create a MIME multipart message
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = subject
# Attach the message to the email
msg.attach(MIMEText(message, 'plain'))
# Create a SMTP server object
server = smtplib.SMTP('smtp.gmail.com', 587)
# Start the TLS encryption
server.starttls()
# Login to the sender's email account
server.login(sender_email, sender_password)
# Send the email
server.send_message(msg)
# Quit the server
server.quit()
# Provide the necessary details and call the function to send the email
sender_email = 'your-email@gmail.com'
sender_password = 'your-password'
receiver_email = 'recipient-email@example.com'
subject = 'Testing Python email'
message = 'This is a test email sent from Python.'
send_email(sender_email, sender_password, receiver_email, subject, message)
Make sure to replace 'your-email@gmail.com'
with your actual Gmail address, 'your-password'
with your Gmail password, and 'recipient-email@example.com'
with the recipient’s email address.
Additionally, note that Gmail might block sign-in attempts from some applications, so you might need to enable “Less secure apps” or generate an “App password” for your Gmail account if the login fails.
Python Training Demo Day 1
Conclusion:
Unogeeks is the No.1 IT Training Institute for Python Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Python here – Python Blogs
You can check out our Best In Class Python Training Details here – Python Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks