0% found this document useful (0 votes)
5 views3 pages

Import Smtplib

The document is a Python script that defines a function to send an email with an attachment using SMTP. It includes parameters for sender and receiver email addresses, student information, and the attachment file path. The script also demonstrates how to create a dummy attachment and send an email with HTML content and an attachment.

Uploaded by

lientt6177
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Import Smtplib

The document is a Python script that defines a function to send an email with an attachment using SMTP. It includes parameters for sender and receiver email addresses, student information, and the attachment file path. The script also demonstrates how to create a dummy attachment and send an email with HTML content and an attachment.

Uploaded by

lientt6177
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import smtplib

from email.mime.multipart import MIMEMultipart


from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import os

def send_email(sender_email, sender_password, receiver_email, student_name, student_info,


attachment_path):
"""Sends an email with the specified details.

Args:
sender_email: The sender's email address.
sender_password: The sender's email password.
receiver_email: The recipient's email address.
student_name: The student's full name.
student_info: The student's information (can include HTML).
attachment_path: The path to the attachment file.
"""
try:
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = f'{student_id} - {student_name}'

# Email body with HTML


body = f"""
<html>
<body>
<p style="color: black; font-size: 20px; font-weight: bold; ">Dear thầy,</p>
<p style="color: red; font-family: Times New Roman; font-size: 16px">Họ và tên:
{student_name}</p>
<p>{student_info}</p>
</body>
</html>
"""
msg.attach(MIMEText(body, 'html'))

# Attach file
if os.path.exists(attachment_path):
with open(attachment_path, "rb") as attachment:
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
f"attachment; filename= {os.path.basename(attachment_path)}",
)
msg.attach(part)
else:
print(f"Error: Attachment file not found at {attachment_path}")
return

with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server: # Use your SMTP server


server.login(sender_email, sender_password)
server.send_message(msg)
print("Email sent successfully!")

except Exception as e:
print(f"Error sending email: {e}")

# Example usage (replace with your actual credentials and information)


sender_email = "lientt6177@ut.edu.vn"
sender_password = "11122005"
receiver_email = "tranlien2k5a@gmail.com"
student_name = "Trần Thị Liên"
student_id = "052305016177"
student_info = "MSSV: 052305016177" #Example
attachment_path = "demo.txt" # Replace with the actual file path

# Create a dummy attachment file


with open(attachment_path, "w") as f:
f.write("This is an attachment file.")

send_email(sender_email, sender_password, receiver_email, student_name, student_info,


attachment_path)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy