Nut/OS  5.0.5
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.

Referenced by NutSmtpSendMail(), and NutSmtpSendMailRequest().

#define MAIL_RCPT_TYPE   0x03

Recipient type mask.

#define MAIL_RCPT_TO   0x01

Standard recipient type.

Referenced by NutSmtpSendMailHeader().

#define MAIL_RCPT_CC   0x02

Carbon copy recipient type.

Referenced by NutSmtpSendMailHeader().

#define MAIL_RCPT_BCC   0x03

Blind carbon copy recipient type.

#define MAIL_RCPT_ACPT   0x20

Recipient accepted flag.

Referenced by NutSmtpSendMail(), and NutSmtpSendMailRequest().

#define MAIL_RCPT_SENT   0x40

Recipient processed flag.

Referenced by NutSmtpSendMail().

#define MAIL_RCPT_FAIL   0x80

Recipient failed flag.

Referenced by NutSmtpSendMailRequest().

#define MAIL_RCPT_DONE   (MAIL_RCPT_SENT | MAIL_RCPT_FAIL)

Recipient done mask.

Referenced by NutSmtpSendMailRequest().

#define SMTP_BUFSIZ   256

Size of the SMTP buffer.

Referenced by NutSmtpSendEncodedLines().

#define SMTPFEAT_VINTAGE   0x00000001

Server doesn't support ESMTP.

Referenced by NutSmtpLogin().

#define SMTPFEAT_AUTH_LOGIN   0x00000002

Server supports LOGIN authentication.

Referenced by NutSmtpLogin().

#define SMTPFEAT_AUTH_PLAIN   0x00000004

Server supports PLAIN authentication.

Referenced by NutSmtpLogin().

#define SMTP_TIMEOUT   600000

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.

References fgets(), NULL, 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.

References fflush(), fputs(), NULL, 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().

References fclose(), free(), NULL, 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.

References _fdopen(), calloc, NULL, 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.

References malloc(), memcpy(), NULL, 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.

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

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.

References fprintf(), gmtime(), _MAILENVELOPE::mail_date, _MAILENVELOPE::mail_from_header, MAIL_RCPT_CC, MAIL_RCPT_TO, _MAILENVELOPE::mail_subj, NULL, 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.

References EOF, fputc(), fputs(), NULL, 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.

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

Here is the call graph for this function: