Nut/OS  4.10.3
API Reference
XgSMTPC

Data Structures

struct  _MAILENVELOPE
 Mail envelope structure type. More...
struct  _SMTPCLIENTSESSION
 SMTP session structure type. More...

Defines

#define MAX_MAIL_RCPTS   4
 Maximum number of recipients.
#define SMTP_BUFSIZ   256
 Size of the SMTP buffer.
#define SMTP_TIMEOUT   600000

Typedefs

typedef struct _MAILENVELOPE MAILENVELOPE
 Mail envelope structure type.
typedef struct _SMTPCLIENTSESSION SMTPCLIENTSESSION
 SMTP session structure type.

Functions

CONST char * NutSmtpReceiveResponse (SMTPCLIENTSESSION *si)
 Read a response line from the server.
CONST char * NutSmtpSendCommand (SMTPCLIENTSESSION *si, CONST char *fmt,...)
 Send command to the server and return the first response line.
void NutSmtpDisconnect (SMTPCLIENTSESSION *si)
 Terminate an SMTP session.
SMTPCLIENTSESSIONNutSmtpConnect (uint32_t ip, uint16_t port)
 Start an SMTP session.
int NutSmtpLogin (SMTPCLIENTSESSION *si, char *host, char *user, char *pass)
 Identify the client SMTP to the server.
int NutSmtpSendMailRequest (SMTPCLIENTSESSION *si, MAILENVELOPE *me)
 Send mail request.
int NutSmtpSendMailHeader (SMTPCLIENTSESSION *si, MAILENVELOPE *me)
 Send major mail header lines.
int NutSmtpSendEncodedLines (SMTPCLIENTSESSION *si, CONST char *text)
 Send encoded email text lines.
CONST char * NutSmtpSendMail (SMTPCLIENTSESSION *si, MAILENVELOPE *me)
 Send an email via an active SMTP session.

Recipient Status Bits

#define MAIL_RCPT_TYPE   0x03
 Recipient type mask.
#define MAIL_RCPT_TO   0x01
 Standard recipient type.
#define MAIL_RCPT_CC   0x02
 Carbon copy recipient type.
#define MAIL_RCPT_BCC   0x03
 Blind carbon copy recipient type.
#define MAIL_RCPT_ACPT   0x20
 Recipient accepted flag.
#define MAIL_RCPT_SENT   0x40
 Recipient processed flag.
#define MAIL_RCPT_FAIL   0x80
 Recipient failed flag.
#define MAIL_RCPT_DONE   (MAIL_RCPT_SENT | MAIL_RCPT_FAIL)
 Recipient done mask.

Server Features

#define SMTPFEAT_VINTAGE   0x00000001
 Server doesn't support ESMTP.
#define SMTPFEAT_AUTH_LOGIN   0x00000002
 Server supports LOGIN authentication.
#define SMTPFEAT_AUTH_PLAIN   0x00000004
 Server supports PLAIN authentication.

Define Documentation

#define MAX_MAIL_RCPTS   4

Maximum number of recipients.

Definition at line 60 of file smtpc.h.

Referenced by NutSmtpSendMail(), and NutSmtpSendMailRequest().

#define MAIL_RCPT_TYPE   0x03

Recipient type mask.

Definition at line 66 of file smtpc.h.

#define MAIL_RCPT_TO   0x01

Standard recipient type.

Definition at line 68 of file smtpc.h.

Referenced by NutSmtpSendMailHeader().

#define MAIL_RCPT_CC   0x02

Carbon copy recipient type.

Definition at line 70 of file smtpc.h.

Referenced by NutSmtpSendMailHeader().

#define MAIL_RCPT_BCC   0x03

Blind carbon copy recipient type.

Definition at line 72 of file smtpc.h.

#define MAIL_RCPT_ACPT   0x20

Recipient accepted flag.

Definition at line 74 of file smtpc.h.

Referenced by NutSmtpSendMail(), and NutSmtpSendMailRequest().

#define MAIL_RCPT_SENT   0x40

Recipient processed flag.

Definition at line 76 of file smtpc.h.

Referenced by NutSmtpSendMail().

#define MAIL_RCPT_FAIL   0x80

Recipient failed flag.

Definition at line 78 of file smtpc.h.

Referenced by NutSmtpSendMailRequest().

#define MAIL_RCPT_DONE   (MAIL_RCPT_SENT | MAIL_RCPT_FAIL)

Recipient done mask.

Definition at line 80 of file smtpc.h.

Referenced by NutSmtpSendMailRequest().

#define SMTP_BUFSIZ   256

Size of the SMTP buffer.

Definition at line 107 of file smtpc.h.

Referenced by NutSmtpSendEncodedLines().

#define SMTPFEAT_VINTAGE   0x00000001

Server doesn't support ESMTP.

Definition at line 113 of file smtpc.h.

Referenced by NutSmtpLogin().

#define SMTPFEAT_AUTH_LOGIN   0x00000002

Server supports LOGIN authentication.

Definition at line 115 of file smtpc.h.

Referenced by NutSmtpLogin().

#define SMTPFEAT_AUTH_PLAIN   0x00000004

Server supports PLAIN authentication.

Definition at line 117 of file smtpc.h.

Referenced by NutSmtpLogin().

#define SMTP_TIMEOUT   600000

Definition at line 62 of file smtpc.c.

Referenced by NutSmtpConnect().


Typedef Documentation

typedef struct _MAILENVELOPE MAILENVELOPE

Mail envelope structure type.

SMTP session structure type.


Function Documentation

CONST char* NutSmtpReceiveResponse ( SMTPCLIENTSESSION si)

Read a response line from the server.

Returns:
Pointer to a buffer containing the response. In case of a broken connection or a line overflow, a NULL pointer is returned.

Definition at line 129 of file smtpc.c.

References fgets(), NUTASSERT, _SMTPCLIENTSESSION::smtp_buff, _SMTPCLIENTSESSION::smtp_stream, and strchr().

Referenced by NutSmtpConnect(), NutSmtpLogin(), NutSmtpSendCommand(), and NutSmtpSendMail().

Here is the call graph for this function:

CONST char* NutSmtpSendCommand ( SMTPCLIENTSESSION si,
CONST char *  fmt,
  ... 
)

Send command to the server and return the first response line.

If a multi-line response is expected, the caller may use NutSmtpReceiveResponse() to receive additional response lines.

Parameters:
fmtFormat string containing conversion specifications like printf.

Definition at line 159 of file smtpc.c.

References fflush(), fputs(), NUTASSERT, NutSmtpReceiveResponse(), _SMTPCLIENTSESSION::smtp_stream, and vfprintf().

Referenced by NutSmtpDisconnect(), NutSmtpLogin(), NutSmtpSendMail(), and NutSmtpSendMailRequest().

Here is the call graph for this function:

void NutSmtpDisconnect ( SMTPCLIENTSESSION si)

Terminate an SMTP session.

Gracefully closes the SMTP connection.

Parameters:
siPointer to the SMTPCLIENTSESSION structure, obtained from a previous call to NutSmtpConnect().

Definition at line 184 of file smtpc.c.

References fclose(), free(), NUTASSERT, NutSmtpSendCommand(), NutTcpCloseSocket(), _SMTPCLIENTSESSION::smtp_sock, and _SMTPCLIENTSESSION::smtp_stream.

Referenced by NutSmtpConnect().

Here is the call graph for this function:

SMTPCLIENTSESSION* NutSmtpConnect ( uint32_t  ip,
uint16_t  port 
)

Start an SMTP session.

Parameters:
ipIP address of the host to connect.
portPort number to connect. Typically port 25 is used by SMTP.
Returns:
A pointer to a newly create SMTPCLIENTSESSION structure, if the server is connected and ready to accept commands. Otherwise a NULL pointer is returned.

Definition at line 209 of file smtpc.c.

References _fdopen(), calloc, NutSmtpDisconnect(), NutSmtpReceiveResponse(), NutTcpConnect(), NutTcpCreateSocket(), NutTcpSetSockOpt(), _SMTPCLIENTSESSION::smtp_sock, _SMTPCLIENTSESSION::smtp_stream, SMTP_TIMEOUT, and SO_RCVTIMEO.

Here is the call graph for this function:

int NutSmtpLogin ( SMTPCLIENTSESSION si,
char *  host,
char *  user,
char *  pass 
)

Identify the client SMTP to the server.

Parameters:
siPointer to the SMTPCLIENTSESSION structure, obtained from a previous call to NutSmtpConnect().
hostPrimary host name, or NULL if the local host has no name.
userLogin name. Set to NULL if authorization is not required.
passLogin password. Ignored if login name is set to NULL.
Returns:
0 on success, -1 otherwise.

Definition at line 258 of file smtpc.c.

References CONST, malloc(), memcpy(), NUTASSERT, NutSmtpReceiveResponse(), NutSmtpSendCommand(), _SMTPCLIENTSESSION::smtp_buff, _SMTPCLIENTSESSION::smtp_feat, SMTPFEAT_AUTH_LOGIN, SMTPFEAT_AUTH_PLAIN, SMTPFEAT_VINTAGE, strlen(), strncmp(), and strstr().

Here is the call graph for this function:

int NutSmtpSendMailRequest ( SMTPCLIENTSESSION si,
MAILENVELOPE me 
)

Send mail request.

Parameters:
siPointer to the SMTPCLIENTSESSION structure, obtained from a previous call to NutSmtpConnect().
mePointer to the MAILENVELOPE structure.
Returns:
Number of recipients accepted by the server. If 0, no mail should be sent. In any case the caller should inspect the status of each recipient address in the mail envelope structure.

Definition at line 328 of file smtpc.c.

References CONST, _MAILENVELOPE::mail_from, _MAILENVELOPE::mail_rcpt, MAIL_RCPT_ACPT, MAIL_RCPT_DONE, MAIL_RCPT_FAIL, _MAILENVELOPE::mail_rcpt_stat, MAX_MAIL_RCPTS, NUTASSERT, and NutSmtpSendCommand().

Referenced by NutSmtpSendMail().

Here is the call graph for this function:

int NutSmtpSendMailHeader ( SMTPCLIENTSESSION si,
MAILENVELOPE me 
)

Send major mail header lines.

This function will send the following header lines:

  • Date
  • From
  • Subject
  • To
  • CC

The header information must be supplied by the caller in the mail envelope structure.

Note, that BCC recipients are not included.

The caller may add additional headers using stdio functions with the stream handle available in the SMTPCLIENTSESSION structure.

Parameters:
siPointer to the SMTPCLIENTSESSION structure, obtained from a previous call to NutSmtpConnect().
mePointer to the MAILENVELOPE structure.
Returns:
Always 0 right now. Later versions may return -1 in case of an error.

Definition at line 423 of file smtpc.c.

References fprintf(), gmtime(), _MAILENVELOPE::mail_date, _MAILENVELOPE::mail_from_header, MAIL_RCPT_CC, MAIL_RCPT_TO, _MAILENVELOPE::mail_subj, NUTASSERT, Rfc1123TimeString(), and _SMTPCLIENTSESSION::smtp_stream.

Referenced by NutSmtpSendMail().

Here is the call graph for this function:

int NutSmtpSendEncodedLines ( SMTPCLIENTSESSION si,
CONST char *  text 
)

Send encoded email text lines.

Each line will be terminated by a carriage return / linefeed pair. Lines larger than SMTP_BUFSIZ are split. This may occure in the middle of a word. A dot will be put in front of lines that start with a dot.

Parameters:
siPointer to the SMTPCLIENTSESSION structure, obtained from a previous call to NutSmtpConnect().
textPointer to a string containing the text lines to send. Lines should be separated by linefeeds and may be optionally prepended by a carriage return. If the pointer points to an empty string, nothing will be sent.
Returns:
0 on success, -1 otherwise. An error typically indicates a broken connection.

Definition at line 459 of file smtpc.c.

References EOF, fputc(), fputs(), NUTASSERT, _SMTPCLIENTSESSION::smtp_buff, SMTP_BUFSIZ, and _SMTPCLIENTSESSION::smtp_stream.

Referenced by NutSmtpSendMail().

Here is the call graph for this function:

CONST char* NutSmtpSendMail ( SMTPCLIENTSESSION si,
MAILENVELOPE me 
)

Send an email via an active SMTP session.

Applications may use the following basic sequence to send an email:

 #include <pro/smtpc.h>

 MAILENVELOPE email = {
     0, "<me@ethernut.de>", "Problem",
     "Be warned.",
     { "<admin@ethernut.de>", NULL, NULL, NULL },
     { MAIL_RCPT_TO, 0, 0, 0 }
 };
 SMTPCLIENTSESSION *smtp;

 smtp = NutSmtpConnect(daemon_ip, 25);
 NutSmtpLogin(smtp, NULL, NULL, NULL);
 NutSmtpSendMail(smtp, &email);
 NutSmtpDisconnect(smtp);

More advanced mail transfers may be implemented by using the other routines of this API for sending commands or parts of an email individually. Even the stream in the session structure may be used with stdio calls.

Parameters:
siPointer to the SMTPCLIENTSESSION structure, obtained from a previous call to NutSmtpConnect().
mePointer to the MAILENVELOPE structure. On return, the caller should inspect the status of each recipient.
Returns:
Pointer to a buffer containing the last response. The status of the recipients in the envelope will have been updated. In case of a fatal error or if all recipients had been rejected, a NULL pointer is returned.

Definition at line 540 of file smtpc.c.

References CONST, fflush(), fputs(), _MAILENVELOPE::mail_body, _MAILENVELOPE::mail_rcpt, MAIL_RCPT_ACPT, MAIL_RCPT_SENT, _MAILENVELOPE::mail_rcpt_stat, MAX_MAIL_RCPTS, NUTASSERT, NutSmtpReceiveResponse(), NutSmtpSendCommand(), NutSmtpSendEncodedLines(), NutSmtpSendMailHeader(), NutSmtpSendMailRequest(), and _SMTPCLIENTSESSION::smtp_stream.

Here is the call graph for this function: