WIZlib Library API  ver 1.0
WIZlib Library API User Menual
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
smtp.c
Go to the documentation of this file.
1 
12 //#define FILE_LOG_SILENCE
13 #include "protocol/SMTP/smtp.h"
14 
15 
16 static int8 buf[255];
17 static uint16 client_port = 2000;
18 
19 
20 /*
21  * Make MIME Message.
22  * This function makes MIME message.
23  *
24  * @param MIME pointer to the destination
25  * @param sender pointer to the sender
26  * @param recipient pointer to the recipient
27  * @param subject pointer to the subject of mail
28  * @param content pointer to the content of mail
29  */
30 static void MakeMIME(uint8 *MIME, uint8 *sender, uint8 *recipient, uint8 *subject, uint8 *content)
31 {
32  sprintf((void *)MIME, "From: %s\r\n"\
33  "To: %s\r\n"\
34  "Subject: %s\r\n"\
35  "\r\n%s\r\n"\
36  "\r\n."\
37  , sender, recipient, subject, content);
38 }
39 
40 /*
41  * Send Message and Receive The Reply.
42  * This function makes DNS query message and parses the reply from DNS server.
43  *
44  * @param s a socket number
45  * @param data pointer to the data for send
46  * @param pSip pointer to the server ipr
47  */
48 static void send_receive(uint8 s, uint8 * data, uint8 * pSip)
49 {
50  int16 n;
51 
52  DBGA("Send------>%s", data);
53 
54  n = sprintf((char*)buf, "%s\r\n", data);
55  while(TCPSend(s, (void *)buf, n) <= 0);
56 
57  //recv
58  while((n = TCPRecv(s, (void *)buf, 255)) <= 0);
59  buf[n]='\0';
60 
61  DBGA("Receive------>%s", buf);
62 }
63 
79 int8 send_mail(uint8 s, uint8 *sender, uint8 *passwd,
80  uint8 *recipient, uint8 *subject, uint8 *content, uint8 *pSip)
81 {
82  int8 ret = RET_OK;
83  uint8 MIME[256]={'\0'}, encode[16]={'\0'};
84  int16 n;
85 
86  if (TCPClientOpen(s, client_port++, pSip, 25) == 1) // connnect to server
87  {
88  do{
89  while((n = TCPRecv(s, (void *)buf, 255)) <= 0); //recv
90  buf[n]='\0';
91  DBGA("Receive(%d)------>%s", n, buf);
92  if(strncmp((char*)buf, "220", 3)) {
93  ERR("This is not SMTP Server");
94  ret = RET_NOK;
95  break;
96  }
97  send_receive(s, "HELO", pSip); // Send HELO
98  if(strncmp((char*)buf, "250", 3)) {
99  ERR("Fail HELO");
100  ret = RET_NOK;
101  break;
102  }
103  send_receive(s, "AUTH LOGIN", pSip); // Send AUTH LOGIN
104  if(strncmp((char*)buf, "334", 3)) {
105  ERR("Fail AUTH LOGIN");
106  ret = RET_NOK;
107  break;
108  }
109  base64_encode((void *)sender, strlen((void *)sender)+1, (void *)encode); // Send ID
110  send_receive(s, encode, pSip);
111  if(strncmp((char*)buf, "334", 3)) {
112  ERR("Fail ID");
113  ret = RET_NOK;
114  break;
115  }
116  base64_encode((void *)passwd, strlen((void *)passwd)+1, (void *)encode); // Send PW
117  send_receive(s, encode, pSip);
118  if(strncmp((char*)buf, "235", 3)) {
119  ERR("Fail PW");
120  ret = RET_NOK;
121  break;
122  }
123  sprintf((char*)buf, "MAIL FROM:%s", sender); // Send MAIL FROM
124  send_receive(s, (void *)buf, pSip);
125  if(strncmp((char*)buf, "250", 3)) {
126  ERR("Fail MAIL FROM");
127  ret = RET_NOK;
128  break;
129  }
130 
131  sprintf((char*)buf, "RCPT TO:%s", recipient); // Send RCPT
132  send_receive(s, (void *)buf, pSip);
133  if(strncmp((char*)buf, "250", 3)) {
134  ERR("Fail RCPT TO");
135  ret = RET_NOK;
136  break;
137  }
138  send_receive(s, "DATA", pSip); // Send DATA
139  if(strncmp((char*)buf, "354", 3)) {
140  ERR("Fail DATA");
141  ret = RET_NOK;
142  break;
143  }
144  MakeMIME(MIME, sender, recipient, subject, content); // Send content
145  send_receive(s, MIME, pSip);
146  if(strncmp((char*)buf, "250", 3)) {
147  ERR("Fail Send Content");
148  ret = RET_NOK;
149  break;
150  }
151  send_receive(s, "QUIT", pSip); // Send QUIT
152  NL1;
153  LOG("Send ok");
154  }while(0);
155 
156  }
157  else
158  {
159  NL1;
160  ERR("connection error");
161  return(RET_NOK);
162  }
163 
164  TCPClose(s);
165  while(getSn_SR(s) != SOCK_CLOSED);
166 
167  return(ret);
168 }