123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
/*******************************************************************************

        copyright:      Copyright (c) 2004 Kris Bell. All rights reserved

        license:        BSD style: $(LICENSE)
      
        version:        Initial release: May 2004
        
        author:         Kris

*******************************************************************************/

module tango.util.log.AppendMail;

private import  tango.util.log.Log;

private import  tango.io.stream.Buffered;

private import  tango.net.device.Socket,
                tango.net.InternetAddress;

/*******************************************************************************

        Appender for sending formatted output to a Mail server. Thanks
        to BCS for posting how to do this.

*******************************************************************************/

public class AppendMail : Appender
{
        private char[]          to,
                                from,
                                subj;
        private Mask            mask_;
        private InternetAddress server;

        /***********************************************************************
                
                Create with the given layout and server address

        ***********************************************************************/

        this (InternetAddress server, char[] from, char[] to, char[] subj, Appender.Layout how = null)
        {
                layout (how);

                this.to = to;
                this.from = from;
                this.subj = subj;
                this.server = server;

                // Get a unique fingerprint for this appender
                mask_ = register (to ~ subj);
        }

        /***********************************************************************
                
                Send an event to the mail server
                 
        ***********************************************************************/

        override final void append (LogEvent event)
        {
            synchronized
            {
                auto conduit = new Socket;
                scope (exit)
                       conduit.close();

                conduit.connect (server);
                auto emit = new Bout (conduit);

                emit.append ("HELO none@anon.org\r\nMAIL FROM:<") 
                    .append (from) 
                    .append (">\r\nRCPT TO:<") 
                    .append (to) 
                    .append (">\r\nDATA\r\nSubject: ") 
                    .append (subj) 
                    .append ("\r\nContent-Type: text/plain; charset=us-ascii\r\n\r\n");
                
                layout.format (event, &emit.write);
                emit.append ("\r\n.\r\nQUIT\r\n");
                emit.flush();
            }
        }

        /***********************************************************************
                
                Return the fingerprint for this class

        ***********************************************************************/

        @property override final const Mask mask ()
        {
                return mask_;
        }

        /***********************************************************************
                
                Return the name of this class

        ***********************************************************************/

        @property override final const const(char)[] name ()
        {
                return this.classinfo.name;
        }
}