Skip to content
Snippets Groups Projects
Commit 04dbba04 authored by Fabrizio Benvenuti's avatar Fabrizio Benvenuti
Browse files

[ReportMailer] add report mailer

parent ee3d2ce2
No related branches found
No related tags found
No related merge requests found
import argparse
import json
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
# Set up argument parser
parser = argparse.ArgumentParser(description="Send emails to ETMs and the Core Team, based on board type.")
parser.add_argument('--board', required=True, help='Board type (e.g., c61, c12, c20)')
parser.add_argument('--jsonfile', type=argparse.FileType('r'), required=True, help='File variable containing JSON data')
parser.add_argument('--attachment', required=False, default="report.pdf", help='Path to the file to attach (e.g., report.pdf)')
parser.add_argument('--sender', required=True, help='Email address from which the email is sent')
parser.add_argument('--email', required=True, help='Email address for authentication')
parser.add_argument('--password', required=True, help='Password for email authentication')
parser.add_argument('--smtp_server', required=False, default='smtp.office365.com', help='SMTP server address')
parser.add_argument('--smtp_port', type=int, required=False, default='587', help='SMTP server port')
args = parser.parse_args()
board_type = args.board
json_file = args.jsonfile
attachment_path = args.attachment
EMAIL_ADDRESS = args.email
EMAIL_PASSWORD = args.password
SMTP_SERVER = args.smtp_server
SMTP_PORT = args.smtp_port
# Check if the attachment file exists
if not os.path.exists(attachment_path):
print(f"Error: The attachment file '{attachment_path}' does not exist.")
exit(1)
# Load JSON data
try:
data = json.load(json_file)
except json.JSONDecodeError as e:
print(f"Error: Failed to parse JSON file. {e}")
exit(1)
# Retrieve emails for the specified board
if 'boards' not in data or board_type not in data['boards']:
print(f"Error: Board type '{board_type}' not found in JSON data.")
exit(1)
email_list = data['boards'][board_type]
if not isinstance(email_list, list) or not email_list:
print(f"Error: No valid email addresses found for board '{board_type}'.")
exit(1)
# Create email message
for index in range(len(email_list)):
recipient=email_list[index]
msg = MIMEMultipart()
msg['From'] = args.sender
msg['To'] = recipient
msg['Subject'] = f'Report for {board_type} Board'
body = f'Please find the attached report for the {board_type} board.'
msg.attach(MIMEText(body, 'plain'))
try:
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)
except Exception as e:
print(f"Error: Failed to attach file. {e}")
exit(1)
# Send email
try:
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls()
server.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
server.sendmail(EMAIL_ADDRESS, recipient, msg.as_string())
print(f"Email sent to recipient n. {index+1} of SECO board {board_type}'s mailing list")
except Exception as e:
print(f"Error: Failed to send email to {recipient}. {e}")
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment