SharkSSL™ Embedded SSL/TLS Stack
SMTP.h
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * SMTP example program
10 ****************************************************************************
11 *
12 * $Id: SMTP.h 4324 2018-11-17 17:13:25Z wini $
13 *
14 * COPYRIGHT: Real Time Logic, 2013 - 2015
15 *
16 * This software is copyrighted by and is the sole property of Real
17 * Time Logic LLC. All rights, title, ownership, or other interests in
18 * the software remain the property of Real Time Logic LLC. This
19 * software may only be used in accordance with the terms and
20 * conditions stipulated in the corresponding license agreement under
21 * which the software has been supplied. Any unauthorized use,
22 * duplication, transmission, distribution, or disclosure of this
23 * software is expressly forbidden.
24 *
25 * This Copyright notice may not be removed or modified without prior
26 * written consent of Real Time Logic LLC.
27 *
28 * Real Time Logic LLC. reserves the right to modify this software
29 * without notice.
30 *
31 * http://www.realtimelogic.com
32 ****************************************************************************
33 */
34
35#ifndef _SMTP_h
36#define _SMTP_h
37
38#include <selib.h>
39#include "BufPrint.h"
40
63typedef enum {
70
71 /* error codes below */
72
76
80
84
88
92
96
100
104
108
112
116
120
124
130
131
132
169typedef struct SMTP
170{
171#ifdef __cplusplus
172public:
190 SMTP(const char* from,
191 const char* to,
192 const char* subject,
193 const char* smtpServer,
194 int port=25,
195 SharkSsl* sharkSsl=0,
196 bool startTLS=false,
197 const char* username=0,
198 const char* password=0,
199 const char* clientDomainName=0,
200 const char* serverDomainName=0,
201 SeCtx* ctx=0);
202
207 ~SMTP();
208
214 int addRecipient(const char* to);
215
220 void setContentType(const char* type);
221
225 int write(const char* txt, int len = -1);
226
230 int printf(const char* fmt, ...);
231
235 int commit();
236
240 SMTP_ErrCode getEcode() const { return eCode; }
241
244 const char* getEmsg();
245
250 bool bit8() { return eightBit?true:false; }
251
252private:
253
254#endif
255 BufPrint bp;
256 char printBuf[1024];
257
258 SOCKET sock;
259 SharkSsl* sharkSsl;
260 SharkSslCon* sharkSslCon;
261 SMTP_ErrCode eCode;
262 char writeStarted;
263
264 const char* from;
265 const char* to;
266 const char* subject;
267 const char* clientDomainName;
268 const char* serverDomainName;
269 const char* contentType;
270 U8 isSecure;
271 U8 eightBit;
273
274#ifdef __cplusplus
275extern "C" {
276#endif
277
280 const char* from,
281 const char* to,
282 const char* subject,
283 const char* smtpServer,
284 int port,
285 SharkSsl* sharkSsl,
286 int startTLS,
287 const char* username,
288 const char* password,
289 const char* clientDomainName,
290 const char* serverDomainName,
291 SeCtx* ctx);
294
296int SMTP_addRecipient(SMTP* o, const char* to);
298int SMTP_write(SMTP* o, const char* txt, int len);
300int SMTP_printf(SMTP* o, const char* fmt, ...);
302int SMTP_vprintf(SMTP* o, const char* fmt, va_list argList);
306#define SMTP_getEcode(o) (o)->eCode
308const char* SMTP_getEmsg(SMTP* o);
310#define SMTP_setContentType(o,type) (o)->contentType=type
312#define SMTP_bit8(o) (o)->eightBit
313
314#ifdef __cplusplus
315}
316inline SMTP::SMTP(const char* from,
317 const char* to,
318 const char* subject,
319 const char* smtpServer,
320 int port,
321 SharkSsl* sharkSsl,
322 bool startTLS,
323 const char* username,
324 const char* password,
325 const char* clientDomainName,
326 const char* serverDomainName,
327 SeCtx* ctx) {
328 SMTP_constructor(this,from,to,subject,smtpServer,port,sharkSsl,startTLS?1:0,
329 username,password,clientDomainName,serverDomainName,ctx);
330}
331
332inline SMTP::~SMTP() {
333 SMTP_destructor(this);
334}
335
336inline int SMTP::addRecipient(const char* to) {
337 return SMTP_addRecipient(this, to);
338}
339
340inline void SMTP::setContentType(const char* type) {
341 SMTP_setContentType(this,type);
342}
343
344inline int SMTP::write(const char* txt, int len) {
345 return SMTP_write(this, txt, len);
346}
347
348inline int SMTP::printf(const char* fmt, ...) {
349 int retVal;
350 va_list varg;
351 va_start(varg, fmt);
352 retVal = SMTP_vprintf(this, fmt, varg);
353 va_end(varg);
354 return retVal;
355}
356
357inline const char* SMTP::getEmsg() {
358 return SMTP_getEmsg(this);
359}
360
361
362inline int SMTP::commit() {
363 return SMTP_commit(this);
364}
365#endif
366 /* end group SMTP */
368
369#endif
void setContentType(const char *type)
Set content-type.
Definition: SMTP.h:340
int SMTP_commit(SMTP *o)
Documentation: SMTP::commit.
#define SMTP_setContentType(o, type)
Documentation: SMTP::setContentType.
Definition: SMTP.h:310
const char * getEmsg()
Returns the SMTP error message if any.
Definition: SMTP.h:357
int SMTP_vprintf(SMTP *o, const char *fmt, va_list argList)
Documentation: SMTP::vprintf.
int write(const char *txt, int len=-1)
Add body text to email.
Definition: SMTP.h:344
int SMTP_printf(SMTP *o, const char *fmt,...)
Documentation: SMTP::printf.
~SMTP()
The destructor automatically commits the email if you do not call SMTP::commit.
Definition: SMTP.h:332
SMTP_ErrCode
SMTP error codes.
Definition: SMTP.h:63
SMTP(const char *from, const char *to, const char *subject, const char *smtpServer, int port=25, SharkSsl *sharkSsl=0, bool startTLS=false, const char *username=0, const char *password=0, const char *clientDomainName=0, const char *serverDomainName=0, SeCtx *ctx=0)
Initiate the SMTP email message.
Definition: SMTP.h:316
int addRecipient(const char *to)
Optionally add more recipients to this email.
Definition: SMTP.h:336
void SMTP_constructor(SMTP *o, const char *from, const char *to, const char *subject, const char *smtpServer, int port, SharkSsl *sharkSsl, int startTLS, const char *username, const char *password, const char *clientDomainName, const char *serverDomainName, SeCtx *ctx)
Documentation: SMTP::SMTP.
void SMTP_destructor(SMTP *o)
Documentation: SMTP::~SMTP.
int SMTP_write(SMTP *o, const char *txt, int len)
Documentation: SMTP::write.
int SMTP_addRecipient(SMTP *o, const char *to)
Documentation: SMTP::addRecipient.
int commit()
Commit (send) email.
Definition: SMTP.h:362
const char * SMTP_getEmsg(SMTP *o)
Returns the SMTP error message if any.
struct SMTP SMTP
This class is a minimal SMTP client that takes an email message body and passes it on to a SMTP serve...
int printf(const char *fmt,...)
Add body text to email.
Definition: SMTP.h:348
@ SMTP_ErrWriteStarted
Cannot add recipients after data is committed: see SMTP_DataCommitted.
Definition: SMTP.h:111
@ SMTP_ErrSocketClosed
Socket unexpectedly closed.
Definition: SMTP.h:103
@ SMTP_ErrNoAuthSup
Unknown/unsupported server authentication request.
Definition: SMTP.h:115
@ SMTP_ErrSmtpResponseCode
Cannot decode response data.
Definition: SMTP.h:107
@ SMTP_DataCommitted
Not an error code: means that the library has started sending data.
Definition: SMTP.h:69
@ SMTP_ErrHostName
Hostname does not resolve or the client is unable to connect to server.
Definition: SMTP.h:75
@ SMTP_ErrDATA
Failed sending the initial DATA message.
Definition: SMTP.h:95
@ SMTP_ErrRCPTTO
Failed sending RCPT TO:<s>
Definition: SMTP.h:99
@ SMTP_ErrCertNotTrusted
Server certificate not trusted.
Definition: SMTP.h:128
@ SMTP_ErrSslCon
SharkSsl_createCon failed.
Definition: SMTP.h:123
@ SMTP_ErrSocketCreate
Cannot create socket.
Definition: SMTP.h:79
@ SMTP_ErrMAILFROM
Failed sending MAIL FROM:<s> BODY=8BITMIME.
Definition: SMTP.h:91
@ SMTP_ErrConnect
Cannot connect.
Definition: SMTP.h:83
@ SMTP_ErrAuth
Server requires credentials, but no credentials were provided.
Definition: SMTP.h:119
@ SMTP_NoError
OK.
Definition: SMTP.h:66
@ SMTP_ErrHELO
EHLO (Extended SMTP (ESMTP)) required, but not supported by server.
Definition: SMTP.h:87
struct SharkSslCon SharkSslCon
SharkSslCon is an opaque handle returned by function SharkSsl_createCon.
Definition: SharkSSL.h:553
#define SOCKET
Infinite wait time option for socket read functions.
Definition: selib.h:102
This class is a minimal SMTP client that takes an email message body and passes it on to a SMTP serve...
Definition: SMTP.h:170
bool bit8()
Returns true if server supports 8BITMIME.
Definition: SMTP.h:250
SMTP_ErrCode getEcode() const
Get the error code (SMTP_ErrCode), if any.
Definition: SMTP.h:240
SeCtx structure: See Context Manager and Bare Metal Systems for details.
Definition: SeCtx.h:71
A SharkSsl object is the coordinator for managing SharkSslCon objects (See SharkSsl_constructor for d...
Definition: SharkSSL.h:561