SharkSSL C/C++ Reference
TLS, cryptography, certificates, and PSA integration
SMTP.h
Go to the documentation of this file.
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * SMTP example program
10 ****************************************************************************
11 *
12 * $Id: SMTP.h 6825 2026-11-10 21:17:44Z wini $
13 *
14 * COPYRIGHT: Real Time Logic, 2013 - 2026
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
129
134
135
136
171typedef struct SMTP
172{
173#ifdef __cplusplus
174public:
192 SMTP(const char* from,
193 const char* to,
194 const char* subject,
195 const char* smtpServer,
196 int port=25,
197 SharkSsl* sharkSsl=0,
198 bool startTLS=false,
199 const char* username=0,
200 const char* password=0,
201 const char* clientDomainName=0,
202 const char* serverDomainName=0,
203 SeCtx* ctx=0);
204
209 ~SMTP();
210
216 int addRecipient(const char* to);
217
222 void setContentType(const char* type);
223
227 int write(const char* txt, int len = -1);
228
232 int printf(const char* fmt, ...);
233
237 int commit();
238
242 SMTP_ErrCode getEcode() const { return eCode; }
243
246 const char* getEmsg();
247
252 bool bit8() { return eightBit?true:false; }
253
254private:
255
256#endif
257 BufPrint bp;
258 char printBuf[1024];
259
260 SOCKET sock;
261 SharkSsl* sharkSsl;
262 SharkSslCon* sharkSslCon;
263 SMTP_ErrCode eCode;
264 char writeStarted;
265
266 const char* from;
267 const char* to;
268 const char* subject;
269 const char* clientDomainName;
270 const char* serverDomainName;
271 const char* contentType;
272 U8 isSecure;
273 U8 eightBit;
275
276#ifdef __cplusplus
277extern "C" {
278#endif
279
282 const char* from,
283 const char* to,
284 const char* subject,
285 const char* smtpServer,
286 int port,
287 SharkSsl* sharkSsl,
288 int startTLS,
289 const char* username,
290 const char* password,
291 const char* clientDomainName,
292 const char* serverDomainName,
293 SeCtx* ctx);
296
298int SMTP_addRecipient(SMTP* o, const char* to);
300int SMTP_write(SMTP* o, const char* txt, int len);
302int SMTP_printf(SMTP* o, const char* fmt, ...);
304int SMTP_vprintf(SMTP* o, const char* fmt, va_list argList);
308#define SMTP_getEcode(o) (o)->eCode
310const char* SMTP_getEmsg(SMTP* o);
312#define SMTP_setContentType(o,type) (o)->contentType=type
314#define SMTP_bit8(o) (o)->eightBit
315
316#ifdef __cplusplus
317}
318inline SMTP::SMTP(const char* from,
319 const char* to,
320 const char* subject,
321 const char* smtpServer,
322 int port,
323 SharkSsl* sharkSsl,
324 bool startTLS,
325 const char* username,
326 const char* password,
327 const char* clientDomainName,
328 const char* serverDomainName,
329 SeCtx* ctx) {
330 SMTP_constructor(this,from,to,subject,smtpServer,port,sharkSsl,startTLS?1:0,
331 username,password,clientDomainName,serverDomainName,ctx);
332}
333
334inline SMTP::~SMTP() {
335 SMTP_destructor(this);
336}
337
338inline int SMTP::addRecipient(const char* to) {
339 return SMTP_addRecipient(this, to);
340}
341
342inline void SMTP::setContentType(const char* type) {
343 SMTP_setContentType(this,type);
344}
345
346inline int SMTP::write(const char* txt, int len) {
347 return SMTP_write(this, txt, len);
348}
349
350inline int SMTP::printf(const char* fmt, ...) {
351 int retVal;
352 va_list varg;
353 va_start(varg, fmt);
354 retVal = SMTP_vprintf(this, fmt, varg);
355 va_end(varg);
356 return retVal;
357}
358
359inline const char* SMTP::getEmsg() {
360 return SMTP_getEmsg(this);
361}
362
363
364inline int SMTP::commit() {
365 return SMTP_commit(this);
366}
367#endif
368 /* end group SMTP */
370
371#endif
void setContentType(const char *type)
Set content-type.
Definition: SMTP.h:342
int SMTP_commit(SMTP *o)
Documentation: SMTP::commit.
#define SMTP_setContentType(o, type)
Documentation: SMTP::setContentType.
Definition: SMTP.h:312
const char * getEmsg()
Returns the SMTP error message if any.
Definition: SMTP.h:359
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:346
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:334
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:318
int addRecipient(const char *to)
Optionally add more recipients to this email.
Definition: SMTP.h:338
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:364
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:350
@ 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_ErrRng
Example RNG initialization failed.
Definition: SMTP.h:132
@ 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:514
#define SOCKET
Infinite wait time option for socket read functions.
Definition: selib.h:103
This class is a minimal SMTP client that takes an email message body and passes it on to a SMTP serve...
Definition: SMTP.h:172
bool bit8()
Returns true if server supports 8BITMIME.
Definition: SMTP.h:252
SMTP_ErrCode getEcode() const
Get the error code (SMTP_ErrCode), if any.
Definition: SMTP.h:242
SeCtx structure: See Context Manager and Bare Metal Systems for details.
Definition: SeCtx.h:75
A SharkSsl object is the coordinator for managing SharkSslCon objects (See SharkSsl_constructor for d...
Definition: SharkSSL.h:534