-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendemail.py
68 lines (58 loc) · 2.58 KB
/
sendemail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import pandas as pd
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
import time
# Gmail SMTP configuration
SMTP_SERVER = 'smtp.gmail.com' # Gmail SMTP server
SMTP_PORT = 587 # Port for sending email via Gmail
EMAIL_ADDRESS = '[email protected]' # Replace with your Gmail email address
EMAIL_PASSWORD = 'your_app_password' # Replace with your Gmail password or app-specific password
# Read the CSV file (modify path as needed)
csv_file = 'email_data.csv' # Replace with the path to your CSV file
data = pd.read_csv(csv_file) # Read CSV with default comma delimiter
# Function to send emails
def send_email(name, recipient_email, pdf_filename):
# Create the email message
msg = MIMEMultipart()
msg['From'] = EMAIL_ADDRESS
msg['To'] = recipient_email
msg['Subject'] = f"Important Document for {name}" # Set a custom subject
# Customize the email body
body = f"""
Dear {name},
Please find the attached PDF file named '{pdf_filename}'.
If you have any questions or need further information, feel free to reach out.
Best regards,
[Your Name / Your Organization]
"""
msg.attach(MIMEText(body, 'plain')) # Set 'plain' for a text email or 'html' for HTML email
# Attach the PDF file
try:
with open(pdf_filename, 'rb') as file:
attach = MIMEApplication(file.read(), _subtype='pdf')
attach.add_header('Content-Disposition', f'attachment; filename="{pdf_filename}"')
msg.attach(attach)
except FileNotFoundError:
print(f"File {pdf_filename} not found. Skipping this email.")
return
# Send the email
try:
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls() # Secure the connection
server.login(EMAIL_ADDRESS, EMAIL_PASSWORD) # Login to Gmail account
server.sendmail(EMAIL_ADDRESS, recipient_email, msg.as_string()) # Send email
print(f"Email sent successfully to {recipient_email}")
return 'Success'
except smtplib.SMTPException as e:
print(f"Failed to send email to {recipient_email}: {e}")
return 'Failed'
# Loop through each row in the CSV file and send emails
for index, row in data.iterrows():
send_email(row['name'], row['email'], row['file_name'])
# Update the DataFrame
data.at[index, 'status'] = status
# Save the updated CSV file with email statuses
data.to_csv('email_data_updated.csv', index=False)
time.sleep(5) # Delay for 5 seconds between each email