Test Outbound Email Delivery with SEND_MAIL Python Script
This Python script is a utility to test outbound email delivery (including attachments) via an SMTP server, specifically for Metric Insights environments.
Key functions:
- Composes an email with a subject, body, and file attachment (/root/test.txt).
- Uses the smtplib library to connect to an SMTP server (mailer.metricinsights.com on port 587).
- Authenticates with the SMTP server using provided credentials.
- Sends the email from [email protected] to the specified recipient(s).
Create send_mail.py
with the below code and add executable permissions.
#!/usr/bin/env python
import smtplib, os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders
from email.mime.text import MIMEText
emaillist=['[email protected]'] # Receipient email addressmsg = MIMEMultipart('mixed')
msg['Subject'] = 'DATA EMAIL' # mail subject line
msg['From'] = '[email protected]' # From email address
msg['To'] = ', '.join(emaillist)
part = MIMEBase('application', "octet-stream")
# Provide the path of the file to be attached in the mailpart.set_payload(open('/root/test.txt', "rb").read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="test.txt"')
msg.attach(part)
msg.add_header('To', msg['From'])
text = "TEST BODY" #Email body
part1 = MIMEText(text, 'plain')
msg.attach(part1)
# provide SMTP details of the host and its port numberserver = smtplib.SMTP("mailer.metricinsights.com",587)
# If the host supports TLS then enable the below 2 lines of code
#MI mail server support it
server.ehlo() # <--
server.starttls() # <--
server.login("notifications", "*************") #Login to mail server credentials
server.sendmail(msg['From'], emaillist , msg.as_string())
Click to copy