Skip to content

Added email attachment feature and logging system #332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 55 additions & 32 deletions Automated_Mailing/mail.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,56 @@
import pandas as pd
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


from_addr='ENTER_SENDERS_MAILID'

data=pd.read_csv("abc.csv") # Enter path of CSV files containing emails
to_addr=data['email'].tolist() # Change'email' to column name containg emailids
name = data['name'].tolist()

l=len(name)
email="" #Enter Your email id here
password="" #Enter your Password

for i in range (l):
msg=MIMEMultipart()
msg['From']=from_addr
msg['To']=to_addr[i]
msg['Subject']='Just to Check'

body=name[i]+'Enter your content here'

msg.attach(MIMEText(body,'plain'))

mail=smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login(email,password)
text=msg.as_string()
mail.sendmail(from_addr,to_addr[i],text)
mail.quit()
import os
import pandas as pd
from email.message import EmailMessage

def send_email(to_email, subject, body, attachment_path=None):
try:
# Email Credentials (Replace with actual credentials or environment variables)
EMAIL_ADDRESS = os.getenv('EMAIL_USER', '[email protected]')
EMAIL_PASSWORD = os.getenv('EMAIL_PASS', 'yourpassword')

# Setup Email
msg = EmailMessage()
msg['From'] = EMAIL_ADDRESS
msg['To'] = to_email
msg['Subject'] = subject
msg.set_content(body)

# Attach file if provided
if attachment_path:
try:
with open(attachment_path, 'rb') as f:
file_data = f.read()
file_name = os.path.basename(attachment_path)
msg.add_attachment(file_data, maintype='application', subtype='octet-stream', filename=file_name)
print(f"Attachment {file_name} added successfully.")
except FileNotFoundError:
print("Error: Attachment file not found.")
return

# Send Email
with smtplib.SMTP('smtp.example.com', 587) as server:
server.starttls()
server.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
server.send_message(msg)
print(f"Email sent to {to_email}")

# Log email details
log_email(to_email, subject, attachment_path)

except smtplib.SMTPException as e:
print(f"SMTP error occurred: {e}")

def log_email(to_email, subject, attachment):
log_file = 'email_log.csv'
log_data = {'Recipient': [to_email], 'Subject': [subject], 'Attachment': [attachment]}
df = pd.DataFrame(log_data)

if os.path.exists(log_file):
df.to_csv(log_file, mode='a', header=False, index=False)
else:
df.to_csv(log_file, mode='w', header=True, index=False)
print("Email logged successfully.")

# Example Usage
send_email("[email protected]", "Test Subject", "This is a test email.", "test.pdf")