SEND_MAIL python script

This script allows to verify if your mail server can correctly send emails with attachments.

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=['alexey@metricinsights.com'] # Receipient email addressmsg = MIMEMultipart('mixed')
msg['Subject'] = 'DATA EMAIL' # mail subject line
msg['From'] = 'notifications@app.metricinsights.com' # 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())

0 Comments

Add your comment

E-Mail me when someone replies to this comment