rit ema in python

To send an email in Python, you can use the smtplib library, which allows you to connect to an SMTP server and send email messages. Here's an example code to send an email:

main.py
import smtplib
from email.mime.text import MIMEText

def send_email(sender, receiver, subject, message):
    msg = MIMEText(message)

    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = receiver

    # Connect to the SMTP server
    smtp_server = smtplib.SMTP('smtp.example.com', 587)
    smtp_server.ehlo()
    smtp_server.starttls()
    smtp_server.login('your_email@example.com', 'your_password')

    # Send the email
    smtp_server.send_message(msg)
    smtp_server.quit()

# Usage
sender = 'your_email@example.com'
receiver = 'recipient_email@example.com'
subject = 'Hello!'
message = 'This is the body of the email.'

send_email(sender, receiver, subject, message)
691 chars
28 lines

Make sure to replace smtp.example.com with the appropriate SMTP server address, and 'your_email@example.com' and 'your_password' with your actual email credentials.

Be cautious when using this code, as it involves your email credentials.

related categories

gistlibby LogSnag