From 4278017e0f3c5721804f13e60b6a461c8a5dae2d Mon Sep 17 00:00:00 2001 From: Saikat Dey <59501072+saikat2955@users.noreply.github.com> Date: Tue, 3 Oct 2023 12:53:56 +0530 Subject: [PATCH] Create Hcl_Inote_Smtp.py Automated Mail Send From HCL Inotes and other SMTP server --- Dey/Hcl_Inote_Smtp.py | 96 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 Dey/Hcl_Inote_Smtp.py diff --git a/Dey/Hcl_Inote_Smtp.py b/Dey/Hcl_Inote_Smtp.py new file mode 100644 index 0000000..fa3483d --- /dev/null +++ b/Dey/Hcl_Inote_Smtp.py @@ -0,0 +1,96 @@ +#'''@author: Saikat Dey''' + +#############Install Below Command If Libs Not Available################# + +#pip install smtplib email pybase64 + + +##############Importation Libs##################### +import smtplib +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +from email.mime.base import MIMEBase +from email import encoders + +def send_email_with_attachments(smtp_server, smtp_port, smtp_username, smtp_password, from_email, to_email, cc_email, subject, html_body, attachments=[]): + try: + message = MIMEMultipart() + message['From'] = from_email + message['To'] = to_email + message['Cc'] = ', '.join(cc_email) if cc_email else '' # Include CC if provided + message['Subject'] = subject + + ######### Create an HTML message part######### + html_message = MIMEText(html_body, 'html') + message.attach(html_message) + + ###########Attach files########### + for attachment_path in attachments: + with open(attachment_path, 'rb') as file: + part = MIMEBase('application', 'octet-stream') + part.set_payload(file.read()) + encoders.encode_base64(part) + part.add_header('Content-Disposition', f'attachment; filename="{attachment_path}"') + message.attach(part) + + ###########Combine TO and CC recipients into a single list########### + all_recipients = [to_email] + (cc_email or []) + + #####Connect to the Inotes server######### + server = smtplib.SMTP_SSL(smtp_server, smtp_port, timeout=30) + server.login(smtp_username, smtp_password) + + ########Send the email to all recipients########## + server.sendmail(from_email, all_recipients, message.as_string()) + print('Email sent successfully') + + except Exception as e: + print(f'Error: {str(e)}') + + finally: + if server: + server.quit() + +#######################Example Variable############## +""" +smtp_server = '199999.1678.69.108' +smtp_port = 777 +smtp_username = 'Secrets' +smtp_password = 'Debo Na' + +# Sender and recipient email addresses +from_email = 'your_email@gmail.com' +to_email = 'saikat.dey@test.ai' +cc_email = ['test@gmail.com'] +# Email subject +subject = 'Inotes Automated Mail Sending' + +# HTML-formatted email body +html_body = ''' + +
+Hello,