1 /* This source file is part of the ATMEL FREERTOS-0.9.0 Release */
\r
3 /*This file has been prepared for Doxygen automatic documentation generation.*/
\r
4 /*! \file *********************************************************************
\r
6 * \brief Basic SMTP Host for AVR32 UC3.
\r
8 * - Compiler: GNU GCC for AVR32
\r
9 * - Supported devices: All AVR32 devices can be used.
\r
12 * \author Atmel Corporation: http://www.atmel.com \n
\r
13 * Support email: avr32@atmel.com
\r
15 *****************************************************************************/
\r
17 /* Copyright (c) 2007, Atmel Corporation All rights reserved.
\r
19 * Redistribution and use in source and binary forms, with or without
\r
20 * modification, are permitted provided that the following conditions are met:
\r
22 * 1. Redistributions of source code must retain the above copyright notice,
\r
23 * this list of conditions and the following disclaimer.
\r
25 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
26 * this list of conditions and the following disclaimer in the documentation
\r
27 * and/or other materials provided with the distribution.
\r
29 * 3. The name of ATMEL may not be used to endorse or promote products derived
\r
30 * from this software without specific prior written permission.
\r
32 * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
\r
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
34 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
\r
35 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
\r
36 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
\r
37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
\r
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
\r
39 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
\r
41 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
46 Implements a simplistic SMTP client.
\r
49 #if (SMTP_USED == 1)
\r
53 /* Scheduler includes. */
\r
54 #include "FreeRTOS.h"
\r
56 #include "BasicSMTP.h"
\r
59 /* Demo includes. */
\r
60 #include "AVR32_EMAC.h"
\r
61 #include "portmacro.h"
\r
62 #include "partest.h"
\r
64 /* lwIP includes. */
\r
65 #include "lwip/api.h"
\r
66 #include "lwip/tcpip.h"
\r
67 #include "lwip/memp.h"
\r
68 #include "lwip/stats.h"
\r
69 #include "lwip/opt.h"
\r
70 #include "lwip/api.h"
\r
71 #include "lwip/arch.h"
\r
72 #include "lwip/sys.h"
\r
73 #include "lwip/sockets.h"
\r
74 #include "netif/loopif.h"
\r
76 /*! SMTP default port */
\r
77 #define SMTP_PORT 25
\r
78 /*! SMTP EHLO code answer */
\r
79 #define SMTP_EHLO_STRING "220"
\r
80 /*! SMTP end of transmission code answer */
\r
81 #define SMTP_END_OF_TRANSMISSION_STRING "221"
\r
82 /*! SMTP OK code answer */
\r
83 #define SMTP_OK_STRING "250"
\r
84 /*! SMTP start of transmission code answer */
\r
85 #define SMTP_START_OF_TRANSMISSION_STRING "354"
\r
86 /*! SMTP DATA<CRLF> */
\r
87 #define SMTP_DATA_STRING "DATA\r\n"
\r
88 /*! SMTP <CRLF>.<CRLF> */
\r
89 #define SMTP_MAIL_END_STRING "\r\n.\r\n"
\r
90 /*! SMTP QUIT<CRLFCRLF> */
\r
91 #define SMTP_QUIT_STRING "QUIT\r\n\r\n"
\r
94 /*! Server address */
\r
95 portCHAR cServer[] = "192.168.0.1";
\r
97 /*! Fill here the ehlo with your SMTP server name */
\r
98 #error configure SMTP server
\r
99 char ehlo[] = "EHLO smtp.domain.com\r\n";
\r
101 /*! Fill here the mailfrom with your mail address */
\r
102 #error configure mail sender
\r
103 char mailfrom[] = "MAIL FROM: <sender@domain.com>\r\n";
\r
105 /*! Fill here the mailto with your contact mail address */
\r
106 #error configure mail receiver
\r
107 char mailto[] = "RCPT TO: <receiver@domain.com>\r\n";
\r
109 /*! Fill here the mailcontent with the mail you want to send */
\r
110 #error configure mail content
\r
111 char mailcontent[] ="Subject: *** SPAM ***\r\nFROM: \"Your Name here\" <sender@domain.com>\r\nTO: \"Your Contact here\" <receiver@domain.com>\r\n\r\nSay what you want here.";
\r
113 Bool bSendMail = pdTRUE;
\r
114 portCHAR cTempBuffer[200];
\r
116 /*! Basic SMTP Host task definition */
\r
117 portTASK_FUNCTION( vBasicSMTPHost, pvParameters )
\r
119 struct sockaddr_in stServeurSockAddr;
\r
121 portLONG lSocket = -1;
\r
126 // wait for a signal to send a mail
\r
127 while (bSendMail != pdTRUE) vTaskDelay(200);
\r
130 memset(&stServeurSockAddr, 0, sizeof(stServeurSockAddr));
\r
131 stServeurSockAddr.sin_len = sizeof(stServeurSockAddr);
\r
132 stServeurSockAddr.sin_addr.s_addr = inet_addr(cServer);
\r
133 stServeurSockAddr.sin_port = htons(SMTP_PORT);
\r
134 stServeurSockAddr.sin_family = AF_INET;
\r
137 bSendMail = pdFALSE;
\r
139 // socket as a stream
\r
140 if ( (lSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
\r
142 // socket failed, blink a LED and stay here
\r
144 vParTestToggleLED( 0 );
\r
148 // connect to the server
\r
149 if(connect(lSocket,(struct sockaddr *)&stServeurSockAddr, sizeof(stServeurSockAddr)) < 0)
\r
151 // connect failed, blink a LED and stay here
\r
153 vParTestToggleLED( 1 );
\r
159 //Server: 220 SMTP Ready
\r
160 /* wait for SMTP Server answer */
\r
163 lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0);
\r
164 }while (lRetval <= 0);
\r
165 if (strncmp(cTempBuffer, SMTP_EHLO_STRING, sizeof(cTempBuffer)) >= 0)
\r
167 //Client: EHLO smtp.domain.com
\r
169 send(lSocket, cEhlo, strlen(cEhlo), 0);
\r
171 /* wait for SMTP Server answer */
\r
174 lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0);
\r
175 }while (lRetval <= 0);
\r
176 if (strncmp(cTempBuffer, SMTP_OK_STRING, sizeof(cTempBuffer)) >= 0)
\r
181 lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0);
\r
182 }while (lRetval <= 0);
\r
183 //Client: MAIL FROM:<sender@domain.com>
\r
184 /* send MAIL FROM */
\r
185 send(lSocket, cMailfrom, strlen(cMailfrom), 0);
\r
187 /* wait for SMTP Server answer */
\r
190 lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0);
\r
191 }while (lRetval <= 0);
\r
192 if (strncmp(cTempBuffer, SMTP_OK_STRING, sizeof(cTempBuffer)) >= 0)
\r
194 //Client: RCPT TO:<receiver@domain.com>
\r
196 send(lSocket, cMailto, strlen(cMailto), 0);
\r
198 /* wait for SMTP Server answer */
\r
201 lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0);
\r
202 }while (lRetval <= 0);
\r
203 if (strncmp(cTempBuffer, SMTP_OK_STRING, sizeof(cTempBuffer)) >= 0)
\r
205 //Client: DATA<CRLF>
\r
207 send(lSocket, SMTP_DATA_STRING, 6, 0);
\r
208 //Server: 354 Start mail input; end with <CRLF>.<CRLF>
\r
209 /* wait for SMTP Server answer */
\r
212 lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0);
\r
213 }while (lRetval <= 0);
\r
214 if (strncmp(cTempBuffer, SMTP_START_OF_TRANSMISSION_STRING, sizeof(cTempBuffer)) >= 0)
\r
217 send(lSocket, cMailcontent, strlen(cMailcontent), 0);
\r
218 //Client: <CRLF>.<CRLF>
\r
219 /* send "<CRLF>.<CRLF>" */
\r
220 send(lSocket, SMTP_MAIL_END_STRING, 5, 0);
\r
222 /* wait for SMTP Server answer */
\r
225 lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0);
\r
226 }while (lRetval <= 0);
\r
227 if (strncmp(cTempBuffer, SMTP_OK_STRING, sizeof(cTempBuffer)) >= 0)
\r
229 //Client: QUIT<CRLFCRLF>
\r
231 send(lSocket, SMTP_QUIT_STRING, 8, 0);
\r
232 //Server: 221 smtp.domain.com closing transmission
\r
235 lRetval = recv(lSocket, cTempBuffer, sizeof(cTempBuffer), 0);
\r
236 }while (lRetval <= 0);
\r
237 if (strncmp(cTempBuffer, SMTP_END_OF_TRANSMISSION_STRING, sizeof(cTempBuffer)) >= 0)
\r
239 vParTestSetLED( 3 , pdTRUE );
\r