@joeschmuck
similar approach of @ruffhi , i create a python script that you can invoke from the MR easily:
#new_sendemail_V1
import smtplib, json, argparse
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
def read_smtp_config():
file_path = "smtp.conf"
try:
with open(file_path, 'r') as file:
config = json.load(file)
required_keys = ["smtp_server", "smtp_port", "smtp_user", "smtp_password"]
for key in required_keys:
if key not in config:
raise ValueError(f"Required '{key}' not set")
return config
except FileNotFoundError:
raise FileNotFoundError(f"File not found")
except json.JSONDecodeError:
raise ValueError(f"File not in json")
def send_email(subject, to_address, mail_body_html, attachment_files=None):
smtp_config = read_smtp_config()
smtp_server = smtp_config["smtp_server"]
smtp_port = smtp_config["smtp_port"]
smtp_user = smtp_config["smtp_user"]
smtp_password = smtp_config["smtp_password"]
try:
with open(mail_body_html, 'r') as f:
html_content = f.read()
msg = MIMEMultipart()
msg['From'] = smtp_user
msg['To'] = to_address
msg['Subject'] = subject
msg.attach(MIMEText(html_content, 'html'))
if attachment_files:
for attachment_file in attachment_files:
try:
with open(attachment_file, 'rb') as f:
part = MIMEBase('application', 'octet-stream')
part.set_payload(f.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f'attachment; filename="{attachment_file.split("/")[-1]}"')
msg.attach(part)
except Exception as e:
print(f"KO {attachment_file}: {e}")
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(smtp_user, smtp_password)
server.sendmail(smtp_user, to_address, msg.as_string())
server.quit()
print("OK")
except Exception as e:
print(f"KO: {e}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Workaround to send email easily in Multi Report")
parser.add_argument("--subject", required=True, help="Email subject")
parser.add_argument("--to_address", required=True, help="Recipient")
parser.add_argument("--mail_body_html", required=True, help="File path for the email body")
parser.add_argument("--attachment_files", nargs='*', help="OPTIONAL attachments as json file path array")
args = parser.parse_args()
try:
send_email(
subject=args.subject,
to_address=args.to_address,
mail_body_html=args.mail_body_html,
attachment_files=args.attachment_files
)
except Exception as e:
print("Error:", e)
actually im reading the smtp data from a smtp.conf
file, json formatted
{
"smtp_server":""
,"smtp_port":""
,"smtp_user":""
,"smtp_password":""
}
example of usage:
#!/bin/bash
subject=''
Email=''
html_file=""
attachment=("" "") #as many file you need, closed by " and separeted by space
python3 new_sendemail_V1.py \
--subject "$subject" \
--to_address "$Email" \
--mail_body_html "$html_file" \
--attachment_files "${attachment[@]}"
Actually is pretty simple, all working expecting to be on the same folder.
Just do some test if you like this solution, because in case i will work on it (there are a lot of things can be made better, like the smtp credential saving)