tarball/crossplatform.mk: Removing cp/cpr -p flag breaking build on Linux/NTFS
[sdk] / extras / SMTPSocket.ec
1 #ifdef ECERE_STATIC
2 public import static "ecere"
3 #else
4 public import "ecere"
5 #endif
6
7 // define app = ((MyApp)__thisModule);
8
9 class SMTPSocket : Socket
10 {
11    bool replied;
12    uint OnReceive(const byte * buffer, uint count)
13    {
14 /*#ifdef _DEBUG
15       int c;
16       for(c = 0; c<count; c++)
17          putch(buffer[c]);
18 #endif*/
19       replied = true;
20       return count;
21    }
22    void OnDisconnect(int code)
23    {
24       replied = true;
25    }
26    void WaitReply()
27    {
28       while(!replied)
29          Process();
30       replied = false;
31    }
32 };
33
34 void SMTPSend(char * host, char * to, char * from, File file)
35 {
36    SMTPSocket socket { };
37    incref socket;
38
39    if(socket.Connect(host, 25)) //995
40    {
41       socket.WaitReply();
42       Log("HELO localhost\n");
43       socket.SendString("HELO localhost\r\n");
44       socket.WaitReply();
45       Logf("MAIL from: %s\n", from);
46       socket.Sendf("MAIL from: %s\r\n", from);
47       socket.WaitReply();
48       Logf("RCPT To: %s\n", to);
49       socket.Sendf("RCPT To: %s\r\n", to);
50       socket.WaitReply();
51       Log("DATA\n");
52       socket.SendString("DATA\r\n");
53       socket.WaitReply();
54       Log("Subject: Email test\n");
55       Log("Mime-Version: 1.0;\n");
56       Log("Content-Type: text/html; charset=\"ISO-8859-1\";\n");
57       Log("Content-Transfer-Encoding: 7bit;\n");
58       socket.SendString("Subject: Email test\r\n");
59       socket.SendString("Mime-Version: 1.0;\r\n");
60       socket.SendString("Content-Type: text/html; charset=\"ISO-8859-1\";\r\n");
61       socket.SendString("Content-Transfer-Encoding: 7bit;\r\n");
62       socket.SendString("\r\n");
63
64       file.Seek(0, start);
65       while(!file.Eof())
66       {
67          char buffer[4096];
68          uint read = file.Read(buffer, 1, sizeof(buffer));
69          socket.Send(buffer, read);
70       }
71
72       Log("\n.\n");
73       socket.SendString("\r\n.\r\n");
74       socket.WaitReply();
75       Log("QUIT\n");
76       socket.SendString("QUIT\r\n");
77       socket.WaitReply();
78       socket.Disconnect(0);
79    }
80    delete socket;
81 }
82
83 /*
84 class MyApp : GuiApplication
85 {
86    void Main()
87    {
88       File f = FileOpen("http://server/dir/", read);
89       if(f)
90          Send("mail.server.ca", "user@mail.com", "user@server.com", f);
91       delete f;
92       getch();
93    }
94 }
95 */