diff --git a/script/send-mail/customize.py b/script/send-mail/customize.py new file mode 100644 index 000000000..9a2089594 --- /dev/null +++ b/script/send-mail/customize.py @@ -0,0 +1,48 @@ +from mlc import utils +import os +import subprocess + + +def preprocess(i): + + env = i['env'] + state = i['state'] + + os_info = i['os_info'] + + # Start constructing the argument string + args = f"--subject \"{env.get('MLC_EMAIL_SUBJECT', '')}\" " + + # Process other arguments + args += f"--to_addresses \"{','.join(env.get('MLC_TO_ADDRESS', []))}\" " + args += f"--cc_addresses \"{env.get('MLC_CC_ADDRESS', '')}\" " + args += f"--bcc_addresses \"{env.get('MLC_BCC_ADDRESS', '')}\" " + args += f"--content_file \"{env.get('MLC_CONTENT_FILE', '')}\" " + + # Process attachments + attachments = ' '.join(env.get('MLC_ATTACHMENTS', '').split()) + args += f"--attachments \"{attachments}\" " + + # Add flags for SMTP server, email and password if needed + if env.get('USE_SMTP_SERVER'): + args += "--use_smtp_server " + args += f"--email \"{env.get('EMAIL', '')}\" " + args += f"--password \"{env.get('PASSWORD', '')}\" " + args += f"--smtp_server \"{env.get('SMTP_SERVER', '')}\" " + + subject = env.get('MLC_EMAIL_SUBJECT', '') + to_address = ",".join(env.get('MLC_TO_ADDRESS', [])) + + env['MLC_RUN_CMD'] = f"""{env['MLC_PYTHON_BIN_WITH_PATH'] {os.path.join(env['MLC_TMP_CURRENT_SCRIPT_PATH'], 'send-email.py')} {args}""" + + return {'return': 0} + + +def postprocess(i): + + env = i['env'] + state = i['state'] + + os_info = i['os_info'] + + return {'return': 0} diff --git a/script/send-mail/meta.yaml b/script/send-mail/meta.yaml new file mode 100644 index 000000000..d19dcc15c --- /dev/null +++ b/script/send-mail/meta.yaml @@ -0,0 +1,18 @@ +alias: send-mail +automation_alias: script +automation_uid: 5b4e0237da074764 +category: Utils +deps: + - tags: get,generic-sys-util,_postfix + enable_if_env: + MLC_HOST_OS_FLAVOR: + - ubuntu +new_env_keys: [] +new_state_keys: [] +post_deps: [] +posthook_deps: [] +prehook_deps: [] +tags: +- generic +- template +uid: 5f9b9654ecbe4662 diff --git a/script/send-mail/run.bat b/script/send-mail/run.bat new file mode 100644 index 000000000..80625c80e --- /dev/null +++ b/script/send-mail/run.bat @@ -0,0 +1,23 @@ +@echo off +setlocal enabledelayedexpansion + +:: Function to exit if the last command failed +:exit_if_error +if %ERRORLEVEL% NEQ 0 exit /b %ERRORLEVEL% +exit /b 0 + +:: Function to run a command +:run +echo Running: +echo %1 +echo. + +if /I "%MLC_FAKE_RUN%" NEQ "yes" ( + call %1 + call :exit_if_error +) +exit /b 0 + +:: Add your run commands here... +:: call :run "%MLC_RUN_CMD%" + diff --git a/script/send-mail/run.sh b/script/send-mail/run.sh new file mode 100644 index 000000000..d191bbd97 --- /dev/null +++ b/script/send-mail/run.sh @@ -0,0 +1,18 @@ +#!/bin/bash +function exit_if_error() { + test $? -eq 0 || exit $? +} + +function run() { + echo "Running: " + echo "$1" + echo "" + if [[ ${MLC_FAKE_RUN} != 'yes' ]]; then + eval "$1" + exit_if_error + fi +} + +#Add your run commands here... +MLC_RUN_CMD="${MLC_PYTHON_BIN_WITH_PATH} ${MLC_TMP_CURRENT_SCRIPT_PATH}/send-email.py" +run "$MLC_RUN_CMD" diff --git a/script/send-mail/send-email.py b/script/send-mail/send-email.py new file mode 100644 index 000000000..631b729b8 --- /dev/null +++ b/script/send-mail/send-email.py @@ -0,0 +1,112 @@ +import smtplib +import sys +import argparse +from email.mime.multipart import MIMEMultipart +from email.mime.base import MIMEBase +from email.mime.text import MIMEText +from email.utils import COMMASPACE +from email import encoders + + +def send_email(subject, to_addresses, cc_addresses, bcc_addresses, content_file, + attachments, use_smtp_server=False, smtp_server=None, email=None, password=None): + + to_list = to_addresses.split(',') + cc_list = cc_addresses.split(',') + bcc_list = bcc_addresses.split(',') + attachment_list = attachments.split(',') + + with open(content_file, 'r') as file: + email_content = file.read() + + msg = MIMEMultipart() + msg['From'] = email if use_smtp_server else 'localhost' + msg['To'] = COMMASPACE.join(to_list) + msg['Cc'] = COMMASPACE.join(cc_list) + msg['Subject'] = subject + + msg.attach(MIMEText(email_content, 'plain')) + + for attachment in attachment_list: + with open(attachment, '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}') + msg.attach(part) + + recipients = to_list + cc_list + bcc_list + + if use_smtp_server: + with smtplib.SMTP_SSL(smtp_server) as server: + server.login(email, password) + server.sendmail(email, recipients, msg.as_string()) + print("Email sent successfully using SMTP server!") + else: + with smtplib.SMTP('localhost') as server: + server.sendmail( + 'localhost@example.com', + recipients, + msg.as_string()) + print("Email sent successfully using localhost!") + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description='Send an email with specified attachments') + parser.add_argument('subject', type=str, help='Email subject') + parser.add_argument( + 'to_addresses', + type=str, + help='To addresses, comma-separated') + parser.add_argument( + 'cc_addresses', + type=str, + help='CC addresses, comma-separated') + parser.add_argument( + 'bcc_addresses', + type=str, + help='BCC addresses, comma-separated') + parser.add_argument( + 'content_file', + type=str, + help='File containing email content') + parser.add_argument( + 'attachments', + type=str, + help='Attachments, comma-separated file paths') + + parser.add_argument( + '--use_smtp_server', + action='store_true', + help='Use an SMTP server instead of localhost') + parser.add_argument( + '--email', + type=str, + help='Email address for SMTP server') + parser.add_argument( + '--password', + type=str, + help='Password for SMTP server') + parser.add_argument('--smtp_server', type=str, help='SMTP server address') + + args = parser.parse_args() + + if args.use_smtp_server and not all( + [args.email, args.password, args.smtp_server]): + parser.error( + '--email, --password, and --smtp_server are required when --use_smtp_server is set') + + send_email( + args.subject, + args.to_addresses, + args.cc_addresses, + args.bcc_addresses, + args.content_file, + args.attachments, + args.use_smtp_server, + args.smtp_server, + args.email, + args.password)