diff --git a/Automated_Mailing/mail.py b/Automated_Mailing/mail.py index 9038b77e..1cdd495b 100644 --- a/Automated_Mailing/mail.py +++ b/Automated_Mailing/mail.py @@ -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() \ No newline at end of file +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', 'your_email@example.com') + 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("recipient@example.com", "Test Subject", "This is a test email.", "test.pdf")