Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to send a simple mail using the SMTP library in Python (without logging into the cloud)?

Simple Mail Sending using SMTP Library in Python

Condition for Simple Mail Sending using SMTP Library in Python

  • Description: SMTP, or Simple Mail Transfer Protocol, is a protocol used to send emails over the Internet.It defines the rules for sending and routing email messages between servers.SMTP operates as a push protocol, which means it is used to send emails from the senders email client to the recipients email server or between email servers.
Step-by-Step Process
  • Import smtplib: Provides the functionality to send emails via the Simple Mail Transfer Protocol (SMTP).email.mime.text and email.mime.multipart: Used to format the email body and headers.
  • Create MIME Email: We use MIMEMultipart to create a message that can contain both text and attachments (although in this simple example, only text is used).MIMEText is used to create the text part of the email (the body).
  • Set Up SMTP Server: The SMTP server is set to "localhost" (this is assuming you're running your own local mail server that doesn't require authentication).Port 25 is the default SMTP port for non-secure connections.
  • Send the Email: The with smtplib.SMTP() context manager connects to the SMTP server and sends the email with server.sendmail().
Sample Code
  • import smtplib
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    sender_email = "your_email@example.com"
    receiver_email = "receiver_email@example.com"
    subject = "Test Email" body = "This is a test email sent from Python using smtplib."
    message = MIMEMultipart()
    message["From"] = sender_email
    message["To"] = receiver_email
    message["Subject"] = subject
    message.attach(MIMEText(body, "plain"))
    smtp_server = "localhost"
    smtp_port = 25
    try:
    with smtplib.SMTP(smtp_server, smtp_port) as server:
    server.sendmail(sender_email, receiver_email, message.as_string())
    print("Email sent successfully!")
    except Exception as e:
    print(f"Error sending email: {e}")
Screenshots
  •  Simple Mail Sending using SMTP Library