Q 33: Write a program in Python to send an Email.
Solution:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_email_via_gmail(email_details):
sender_email = email_details.get("sender_email")
sender_password = email_details.get("sender_password")
receiver_email = email_details.get("receiver_email")
email_subject = email_details.get("email_subject")
email_body = email_details.get("email_body")
smtp_server = "smtp.gmail.com"
smtp_port = 587
email_message = MIMEMultipart()
email_message["From"] = sender_email
email_message["To"] = receiver_email
email_message["Subject"] = email_subject
email_message.attach(MIMEText(email_body, "plain"))
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(sender_email, sender_password)
server.send_message(email_message)
print("The email has been sent successfully!")
email_details = {
"sender_email": "sender-email-id@gmail.com",
"sender_password": "sender-password-goes-here",
"receiver_email": "recipient-email@example.com",
"email_subject": "This email has been sent via Python Code!",
"email_body": "Hello! This is a test email sent via Python code!"
}
send_email_via_gmail(email_details)
You can download this article in various formats for your convenience. Choose from the options below:
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Python,
- Python List of Lists with Examples
- Fix: ERROR: Could not find a version that satisfies the requirement tkinter (from versions: none) ERROR: No matching distribution found for tkinter
- How to Convert Python Notebook .ipynb (JSON) to Executable .py Module
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: Pandas Merge Indicator (Left, Right and Both) Example
- Fix: EOFError Exception in Python
- How to URL Decode a Query String in Python
- How to take user input from the console in a Python program
- Compare two lists in Python and return matches
- How to change the Python Default version
- Fix: KeyError: exception in Python
- How to get the Execution Time of A Python Program
- Fix: ModuleNotFoundError: No module named boto3 [Python]
- How to get unique values from a list in Python
- How to pip install Python Modules in VSCode
- Python: Fix - TypeError: NoneType object is not iterable
- How to delete a file using Python code example
- How to create a dictionary comprehension in Python
- Python: Get just the filename without extension using Path
- How to comment out a block of code in Python
- How to add two float numbers in Python
- Python: How to POST Json Data with HTTP Request
- Get MD5 Hash as Checksum of a String in Python
- Python - Convert float to String
- How to Parse XML String in Python
More Posts:
- 10 FTP SFTP Clients and Alternatives - FTP
- How to turn on Bluetooth on Windows 10 - Microsoft
- [fix] Editor could not be opened unexpected error: File is a directory (VS Code) - HowTos
- How to switch to Powershell on Mac Terminal - Powershell
- Generate SHA-256 Hash and Salt in Java Example - Java
- Program 3: Print the name of user as input - 1000+ Python Programs - Python-Programs
- How to fix Jenkins Plugin Stuck in Uninstallation pending Status - Linux
- Fix - 412 Cookies Are Disabled error in Exchange Admin Center when you click hybrid - Microsoft