Sending email with python
Certainly! Let’s go through the code step by step:
1. First, we import the necessary modules: `smtplib`, `MIMEText`, and `MIMEMultipart`. These modules are part of the Python `email` library and allow us to work with email messages and send them using the SMTP protocol.
2. Next, we define the email details such as the sender’s email address (`sender_email`), recipient’s email address (`receiver_email`), subject of the email (`subject`), and the message content (`message`).
3. We create a `MIMEMultipart` object named `msg`. This object represents the email message and allows us to attach the message content and set headers like ‘From’, ‘To’, and ‘Subject’.
4. We set the ‘From’, ‘To’, and ‘Subject’ headers of the email using the `msg` object.
5. The message content is attached to the email using `MIMEText`. We pass the `message` content and the subtype ‘plain’ to create a plain text part of the email.
6. We define the SMTP server details, including the server address (`smtp_server`), port number (`smtp_port`), and the email credentials (`smtp_username` and `smtp_password`).
7. Inside a try-except block, we attempt to send the email:
a. We create an SMTP connection to the server using `smtplib.SMTP(smtp_server, smtp_port)`. b. We start a secure TLS connection using `starttls()` method to encrypt the connection. c. We log in to the email account using `server.login(smtp_username, smtp_password)`. d. The `sendmail()` method is used to send the email. We pass the sender’s email, recipient’s email, and the email message converted to a string using `msg.as_string()`. e. If the email is sent successfully, a success message is printed. f. If any exception occurs during the email sending process, an error message is printed. 8. Finally, we close the connection to the SMTP server using `server.quit()`.
Make sure to replace `’your-email@gmail.com’` and `’your-password’` with your actual Gmail email address and password. Also, update the `receiver_email` with the recipient’s email address.
This code demonstrates how to send an email using Gmail’s SMTP server, but you can modify the server details to use a different SMTP server if needed.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Email details
sender_email = 'your-email@gmail.com'
receiver_email = 'recipient-email@example.com'
subject = 'Hello from Python!'
message = 'This is a test email sent using Python.'
# Create a multipart message and set headers
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = subject
# Attach the message to the email
msg.attach(MIMEText(message, 'plain'))
# SMTP server details
smtp_server = 'smtp.gmail.com'
smtp_port = 587
smtp_username = 'your-email@gmail.com'
smtp_password = 'your-password'
try:
# Create a secure connection with the SMTP server
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
# Login to your account
server.login(smtp_username, smtp_password)
# Send the email
server.sendmail(sender_email, receiver_email, msg.as_string())
print('Email sent successfully!')
except Exception as e:
print('Error sending email:', str(e))
finally:
# Close the connection
server.quit()