Mini Shell

Direktori : /proc/self/root/opt/imh-python/lib/python3.9/site-packages/ngxutil/
Upload File :
Current File : //proc/self/root/opt/imh-python/lib/python3.9/site-packages/ngxutil/sendmail.py

#!/opt/imh-python/bin/python3.9
# vim: set ts=4 sw=4 expandtab syntax=python:
"""

ngxutil.sendmail
Send email reports

@author J. Hipps <jacobh@inmotionhosting.com>

"""

import logging
import smtplib
import socket
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

from ngxutil import __version__, __date__

logger = logging.getLogger('ngxutil')

def email_report(rcpt, sender, subject, msg_txt, msg_html=None):
    """
    Send email report to @rcpt with @subject
    """
    if msg_html:
        msg = MIMEMultipart('alternative')
        msg.attach(MIMEText(msg_txt, 'plain'))
        msg.attach(MIMEText(msg_html, 'html'))
    else:
        msg = MIMEText(msg_txt)

    # Add headers
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = rcpt
    msg['User-Agent'] = 'ngxutil/%s (%s)' % (__version__, __date__)

    # Send message
    return send_locally(msg)

def send_locally(msg):
    """
    Send message @msg via local SMTP server
    """
    socket.setdefaulttimeout(10.0)
    try:
        smtp = smtplib.SMTP('localhost')
        smtp.sendmail(msg['From'], msg['To'], msg.as_string())
        smtp.quit()
        logger.info("Sent message to %s successfully", msg['To'])
    except Exception as e:
        logger.error("Failed to send message to %s: %s", msg['To'], str(e))
        return False
    return True

Zerion Mini Shell 1.0