1 /******************************************************************************* 2 3 copyright: Copyright (c) 2004 Kris Bell. All rights reserved 4 5 license: BSD style: $(LICENSE) 6 7 version: Initial release: May 2004 8 9 author: Kris 10 11 *******************************************************************************/ 12 13 module tango.util.log.AppendMail; 14 15 private import tango.util.log.Log; 16 17 private import tango.io.stream.Buffered; 18 19 private import tango.net.device.Socket, 20 tango.net.InternetAddress; 21 22 /******************************************************************************* 23 24 Appender for sending formatted output to a Mail server. Thanks 25 to BCS for posting how to do this. 26 27 *******************************************************************************/ 28 29 public class AppendMail : Appender 30 { 31 private char[] to, 32 from, 33 subj; 34 private Mask mask_; 35 private InternetAddress server; 36 37 /*********************************************************************** 38 39 Create with the given layout and server address 40 41 ***********************************************************************/ 42 43 this (InternetAddress server, char[] from, char[] to, char[] subj, Appender.Layout how = null) 44 { 45 layout (how); 46 47 this.to = to; 48 this.from = from; 49 this.subj = subj; 50 this.server = server; 51 52 // Get a unique fingerprint for this appender 53 mask_ = register (to ~ subj); 54 } 55 56 /*********************************************************************** 57 58 Send an event to the mail server 59 60 ***********************************************************************/ 61 62 override final void append (LogEvent event) 63 { 64 synchronized 65 { 66 auto conduit = new Socket; 67 scope (exit) 68 conduit.close(); 69 70 conduit.connect (server); 71 auto emit = new Bout (conduit); 72 73 emit.append ("HELO none@anon.org\r\nMAIL FROM:<") 74 .append (from) 75 .append (">\r\nRCPT TO:<") 76 .append (to) 77 .append (">\r\nDATA\r\nSubject: ") 78 .append (subj) 79 .append ("\r\nContent-Type: text/plain; charset=us-ascii\r\n\r\n"); 80 81 layout.format (event, &emit.write); 82 emit.append ("\r\n.\r\nQUIT\r\n"); 83 emit.flush(); 84 } 85 } 86 87 /*********************************************************************** 88 89 Return the fingerprint for this class 90 91 ***********************************************************************/ 92 93 @property override final const Mask mask () 94 { 95 return mask_; 96 } 97 98 /*********************************************************************** 99 100 Return the name of this class 101 102 ***********************************************************************/ 103 104 @property override final const const(char)[] name () 105 { 106 return this.classinfo.name; 107 } 108 }