1 #ifndef _smtp_smtp_h 2 #define _smtp_smtp_h 3 4 #include <Core/Core.h> 5 6 namespace Upp { 7 8 class Smtp : public TcpSocket { 9 struct Attachment 10 { 11 String name; // mail name 12 String file; // source path (dynamic attachments only) 13 String mime; // content type (application/octet-stream by default) 14 String data; 15 }; 16 17 String host; 18 int port; // default = 25 19 bool ssl; 20 bool starttls; 21 String auth_user; 22 String auth_pwd; 23 String sender; 24 String from; 25 String from_name; 26 Vector<String> to; 27 Vector<String> to_name; 28 Vector<char> as; 29 Vector<String> body; 30 Vector<String> mime; // default: text/plain; charset=<default application charset> 31 Array<Attachment> attachments; 32 int request_timeout; 33 String add_header; 34 35 bool no_header; // default = false 36 bool no_header_sep; // default = false 37 Time time_sent; 38 String reply_to; 39 String reply_to_name; 40 String subject; 41 42 String smtp_msg; 43 int smtp_code; 44 String error; 45 String message_id; 46 47 String GetDomainName(); 48 ValueMap GetExtensions(); 49 int GetSmtpCode(const String& s); 50 void SetSender(); 51 void SetRecipients(); 52 void SendRecv(const String& s); 53 void SendRecvOK(const String& s); 54 void SendMail(const String& msg_); 55 void SendData(const String &s); 56 bool SendHello(); 57 void StartTls(); 58 void Authenticate(); 59 void Quit(); 60 61 String GetMessage(bool chunks); 62 63 ReplyIsWait()64 bool ReplyIsWait() const { return smtp_code >= 100 && smtp_code <= 199; } ReplyIsSuccess()65 bool ReplyIsSuccess() const { return smtp_code >= 200 && smtp_code <= 299; } ReplyIsPending()66 bool ReplyIsPending() const { return smtp_code >= 300 && smtp_code <= 399; } ReplyIsFailure()67 bool ReplyIsFailure() const { return smtp_code >= 400 && smtp_code <= 499; } ReplyIsError()68 bool ReplyIsError() const { return smtp_code >= 500 || smtp_code == -1; } 69 70 public: 71 enum AS { TO, CC, BCC }; 72 RequestTimeout(int ms)73 Smtp& RequestTimeout(int ms) { request_timeout = ms; return *this; } Host(const String & h)74 Smtp& Host(const String& h) { host = h; return *this; } Port(int p)75 Smtp& Port(int p) { port = p; return *this; } 76 Smtp& SSL(bool b = true) { ssl = b; if(b) starttls = !b; return *this; } 77 Smtp& StartTLS(bool b = true) { starttls = b; if(b) ssl = !b; return *this; } Auth(const String & user,const String & pwd)78 Smtp& Auth(const String& user, const String& pwd) { auth_user = user; auth_pwd = pwd; return *this; } 79 Smtp& From(const String& email, const String& name = Null, const String& sender = Null); 80 Smtp& To(const String& email, const String& name, AS a = TO); 81 Smtp& To(const String& email, AS a = TO) { return To(email, Null, a); } 82 Smtp& Cc(const String& email, const String& name = Null) { return To(email, name, CC); } 83 Smtp& Bcc(const String& email, const String& name = Null) { return To(email, name, BCC); } 84 Smtp& ReplyTo(const String& email, const String& name = Null); TimeSent(Time t)85 Smtp& TimeSent(Time t) { time_sent = t; return *this; } 86 Smtp& Subject(const String& s); 87 Smtp& Body(const String& s, const String& mime_ = Null) { body.Add(s); mime.Add(mime_); return *this; } NoHeader()88 Smtp& NoHeader() { no_header = true; return *this; } NoHeaderSep()89 Smtp& NoHeaderSep() { no_header_sep = true; return *this; } 90 Smtp& AttachFile(const char *filename, const char *mime = 0); 91 Smtp& Attach(const char *name, const String& data, const char *mime = 0); AddHeader(const String & text)92 Smtp& AddHeader(const String& text) { add_header << text << "\r\n"; return *this; } AddHeader(const char * id,const String & txt)93 Smtp& AddHeader(const char *id, const String& txt) { add_header << id << ": " << txt << "\r\n"; return *this; } 94 95 Smtp& New(); 96 GetMessage()97 String GetMessage() { return GetMessage(false); } 98 String GetMessageID(); 99 bool Send(const String& message); 100 Send()101 bool Send() { return Send(Null); } 102 GetError()103 String GetError() const { return error; } 104 105 Smtp(); 106 107 static void Trace(bool b = true); 108 static void TraceBody(bool b = true); 109 110 static String Encode(const String& text); 111 static String FormatAddr(const String& addr, const String& name); 112 }; 113 114 } 115 116 #endif 117