On 22/03/2022 14:57, Edward Sandberg wrote:
You could use inotify to monitor a directory and trigger a script to send the mail.

Here is a very simple example content of such an email file, but they get much more complex.

To: f...@bar.com
From: b...@foo.com
Subject: example email

Hello World!

If the files are valid email files with headers you could just pipe them to sendmail.

If the files are not valid emails with headers then you could send them as attachments. Something like the python module envelope would be a simple way to do this: https://pypi.org/project/envelope/

The following code (function) takes a bytesIO object and attaches it to an email.

A warning about inotify and python, if the rate of writing new files to a directory is too high python may not keep up to it there are events that may be "lost". I have found it the hard way.

note: 127.0.0.1 was choosen instead of localhost because in some circunstances -> IPv6 issues

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.encoders import encode_base64
from email.utils import formatdate
import email.header
import email.utils
import magic

def sendEmail(mailFrom,to,subject,msg,attachment,mailServer='127.0.0.1',mailPort='25',mailProtocol='NONE',mailUser='',mailPassword=''):
    if mailProtocol == "TLS" or mailProtocol == "NONE":
        try:
            smtp = smtplib.SMTP(mailServer,mailPort)
        except Exception:
            print("Failed connect to server")
            return False
        if mailProtocol == "TLS":
            try:
                smtp.starttls()
            except Exception:
                print("Failed starttls")
                return False
    if mailProtocol == "SSL":
        try:
            smtp = smtplib.SMTP_SSL(mailServer,mailPort)
        except Exception:
            print("Failed connect to server and start ssl session")
            return False
    smtp.ehlo_or_helo_if_needed()
    if mailProtocol == "TLS" or mailProtocol == "SSL":
        try:
            smtp.login(mailUser,mailPassword)
        except Exception:
            print("Failed login")
            smtp.quit()
            return False

    message  = MIMEMultipart()
    message.set_charset( 'utf-8' )
    message['Subject'] = subject
    message['To'] = f"""<{to}>"""
    message['Date'] = formatdate(localtime=True)
    message['From'] = f"""<{mailFrom}>"""
    message['Message-ID'] = email.header.Header( email.utils.make_msgid() )
    mimeType = magic.Magic(mime=True, uncompress=True)
    mimetype = mimeType.from_buffer(attachment.read(2048))
    mimetype = mimetype.split('/', 1)
    attachment.seek(0)
    part1 = MIMEText(msg, "plain")
    part2 = MIMEBase(mimetype[0], mimetype[1])
    part2.set_payload(attachment.read())
    encode_base64(part2)
    part2.add_header('Content-Disposition', 'attachment',filename='relatorio.spam.pdf')
    message.attach(part1)
    message.attach(part2)
    #print(message)
    try:
        smtp.send_message(message,from_addr=mailFrom,to_addrs=to)
    except Exception:
        smtp.quit()
        #print("Failed to send email {}",str(Exception))
        return False
    smtp.quit()
    #print("SMTP Not false")
    return True



On 3/22/22 9:28 AM, Linda Pagillo wrote:
Hi everyone! I hope all of you are doing well.


Does postfix allow for sending email by copying a file or files to anappropriate queue directory?If so, what is the format of thefile(s)?A reference would be very helpful if someone can point me in the right direction.


Thanks!

Reply via email to