Python Sending Email using SMTP

Sure! Here’s an example code snippet for sending an email using SMTP in Python:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication

# SMTP server settings
smtp_server = 'smtp.gmail.com'
smtp_port = 587
smtp_username = '[email protected]'
smtp_password = 'your_password'

# Create message
msg = MIMEMultipart()
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
msg['Subject'] = 'Test Email'

# Add body to message
body = 'This is a test email sent using Python!'
msg.attach(MIMEText(body, 'plain'))

# Add attachment to message
with open('example.pdf', 'rb') as f:
    attachment = MIMEApplication(f.read(), _subtype='pdf')
    attachment.add_header('Content-Disposition', 'attachment', filename='example.pdf')
    msg.attach(attachment)

# Connect to SMTP server and send message
with smtplib.SMTP(smtp_server, smtp_port) as server:
    server.starttls()
    server.login(smtp_username, smtp_password)
    server.sendmail(smtp_username, '[email protected]', msg.as_string())

print('Email sent successfully!')

In this example, we’re using the smtplib module to connect to an SMTP server (in this case, Gmail’s SMTP server). We’re also using several modules from the email package to create and format our email message.

To send an email, we first create a MIMEMultipart object and set its From, To, and Subject fields. We then attach a plain text body to the message using MIMEText, and add an attachment (in this case, a PDF file) using MIMEApplication.

Finally, we connect to the SMTP server using smtplib.SMTP, login using our email credentials, and use server.sendmail to send the message. If the email is sent successfully, we print a confirmation message to the console.

Sending email from gmail:

Sure, here’s an example of sending an email from a Gmail account using the smtplib module in Python:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# email addresses
sender_email = "[email protected]"
receiver_email = "[email protected]"

# create message object
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = "Test email from Python"

# message body
body = "This is a test email sent from Python using smtplib!"
msg.attach(MIMEText(body, 'plain'))

# establish connection with gmail's SMTP server
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
    smtp.starttls()  # enable TLS encryption
    # login to sender email account
    smtp.login(sender_email, 'your_gmail_password')
    # send email
    smtp.send_message(msg)

print("Email sent successfully!")

In this example, we create a MIMEMultipart object for our email message and set the sender and receiver email addresses, as well as the subject line. We attach a plain text body to the message using MIMEText.

Next, we establish a connection with Gmail’s SMTP server using smtplib.SMTP and enable TLS encryption using starttls(). We then log in to the sender’s Gmail account using their email address and password.

Finally, we use smtp.send_message() to send the email message, and print a confirmation message to the console if the email is sent successfully. Note that you will need to replace '[email protected]' and 'your_gmail_password' with your own email address and Gmail password, respectively.

Sending HTML in email:

Sure! Here’s an example of sending an email with HTML content using the smtplib module in Python:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# email addresses
sender_email = "[email protected]"
receiver_email = "[email protected]"

# create message object
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = "Test email with HTML content"

# message body (HTML)
body = """
<html>
  <head></head>
  <body>
    <p>Hi there,</p>
    <p>This is a test email with <b>HTML content</b> sent from Python using smtplib.</p>
    <p>Regards,<br>Me</p>
  </body>
</html>
"""
msg.attach(MIMEText(body, 'html'))

# establish connection with gmail's SMTP server
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
    smtp.starttls()  # enable TLS encryption
    # login to sender email account
    smtp.login(sender_email, 'your_gmail_password')
    # send email
    smtp.send_message(msg)

print("Email sent successfully!")

In this example, we create a MIMEMultipart object for our email message and set the sender and receiver email addresses, as well as the subject line. We attach an HTML body to the message using MIMEText with the html parameter.

Next, we establish a connection with Gmail’s SMTP server using smtplib.SMTP and enable TLS encryption using starttls(). We then log in to the sender’s Gmail account using their email address and password.

Finally, we use smtp.send_message() to send the email message, and print a confirmation message to the console if the email is sent successfully. Note that you will need to replace '[email protected]' and 'your_gmail_password' with your own email address and Gmail password, respectively.