smtp-client
SMTP Client C Library
smtp.h File Reference

SMTP client library. More...

#include <sys/types.h>
#include <stddef.h>
#include <stdio.h>
+ Include dependency graph for smtp.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  smtp_command
 
struct  str_getdelimfd
 

Macros

#define SIZE_MAX   ((size_t)(-1))
 

Enumerations

enum  smtp_status_code {
  SMTP_STATUS_OK = 0, SMTP_STATUS_NOMEM = 1, SMTP_STATUS_CONNECT = 2, SMTP_STATUS_HANDSHAKE = 3,
  SMTP_STATUS_AUTH = 4, SMTP_STATUS_SEND = 5, SMTP_STATUS_RECV = 6, SMTP_STATUS_CLOSE = 7,
  SMTP_STATUS_SERVER_RESPONSE = 8, SMTP_STATUS_PARAM = 9, SMTP_STATUS_FILE = 10, SMTP_STATUS_DATE = 11,
  SMTP_STATUS__LAST
}
 
enum  smtp_address_type { SMTP_ADDRESS_FROM = 0, SMTP_ADDRESS_TO = 1, SMTP_ADDRESS_CC = 2, SMTP_ADDRESS_BCC = 3 }
 
enum  smtp_connection_security { SMTP_SECURITY_STARTTLS = 0, SMTP_SECURITY_TLS = 1, SMTP_SECURITY_NONE = 2 }
 
enum  smtp_authentication_method { SMTP_AUTH_CRAM_MD5 = 0, SMTP_AUTH_NONE = 1, SMTP_AUTH_PLAIN = 2, SMTP_AUTH_LOGIN = 3 }
 
enum  smtp_flag { SMTP_DEBUG = 1 << 0, SMTP_NO_CERT_VERIFY = 1 << 1 }
 
enum  smtp_result_code {
  SMTP_INTERNAL_ERROR = -1, SMTP_READY = 220, SMTP_CLOSE = 221, SMTP_AUTH_SUCCESS = 235,
  SMTP_DONE = 250, SMTP_AUTH_CONTINUE = 334, SMTP_BEGIN_MAIL = 354
}
 
enum  str_getdelim_retcode { STRING_GETDELIMFD_ERROR = -1, STRING_GETDELIMFD_NEXT = 0, STRING_GETDELIMFD_DONE = 1 }
 

Functions

enum smtp_status_code smtp_open (const char *const server, const char *const port, enum smtp_connection_security connection_security, enum smtp_flag flags, const char *const cafile, struct smtp **smtp)
 
enum smtp_status_code smtp_auth (struct smtp *const smtp, enum smtp_authentication_method auth_method, const char *const user, const char *const pass)
 
enum smtp_status_code smtp_mail (struct smtp *const smtp, const char *const body)
 
enum smtp_status_code smtp_close (struct smtp *smtp)
 
enum smtp_status_code smtp_status_code_get (const struct smtp *const smtp)
 
enum smtp_status_code smtp_status_code_clear (struct smtp *const smtp)
 
enum smtp_status_code smtp_status_code_set (struct smtp *const smtp, enum smtp_status_code new_status_code)
 
const char * smtp_status_code_errstr (enum smtp_status_code status_code)
 
enum smtp_status_code smtp_header_add (struct smtp *const smtp, const char *const key, const char *const value)
 
void smtp_header_clear_all (struct smtp *const smtp)
 
enum smtp_status_code smtp_address_add (struct smtp *const smtp, enum smtp_address_type type, const char *const email, const char *const name)
 
void smtp_address_clear_all (struct smtp *const smtp)
 
enum smtp_status_code smtp_attachment_add_path (struct smtp *const smtp, const char *const name, const char *const path)
 
enum smtp_status_code smtp_attachment_add_fp (struct smtp *const smtp, const char *const name, FILE *fp)
 
enum smtp_status_code smtp_attachment_add_mem (struct smtp *const smtp, const char *const name, const void *const data, size_t datasz)
 
void smtp_attachment_clear_all (struct smtp *const smtp)
 

Detailed Description

SMTP client library.

Author
James Humphrey (mail@.nosp@m.somn.nosp@m.isoft.nosp@m..com)
Version
1.00

This SMTP client library allows the user to send emails to an SMTP server. The user can include custom headers and MIME attachments.

This software has been placed into the public domain using CC0.

Definition in file smtp.h.

Macro Definition Documentation

◆ SIZE_MAX

Enumeration Type Documentation

◆ smtp_address_type

Address source and destination types.

Enumerator
SMTP_ADDRESS_FROM 

From address.

SMTP_ADDRESS_TO 

To address.

SMTP_ADDRESS_CC 

Copy address.

SMTP_ADDRESS_BCC 

Blind copy address.

Recipients should not see any of the BCC addresses when they receive their email. However, some SMTP server implementations may copy this information into the mail header, so do not assume that this will always get hidden. If the BCC addresses must not get shown to the receivers, then send one separate email to each BCC party and add the TO and CC addresses manually as a header property using smtp_header_add instead of as an address using smtp_address_add.

Definition at line 105 of file smtp.h.

105  {
109  SMTP_ADDRESS_FROM = 0,
110 
114  SMTP_ADDRESS_TO = 1,
115 
119  SMTP_ADDRESS_CC = 2,
120 
133  SMTP_ADDRESS_BCC = 3
134 };

◆ smtp_authentication_method

List of supported methods for authenticating a mail user account on the server.

Enumerator
SMTP_AUTH_CRAM_MD5 

Use HMAC-MD5.

SMTP_AUTH_NONE 

No authentication required.

Some servers support this option if connecting locally.

SMTP_AUTH_PLAIN 

Authenticate using base64 user and password.

SMTP_AUTH_LOGIN 

Another base64 authentication method, similar to SMTP_AUTH_PLAIN.

Definition at line 170 of file smtp.h.

170  {
171 #ifdef SMTP_OPENSSL
172 
175  SMTP_AUTH_CRAM_MD5 = 0,
176 #endif /* SMTP_OPENSSL */
177 
182  SMTP_AUTH_NONE = 1,
183 
187  SMTP_AUTH_PLAIN = 2,
188 
192  SMTP_AUTH_LOGIN = 3
193 };

◆ smtp_connection_security

Connect to the SMTP server using either an unencrypted socket or TLS encryption.

Enumerator
SMTP_SECURITY_STARTTLS 

First connect without encryption, then negotiate an encrypted connection by issuing a STARTTLS command.

Typically used when connecting to a mail server on port 25 or 587.

SMTP_SECURITY_TLS 

Use TLS when initially connecting to server.

Typically used when connecting to a mail server on port 465.

SMTP_SECURITY_NONE 

Do not use TLS encryption.

Not recommended unless connecting to the SMTP server locally.

Definition at line 140 of file smtp.h.

140  {
141 #ifdef SMTP_OPENSSL
142 
149 
155  SMTP_SECURITY_TLS = 1,
156 #endif /* SMTP_OPENSSL */
157 
164 };

◆ smtp_flag

enum smtp_flag

Special flags defining certain behaviors for the SMTP client context.

Enumerator
SMTP_DEBUG 

Print client and server communication on stderr.

SMTP_NO_CERT_VERIFY 

Do not verify TLS certificate.

By default, the TLS handshake function will check if a certificate has expired or if using a self-signed certificate. Either of those conditions will cause the connection to fail. This option allows the connection to proceed even if those checks fail.

Definition at line 198 of file smtp.h.

198  {
202  SMTP_DEBUG = 1 << 0,
203 
212  SMTP_NO_CERT_VERIFY = 1 << 1
213 };

◆ smtp_result_code

SMTP codes returned by the server and parsed by the client.

Enumerator
SMTP_INTERNAL_ERROR 

Client error code which does not get set by the server.

SMTP_READY 

Returned when ready to begin processing next step.

SMTP_CLOSE 

Returned in response to QUIT.

SMTP_AUTH_SUCCESS 

Returned if client successfully authenticates.

SMTP_DONE 

Returned when some commands successfully complete.

SMTP_AUTH_CONTINUE 

Returned for some multi-line authentication mechanisms where this code indicates the next stage in the authentication step.

SMTP_BEGIN_MAIL 

Returned in response to DATA command.

Definition at line 483 of file smtp.h.

483  {
487  SMTP_INTERNAL_ERROR = -1,
488 
492  SMTP_READY = 220,
493 
497  SMTP_CLOSE = 221,
498 
502  SMTP_AUTH_SUCCESS = 235,
503 
507  SMTP_DONE = 250,
508 
513  SMTP_AUTH_CONTINUE = 334,
514 
518  SMTP_BEGIN_MAIL = 354
519 };

◆ smtp_status_code

Status codes indicating success or failure from calling any of the SMTP library functions.

This code gets returned by all functions in this header.

Enumerator
SMTP_STATUS_OK 

Successful operation completed.

SMTP_STATUS_NOMEM 

Memory allocation failed.

SMTP_STATUS_CONNECT 

Failed to connect to the mail server.

SMTP_STATUS_HANDSHAKE 

Failed to handshake or negotiate a TLS connection with the server.

SMTP_STATUS_AUTH 

Failed to authenticate with the given credentials.

SMTP_STATUS_SEND 

Failed to send bytes to the server.

SMTP_STATUS_RECV 

Failed to receive bytes from the server.

SMTP_STATUS_CLOSE 

Failed to properly close a connection.

SMTP_STATUS_SERVER_RESPONSE 

SMTP server sent back an unexpected status code.

SMTP_STATUS_PARAM 

Invalid parameter.

SMTP_STATUS_FILE 

Failed to open or read a local file.

SMTP_STATUS_DATE 

Failed to get the local date and time.

SMTP_STATUS__LAST 

Indicates the last status code in the enumeration, useful for bounds checking.

Not a valid status code.

Definition at line 32 of file smtp.h.

32  {
36  SMTP_STATUS_OK = 0,
37 
42 
47 
52 
56  SMTP_STATUS_AUTH = 4,
57 
61  SMTP_STATUS_SEND = 5,
62 
66  SMTP_STATUS_RECV = 6,
67 
72 
77 
82 
86  SMTP_STATUS_FILE = 10,
87 
91  SMTP_STATUS_DATE = 11,
92 
100 };

◆ str_getdelim_retcode

Return codes for the getdelim interface which allows the caller to check if more delimited lines can get processed.

Enumerator
STRING_GETDELIMFD_ERROR 

An error occurred during the getdelim processing.

STRING_GETDELIMFD_NEXT 

Found a new line and can process more lines in the next call.

STRING_GETDELIMFD_DONE 

Found a new line and unable to read any more lines at this time.

Definition at line 551 of file smtp.h.

Function Documentation

◆ smtp_address_add()

enum smtp_status_code smtp_address_add ( struct smtp *const  smtp,
enum smtp_address_type  type,
const char *const  email,
const char *const  name 
)

Add a FROM, TO, CC, or BCC address destination to this SMTP context.

Note
Some SMTP servers may reject over 100 recipients.
Parameters
[in]smtpSMTP client context.
[in]typeSee smtp_address_type.
[in]emailThe email address of the party. Must consist only of printable characters excluding the angle brackets (<) and (>).
[in]nameName or description of the party. Must consist only of printable characters, excluding the quote characters. If set to NULL or empty string, no name will get associated with this email.
Returns
See smtp_status_code.

Definition at line 3347 of file smtp.c.

References smtp::address_list, smtp_address::email, smtp_address::name, smtp::num_address, smtp_address_validate_email(), smtp_address_validate_name(), smtp_reallocarray(), smtp_si_add_size_t(), smtp_status_code_set(), SMTP_STATUS_NOMEM, SMTP_STATUS_OK, SMTP_STATUS_PARAM, smtp_strdup(), smtp::status_code, and smtp_address::type.

Referenced by SMTPMail::address_add(), mailx_send(), smtp_address_add_check(), and test_nossl_smtp().

3350  {
3351  struct smtp_address *new_address_list;
3352  struct smtp_address *new_address;
3353  size_t num_address_inc;
3354 
3355  if(smtp->status_code != SMTP_STATUS_OK){
3356  return smtp->status_code;
3357  }
3358 
3361  }
3362 
3363  if(name && smtp_address_validate_name(name) < 0){
3365  }
3366 
3367  if(smtp_si_add_size_t(smtp->num_address, 1, &num_address_inc)){
3369  }
3370 
3371  new_address_list = smtp_reallocarray(smtp->address_list,
3372  num_address_inc,
3373  sizeof(*new_address_list));
3374  if(new_address_list == NULL){
3376  }
3377  new_address = &new_address_list[smtp->num_address];
3378 
3379  smtp->address_list = new_address_list;
3380 
3381  new_address->type = type;
3382  new_address->email = smtp_strdup(email);
3383  if(name){
3384  new_address->name = smtp_strdup(name);
3385  }
3386  else{
3387  new_address->name = NULL;
3388  }
3389  if(new_address->email == NULL ||
3390  (new_address->name == NULL && name)){
3391  free(new_address->email);
3392  free(new_address->name);
3394  }
3395  smtp->num_address = num_address_inc;
3396 
3397  return smtp->status_code;
3398 }
enum smtp_address_type type
Definition: smtp.c:109
struct smtp_address * address_list
Definition: smtp.c:179
SMTP_LINKAGE void * smtp_reallocarray(void *ptr, size_t nmemb, size_t size)
Definition: smtp.c:622
SMTP_LINKAGE int smtp_address_validate_name(const char *const name)
Definition: smtp.c:2903
SMTP_LINKAGE int smtp_address_validate_email(const char *const email)
Definition: smtp.c:2878
enum smtp_status_code smtp_status_code_set(struct smtp *const smtp, enum smtp_status_code status_code)
Definition: smtp.c:3231
enum smtp_status_code status_code
Definition: smtp.c:208
SMTP_LINKAGE int smtp_si_add_size_t(const size_t a, const size_t b, size_t *const result)
Definition: smtp.c:252
SMTP_LINKAGE char * smtp_strdup(const char *s)
Definition: smtp.c:650
char * name
Definition: smtp.c:104
size_t num_address
Definition: smtp.c:184
char * email
Definition: smtp.c:99
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_address_clear_all()

void smtp_address_clear_all ( struct smtp *const  smtp)

Free all memory related to the address list.

Parameters
[in]smtpSMTP client context.

Definition at line 3401 of file smtp.c.

References smtp::address_list, smtp_address::email, smtp_address::name, and smtp::num_address.

Referenced by SMTPMail::address_clear_all(), smtp_close(), smtp_func_test_all_address(), and smtp_func_test_all_names().

3401  {
3402  size_t i;
3403  struct smtp_address *address;
3404 
3405  for(i = 0; i < smtp->num_address; i++){
3406  address = &smtp->address_list[i];
3407  free(address->email);
3408  free(address->name);
3409  }
3410  free(smtp->address_list);
3411  smtp->address_list = NULL;
3412  smtp->num_address = 0;
3413 }
struct smtp_address * address_list
Definition: smtp.c:179
char * name
Definition: smtp.c:104
size_t num_address
Definition: smtp.c:184
char * email
Definition: smtp.c:99
+ Here is the caller graph for this function:

◆ smtp_attachment_add_fp()

enum smtp_status_code smtp_attachment_add_fp ( struct smtp *const  smtp,
const char *const  name,
FILE *  fp 
)

Add an attachment using a file pointer.

See smtp_attachment_add_mem for more details.

Parameters
[in]smtpSMTP client context.
[in]nameFilename of the attachment shown to recipients. Must consist only of printable ASCII characters, excluding the quote characters (') and (").
[in]fpFile pointer already opened by the caller.
Returns
See smtp_status_code.

Definition at line 3439 of file smtp.c.

References smtp_attachment_add_mem(), smtp_ffile_get_contents(), smtp_status_code_set(), SMTP_STATUS_FILE, SMTP_STATUS_NOMEM, SMTP_STATUS_OK, and smtp::status_code.

Referenced by SMTPMail::attachment_add_fp(), smtp_func_test_attachment_fp(), and test_failure_attachment_add().

3441  {
3442  char *data;
3443  size_t bytes_read;
3444 
3445  if(smtp->status_code != SMTP_STATUS_OK){
3446  return smtp->status_code;
3447  }
3448 
3449  errno = 0;
3450  if((data = smtp_ffile_get_contents(fp, &bytes_read)) == NULL){
3451  if(errno == ENOMEM){
3453  }
3454  return smtp_status_code_set(smtp, SMTP_STATUS_FILE);
3455  }
3456  smtp_attachment_add_mem(smtp, name, data, bytes_read);
3457  free(data);
3458  return smtp->status_code;
3459 }
SMTP_LINKAGE char * smtp_ffile_get_contents(FILE *stream, size_t *bytes_read)
Definition: smtp.c:1386
enum smtp_status_code smtp_attachment_add_mem(struct smtp *const smtp, const char *const name, const void *const data, size_t datasz)
Definition: smtp.c:3462
enum smtp_status_code smtp_status_code_set(struct smtp *const smtp, enum smtp_status_code status_code)
Definition: smtp.c:3231
enum smtp_status_code status_code
Definition: smtp.c:208
char * name
Definition: smtp.c:104
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_attachment_add_mem()

enum smtp_status_code smtp_attachment_add_mem ( struct smtp *const  smtp,
const char *const  name,
const void *const  data,
size_t  datasz 
)

Add a MIME attachment to this SMTP context with the data retrieved from memory.

The attachment data will get base64 encoded before sending to the server.

Parameters
[in]smtpSMTP client context.
[in]nameFilename of the attachment shown to recipients. Must consist only of printable ASCII characters, excluding the quote characters (') and (").
[in]dataRaw attachment data stored in memory.
[in]dataszNumber of bytes in data, or -1 if data null-terminated.
Returns
See smtp_status_code.

Definition at line 3462 of file smtp.c.

References smtp::attachment_list, smtp_attachment::b64_data, smtp_attachment::name, smtp::num_attachment, SIZE_MAX, smtp_attachment_validate_name(), smtp_base64_encode(), smtp_chunk_split(), SMTP_LINE_MAX, smtp_reallocarray(), smtp_si_add_size_t(), smtp_status_code_set(), SMTP_STATUS_NOMEM, SMTP_STATUS_OK, SMTP_STATUS_PARAM, smtp_strdup(), smtp::status_code, and strlen.

Referenced by SMTPMail::attachment_add_mem(), smtp_attachment_add_fp(), smtp_attachment_add_path(), smtp_func_test_attachment_long_text(), smtp_func_test_attachment_mem(), test_failure_attachment_add(), and test_smtp_open_default().

3465  {
3466  size_t num_attachment_inc;
3467  struct smtp_attachment *new_attachment_list;
3468  struct smtp_attachment *new_attachment;
3469  char *b64_encode;
3470 
3471  if(smtp->status_code != SMTP_STATUS_OK){
3472  return smtp->status_code;
3473  }
3474 
3477  }
3478 
3479  if(datasz == SIZE_MAX){
3480  datasz = strlen(data);
3481  }
3482 
3483  if(smtp_si_add_size_t(smtp->num_attachment, 1, &num_attachment_inc) ||
3484  (new_attachment_list = smtp_reallocarray(
3485  smtp->attachment_list,
3486  num_attachment_inc,
3487  sizeof(*new_attachment_list))) == NULL){
3489  }
3490  smtp->attachment_list = new_attachment_list;
3491  new_attachment = &new_attachment_list[smtp->num_attachment];
3492 
3493  new_attachment->name = smtp_strdup(name);
3494  b64_encode = smtp_base64_encode(data, datasz);
3495  if(new_attachment->name == NULL || b64_encode == NULL){
3496  free(new_attachment->name);
3497  free(b64_encode);
3499  }
3500 
3501  new_attachment->b64_data = smtp_chunk_split(b64_encode,
3502  SMTP_LINE_MAX,
3503  "\r\n");
3504  free(b64_encode);
3505  if(new_attachment->b64_data == NULL){
3506  free(new_attachment->name);
3508  }
3509 
3510  smtp->num_attachment = num_attachment_inc;
3511  return smtp->status_code;
3512 }
char * b64_data
Definition: smtp.c:129
#define SIZE_MAX
Definition: smtp.h:23
SMTP_LINKAGE void * smtp_reallocarray(void *ptr, size_t nmemb, size_t size)
Definition: smtp.c:622
#define SMTP_LINE_MAX
Definition: smtp.c:1218
struct smtp_attachment * attachment_list
Definition: smtp.c:189
SMTP_LINKAGE char * smtp_base64_encode(const char *const buf, size_t buflen)
Definition: smtp.c:825
size_t num_attachment
Definition: smtp.c:194
enum smtp_status_code smtp_status_code_set(struct smtp *const smtp, enum smtp_status_code status_code)
Definition: smtp.c:3231
enum smtp_status_code status_code
Definition: smtp.c:208
#define strlen
Definition: seams.h:273
SMTP_LINKAGE int smtp_attachment_validate_name(const char *const name)
Definition: smtp.c:2927
char * name
Definition: smtp.c:124
SMTP_LINKAGE int smtp_si_add_size_t(const size_t a, const size_t b, size_t *const result)
Definition: smtp.c:252
SMTP_LINKAGE char * smtp_chunk_split(const char *const s, size_t chunklen, const char *const end)
Definition: smtp.c:1312
SMTP_LINKAGE char * smtp_strdup(const char *s)
Definition: smtp.c:650
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_attachment_add_path()

enum smtp_status_code smtp_attachment_add_path ( struct smtp *const  smtp,
const char *const  name,
const char *const  path 
)

Add a file attachment from a path.

See smtp_attachment_add_mem for more details.

Parameters
[in]smtpSMTP client context.
[in]nameFilename of the attachment shown to recipients. Must consist only of printable ASCII characters, excluding the quote characters (') and (").
[in]pathPath to file.
Returns
See smtp_status_code.

Definition at line 3416 of file smtp.c.

References smtp_attachment_add_mem(), smtp_file_get_contents(), smtp_status_code_set(), SMTP_STATUS_FILE, SMTP_STATUS_NOMEM, SMTP_STATUS_OK, and smtp::status_code.

Referenced by SMTPMail::attachment_add_path(), mailx_send(), smtp_func_test_attachment_path(), smtp_func_test_attachment_pdf(), smtp_func_test_gmail_attachment(), and test_failure_attachment_add().

3418  {
3419  char *data;
3420  size_t bytes_read;
3421 
3422  if(smtp->status_code != SMTP_STATUS_OK){
3423  return smtp->status_code;
3424  }
3425 
3426  errno = 0;
3427  if((data = smtp_file_get_contents(path, &bytes_read)) == NULL){
3428  if(errno == ENOMEM){
3430  }
3431  return smtp_status_code_set(smtp, SMTP_STATUS_FILE);
3432  }
3433  smtp_attachment_add_mem(smtp, name, data, bytes_read);
3434  free(data);
3435  return smtp->status_code;
3436 }
SMTP_LINKAGE char * smtp_file_get_contents(const char *const filename, size_t *bytes_read)
Definition: smtp.c:1439
enum smtp_status_code smtp_attachment_add_mem(struct smtp *const smtp, const char *const name, const void *const data, size_t datasz)
Definition: smtp.c:3462
enum smtp_status_code smtp_status_code_set(struct smtp *const smtp, enum smtp_status_code status_code)
Definition: smtp.c:3231
enum smtp_status_code status_code
Definition: smtp.c:208
char * name
Definition: smtp.c:124
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_attachment_clear_all()

void smtp_attachment_clear_all ( struct smtp *const  smtp)

Remove all attachments from the SMTP client context.

Parameters
[in]smtpSMTP client context.

Definition at line 3515 of file smtp.c.

References smtp::attachment_list, smtp_attachment::b64_data, smtp_attachment::name, and smtp::num_attachment.

Referenced by SMTPMail::attachment_clear_all(), and smtp_close().

3515  {
3516  size_t i;
3517  struct smtp_attachment *attachment;
3518 
3519  for(i = 0; i < smtp->num_attachment; i++){
3520  attachment = &smtp->attachment_list[i];
3521  free(attachment->name);
3522  free(attachment->b64_data);
3523  }
3524  free(smtp->attachment_list);
3525  smtp->attachment_list = NULL;
3526  smtp->num_attachment = 0;
3527 }
char * b64_data
Definition: smtp.c:129
struct smtp_attachment * attachment_list
Definition: smtp.c:189
size_t num_attachment
Definition: smtp.c:194
char * name
Definition: smtp.c:124
+ Here is the caller graph for this function:

◆ smtp_auth()

enum smtp_status_code smtp_auth ( struct smtp *const  smtp,
enum smtp_authentication_method  auth_method,
const char *const  user,
const char *const  pass 
)

Authenticate the user using one of the methods listed in smtp_authentication_method.

Parameters
[in]smtpSMTP client context.
[in]auth_methodSee smtp_authentication_method.
[in]userSMTP user name.
[in]passSMTP password.
Returns
See smtp_status_code.

Definition at line 3026 of file smtp.c.

References SMTP_AUTH_CRAM_MD5, smtp_auth_cram_md5(), SMTP_AUTH_LOGIN, smtp_auth_login(), SMTP_AUTH_NONE, SMTP_AUTH_PLAIN, smtp_auth_plain(), SMTP_STATUS_AUTH, smtp_status_code_set(), SMTP_STATUS_OK, SMTP_STATUS_PARAM, and smtp::status_code.

Referenced by SMTPMail::auth(), mailx_send(), and smtp_auth_check().

3029  {
3030  int auth_rc;
3031 
3032  if(smtp->status_code != SMTP_STATUS_OK){
3033  return smtp->status_code;
3034  }
3035 
3036  if(auth_method == SMTP_AUTH_PLAIN){
3037  auth_rc = smtp_auth_plain(smtp, user, pass);
3038  }
3039  else if(auth_method == SMTP_AUTH_LOGIN){
3040  auth_rc = smtp_auth_login(smtp, user, pass);
3041  }
3042 #ifdef SMTP_OPENSSL
3043  else if(auth_method == SMTP_AUTH_CRAM_MD5){
3044  auth_rc = smtp_auth_cram_md5(smtp, user, pass);
3045  }
3046 #endif /* SMTP_OPENSSL */
3047  else if(auth_method == SMTP_AUTH_NONE){
3048  auth_rc = 0;
3049  }
3050  else{
3052  }
3053 
3054  if(auth_rc < 0){
3055  return smtp_status_code_set(smtp, SMTP_STATUS_AUTH);
3056  }
3057 
3058  return smtp->status_code;
3059 }
static int smtp_auth_cram_md5(struct smtp *const smtp, const char *const user, const char *const pass)
Definition: smtp.c:2037
static int smtp_auth_plain(struct smtp *const smtp, const char *const user, const char *const pass)
Definition: smtp.c:1893
static int smtp_auth_login(struct smtp *const smtp, const char *const user, const char *const pass)
Definition: smtp.c:1966
enum smtp_status_code smtp_status_code_set(struct smtp *const smtp, enum smtp_status_code status_code)
Definition: smtp.c:3231
enum smtp_status_code status_code
Definition: smtp.c:208
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_close()

enum smtp_status_code smtp_close ( struct smtp smtp)

Close the SMTP connection and frees all resources held by the SMTP context.

Parameters
[in]smtpSMTP client context.
Returns
See smtp_status_code.

Definition at line 3168 of file smtp.c.

References close, smtp::flags, smtp::gdfd, smtp_address_clear_all(), smtp_attachment_clear_all(), SMTP_FLAG_INVALID_MEMORY, smtp_header_clear_all(), smtp_puts(), SMTP_STATUS_CLOSE, smtp_status_code_set(), SMTP_STATUS_OK, smtp_str_getdelimfd_free(), smtp::sock, smtp::status_code, smtp::tls, smtp::tls_ctx, and smtp::tls_on.

Referenced by SMTPMail::close(), mailx_send(), smtp_close_check(), smtp_func_test_server_secureserver(), and test_nossl_smtp().

3168  {
3169  enum smtp_status_code status_code;
3170 
3171  status_code = smtp->status_code;
3172 
3173  if(smtp->flags == SMTP_FLAG_INVALID_MEMORY){
3174  return status_code;
3175  }
3176 
3177  if(smtp->sock != -1){
3178  /*
3179  * Do not error out if this fails because we still need to free the
3180  * SMTP client resources.
3181  */
3182  smtp->status_code = SMTP_STATUS_OK;
3183  smtp_puts(smtp, "QUIT\r\n");
3184 
3185 #ifdef SMTP_OPENSSL
3186  if(smtp->tls_on){
3187  SSL_free(smtp->tls);
3188  SSL_CTX_free(smtp->tls_ctx);
3189  }
3190 #endif /* SMTP_OPENSSL */
3191 
3192 #ifdef SMTP_IS_WINDOWS
3193  closesocket(smtp->sock);
3194  WSACleanup();
3195 #else /* POSIX */
3196  if(close(smtp->sock) < 0){
3197  if(smtp->status_code == SMTP_STATUS_OK){
3199  }
3200  }
3201 #endif /* SMTP_IS_WINDOWS */
3202  }
3203 
3205  smtp_header_clear_all(smtp);
3206  smtp_address_clear_all(smtp);
3208  if(status_code == SMTP_STATUS_OK){
3209  status_code = smtp->status_code;
3210  }
3211  free(smtp);
3212 
3213  return status_code;
3214 }
#define SMTP_FLAG_INVALID_MEMORY
Definition: smtp.c:2945
struct str_getdelimfd gdfd
Definition: smtp.c:164
int tls_on
Definition: smtp.c:215
SSL_CTX * tls_ctx
Definition: smtp.c:232
void smtp_address_clear_all(struct smtp *const smtp)
Definition: smtp.c:3401
enum smtp_flag flags
Definition: smtp.c:154
#define close
Definition: seams.h:81
SMTP_LINKAGE void smtp_str_getdelimfd_free(struct str_getdelimfd *const gdfd)
Definition: smtp.c:489
static enum smtp_status_code smtp_puts(struct smtp *const smtp, const char *const s)
Definition: smtp.c:1655
void smtp_header_clear_all(struct smtp *const smtp)
Definition: smtp.c:3332
smtp_status_code
Definition: smtp.h:32
void smtp_attachment_clear_all(struct smtp *const smtp)
Definition: smtp.c:3515
enum smtp_status_code smtp_status_code_set(struct smtp *const smtp, enum smtp_status_code status_code)
Definition: smtp.c:3231
enum smtp_status_code status_code
Definition: smtp.c:208
SSL * tls
Definition: smtp.c:227
int sock
Definition: smtp.c:159
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_header_add()

enum smtp_status_code smtp_header_add ( struct smtp *const  smtp,
const char *const  key,
const char *const  value 
)

Add a key/value header to the header list in the SMTP context.

If adding a header with an existing key, this will insert instead of replacing the existing header. See smtp_header_clear_all.

See smtp_mail when overriding the default 'Content-Type' header.

Parameters
[in]smtpSMTP client context.
[in]keyKey name for new header. It must consist only of printable US-ASCII characters except colon.
[in]valueValue for new header. It must consist only of printable US-ASCII, space, or horizontal tab. If set to NULL, this will prevent the header from printing out.
Returns
See smtp_status_code.

Definition at line 3278 of file smtp.c.

References smtp::header_list, smtp_header::key, smtp::num_headers, smtp_header_cmp(), smtp_header_key_validate(), smtp_header_value_validate(), smtp_reallocarray(), smtp_si_add_size_t(), smtp_status_code_set(), SMTP_STATUS_NOMEM, SMTP_STATUS_OK, SMTP_STATUS_PARAM, smtp_strdup(), smtp::status_code, and smtp_header::value.

Referenced by SMTPMail::header_add(), mailx_send(), smtp_append_address_to_header(), smtp_header_add_check(), smtp_mail(), and test_nossl_smtp().

3280  {
3281  struct smtp_header *new_header_list;
3282  struct smtp_header *new_header;
3283  size_t num_headers_inc;
3284 
3285  if(smtp->status_code != SMTP_STATUS_OK){
3286  return smtp->status_code;
3287  }
3288 
3289  if(smtp_header_key_validate(key) < 0){
3291  }
3292 
3295  }
3296 
3297  if(smtp_si_add_size_t(smtp->num_headers, 1, &num_headers_inc) ||
3298  (new_header_list = smtp_reallocarray(
3299  smtp->header_list,
3300  num_headers_inc,
3301  sizeof(*smtp->header_list))) == NULL){
3303  }
3304  smtp->header_list = new_header_list;
3305  new_header = &smtp->header_list[smtp->num_headers];
3306 
3307  new_header->key = smtp_strdup(key);
3308  if(value){
3309  new_header->value = smtp_strdup(value);
3310  }
3311  else{
3312  new_header->value = NULL;
3313  }
3314  if(new_header->key == NULL ||
3315  (new_header->value == NULL && value)){
3316  free(new_header->key);
3317  free(new_header->value);
3319  }
3320 
3321  smtp->num_headers = num_headers_inc;
3322 
3323  qsort(smtp->header_list,
3324  smtp->num_headers,
3325  sizeof(*smtp->header_list),
3326  smtp_header_cmp);
3327 
3328  return smtp->status_code;
3329 }
SMTP_LINKAGE void * smtp_reallocarray(void *ptr, size_t nmemb, size_t size)
Definition: smtp.c:622
SMTP_LINKAGE int smtp_header_key_validate(const char *const key)
Definition: smtp.c:2822
char * key
Definition: smtp.c:139
char * value
Definition: smtp.c:144
struct smtp_header * header_list
Definition: smtp.c:169
size_t num_headers
Definition: smtp.c:174
enum smtp_status_code smtp_status_code_set(struct smtp *const smtp, enum smtp_status_code status_code)
Definition: smtp.c:3231
enum smtp_status_code status_code
Definition: smtp.c:208
SMTP_LINKAGE int smtp_header_value_validate(const char *const value)
Definition: smtp.c:2852
SMTP_LINKAGE int smtp_si_add_size_t(const size_t a, const size_t b, size_t *const result)
Definition: smtp.c:252
static int smtp_header_cmp(const void *v1, const void *v2)
Definition: smtp.c:2802
SMTP_LINKAGE char * smtp_strdup(const char *s)
Definition: smtp.c:650
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_header_clear_all()

void smtp_header_clear_all ( struct smtp *const  smtp)

Free all memory related to email headers.

Parameters
[in]smtpSMTP client context.

Definition at line 3332 of file smtp.c.

References smtp::header_list, smtp_header::key, smtp::num_headers, and smtp_header::value.

Referenced by SMTPMail::header_clear_all(), smtp_close(), smtp_func_test_all_address(), smtp_func_test_all_names(), smtp_func_test_all_nodebug(), smtp_func_test_header_custom_date(), smtp_func_test_header_long(), smtp_func_test_header_null_no_date(), smtp_func_test_html(), and smtp_func_test_html_with_plaintext().

3332  {
3333  size_t i;
3334  struct smtp_header *header;
3335 
3336  for(i = 0; i < smtp->num_headers; i++){
3337  header = &smtp->header_list[i];
3338  free(header->key);
3339  free(header->value);
3340  }
3341  free(smtp->header_list);
3342  smtp->header_list = NULL;
3343  smtp->num_headers = 0;
3344 }
char * key
Definition: smtp.c:139
char * value
Definition: smtp.c:144
struct smtp_header * header_list
Definition: smtp.c:169
size_t num_headers
Definition: smtp.c:174
+ Here is the caller graph for this function:

◆ smtp_mail()

enum smtp_status_code smtp_mail ( struct smtp *const  smtp,
const char *const  body 
)

Sends an email using the addresses, attachments, and headers defined in the current SMTP context.

The caller must call the smtp_open function prior to this.

The 'Date' header will automatically get generated here if it hasn't already been set using smtp_header_add.

If the application overrides the default 'Content-Type' header, then this function will output the body as raw data just below the email headers, and it will not output the attachments added using the smtp_attachment_add_* functions. In other words, the application must create its own MIME sections (if needed) when overriding the 'Content-Type' header.

Parameters
[in]smtpSMTP client context.
[in]bodyNull-terminated string to send in the email body.
Returns
See smtp_status_code.

Definition at line 3062 of file smtp.c.

References smtp::address_list, smtp::header_list, smtp::num_address, smtp::num_headers, SMTP_ADDRESS_CC, SMTP_ADDRESS_FROM, SMTP_ADDRESS_TO, smtp_append_address_to_header(), SMTP_BEGIN_MAIL, SMTP_DATE_MAX_SZ, smtp_date_rfc_2822(), SMTP_DONE, smtp_header_add(), smtp_header_exists(), smtp_mail_envelope_header(), smtp_print_email(), smtp_print_header(), smtp_puts(), smtp_read_and_parse_code(), smtp_set_read_timeout(), smtp_status_code_set(), SMTP_STATUS_DATE, SMTP_STATUS_OK, SMTP_STATUS_PARAM, SMTP_STATUS_SERVER_RESPONSE, smtp::status_code, and smtp_address::type.

Referenced by SMTPMail::mail(), mailx_send(), smtp_mail_check(), and test_nossl_smtp().

3063  {
3064  size_t i;
3065  int has_mail_from;
3066  struct smtp_address *address;
3067  char date[SMTP_DATE_MAX_SZ];
3068 
3069  if(smtp->status_code != SMTP_STATUS_OK){
3070  return smtp->status_code;
3071  }
3072 
3073  /* MAIL timeout 5 minutes. */
3074  smtp_set_read_timeout(smtp, 60 * 5);
3075  has_mail_from = 0;
3076  for(i = 0; i < smtp->num_address; i++){
3077  address = &smtp->address_list[i];
3078  if(address->type == SMTP_ADDRESS_FROM){
3079  if(smtp_mail_envelope_header(smtp,
3080  "MAIL FROM",
3081  address) != SMTP_STATUS_OK){
3082  return smtp->status_code;
3083  }
3084  has_mail_from = 1;
3085  break;
3086  }
3087  }
3088 
3089  if(!has_mail_from){
3091  }
3092 
3093  /* RCPT timeout 5 minutes. */
3094  smtp_set_read_timeout(smtp, 60 * 5);
3095 
3096  for(i = 0; i < smtp->num_address; i++){
3097  address = &smtp->address_list[i];
3098  if(address->type != SMTP_ADDRESS_FROM){
3099  if(smtp_mail_envelope_header(smtp,
3100  "RCPT TO",
3101  address) != SMTP_STATUS_OK){
3102  return smtp->status_code;
3103  }
3104  }
3105  }
3106 
3107  /* DATA timeout 2 minutes. */
3108  smtp_set_read_timeout(smtp, 60 * 2);
3109 
3110  if(smtp_puts(smtp, "DATA\r\n") != SMTP_STATUS_OK){
3111  return smtp->status_code;
3112  }
3113 
3114  /* 354 response to DATA must get returned before we can send the message. */
3117  }
3118 
3119  if(!smtp_header_exists(smtp, "Date")){
3120  if(smtp_date_rfc_2822(date) < 0){
3121  return smtp_status_code_set(smtp, SMTP_STATUS_DATE);
3122  }
3123  if(smtp_header_add(smtp, "Date", date) != SMTP_STATUS_OK){
3124  return smtp->status_code;
3125  }
3126  }
3127 
3128  /* DATA block timeout 3 minutes. */
3129  smtp_set_read_timeout(smtp, 60 * 3);
3130 
3133  "From") != SMTP_STATUS_OK ||
3136  "To") != SMTP_STATUS_OK ||
3139  "Cc") != SMTP_STATUS_OK){
3140  return smtp->status_code;
3141  }
3142 
3143  for(i = 0; i < smtp->num_headers; i++){
3144  if(smtp_print_header(smtp, &smtp->header_list[i]) != SMTP_STATUS_OK){
3145  return smtp->status_code;
3146  }
3147  }
3148 
3149  if(smtp_print_email(smtp, body)){
3150  return smtp->status_code;
3151  }
3152 
3153  /* End of DATA segment. */
3154  if(smtp_puts(smtp, ".\r\n") != SMTP_STATUS_OK){
3155  return smtp->status_code;
3156  }
3157 
3158  /* DATA termination timeout 250 return code - 10 minutes. */
3159  smtp_set_read_timeout(smtp, 60 * 10);
3160  if(smtp_read_and_parse_code(smtp) != SMTP_DONE){
3162  }
3163 
3164  return smtp->status_code;
3165 }
enum smtp_address_type type
Definition: smtp.c:109
struct smtp_address * address_list
Definition: smtp.c:179
static int smtp_header_exists(const struct smtp *const smtp, const char *const key)
Definition: smtp.c:2352
static enum smtp_status_code smtp_print_header(struct smtp *const smtp, const struct smtp_header *const header)
Definition: smtp.c:2624
static enum smtp_status_code smtp_puts(struct smtp *const smtp, const char *const s)
Definition: smtp.c:1655
static int smtp_read_and_parse_code(struct smtp *const smtp)
Definition: smtp.c:1574
static enum smtp_status_code smtp_mail_envelope_header(struct smtp *const smtp, const char *const header, const struct smtp_address *const address)
Definition: smtp.c:2752
struct smtp_header * header_list
Definition: smtp.c:169
static enum smtp_status_code smtp_append_address_to_header(struct smtp *const smtp, enum smtp_address_type address_type, const char *const key)
Definition: smtp.c:2674
SMTP_LINKAGE int smtp_date_rfc_2822(char *const date)
Definition: smtp.c:2236
size_t num_headers
Definition: smtp.c:174
static void smtp_set_read_timeout(struct smtp *const smtp, long seconds)
Definition: smtp.c:2138
enum smtp_status_code smtp_header_add(struct smtp *const smtp, const char *const key, const char *const value)
Definition: smtp.c:3278
static enum smtp_status_code smtp_print_email(struct smtp *const smtp, const char *const body)
Definition: smtp.c:2589
enum smtp_status_code smtp_status_code_set(struct smtp *const smtp, enum smtp_status_code status_code)
Definition: smtp.c:3231
enum smtp_status_code status_code
Definition: smtp.c:208
size_t num_address
Definition: smtp.c:184
#define SMTP_DATE_MAX_SZ
Definition: smtp.c:2222
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_open()

enum smtp_status_code smtp_open ( const char *const  server,
const char *const  port,
enum smtp_connection_security  connection_security,
enum smtp_flag  flags,
const char *const  cafile,
struct smtp **  smtp 
)

Open a connection to an SMTP server and return the context.

After successfully connecting and performing a handshake with the SMTP server, this will return a valid SMTP client context that the application can use in the other library functions.

This always returns a valid SMTP client context even if the server connection or memory allocation fails. In this scenario, the error status will continue to propagate to future library calls for the SMTP context while in this failure mode.

This function will ignore the SIGPIPE signal. Applications that require a handler for that signal should set it up after calling this function.

Parameters
[in]serverServer name or IP address.
[in]portServer port number.
[in]connection_securitySee smtp_connection_security.
[in]flagsSee smtp_flag.
[in]cafilePath to certificate file, or NULL to use certificates in the default path.
[out]smtpPointer to a new SMTP context. When finished, the caller must free this context using smtp_close.
Returns
See smtp_status_code.

Definition at line 2987 of file smtp.c.

References smtp::cafile, calloc, str_getdelimfd::delim, smtp::flags, g_smtp_error, smtp::gdfd, str_getdelimfd::getdelimfd_read, smtp_connect(), smtp_initiate_handshake(), smtp_status_code_get(), smtp_status_code_set(), SMTP_STATUS_CONNECT, SMTP_STATUS_HANDSHAKE, SMTP_STATUS_OK, smtp_str_getdelimfd_read(), smtp::sock, smtp::status_code, and str_getdelimfd::user_data.

Referenced by mailx_send(), SMTPMail::open(), smtp_func_test_all_address(), smtp_func_test_all_body(), smtp_func_test_all_names(), smtp_func_test_all_nodebug(), smtp_func_test_all_status_code_get(), smtp_func_test_all_write(), smtp_func_test_attachment_fp(), smtp_func_test_attachment_long_text(), smtp_func_test_attachment_mem(), smtp_func_test_attachment_path(), smtp_func_test_attachment_pdf(), smtp_func_test_gmail_attachment(), smtp_func_test_header_custom_date(), smtp_func_test_header_long(), smtp_func_test_header_null_no_date(), smtp_func_test_html(), smtp_func_test_html_with_plaintext(), smtp_func_test_send_email(), smtp_func_test_server_secureserver(), test_failure_address_add(), test_failure_attachment_add(), test_failure_close(), test_failure_header_add(), test_failure_misc(), test_failure_open(), test_failure_status_code_set(), test_failure_timeout(), test_nossl_smtp(), and test_smtp_open_default().

2992  {
2993  struct smtp *snew;
2994 
2995  if((snew = calloc(1, sizeof(**smtp))) == NULL){
2996  *smtp = &g_smtp_error;
2997  return smtp_status_code_get(*smtp);
2998  }
2999  *smtp = snew;
3000 
3001  snew->flags = flags;
3002  snew->sock = -1;
3003  snew->gdfd.delim = '\n';
3005  snew->gdfd.user_data = snew;
3006  snew->cafile = cafile;
3007 
3008 #ifndef SMTP_IS_WINDOWS
3009  signal(SIGPIPE, SIG_IGN);
3010 #endif /* !(SMTP_IS_WINDOWS) */
3011 
3012  if(smtp_connect(snew, server, port) < 0){
3014  }
3015 
3016  if(smtp_initiate_handshake(snew,
3017  server,
3018  connection_security) != SMTP_STATUS_OK){
3020  }
3021 
3022  return snew->status_code;
3023 }
#define calloc
Definition: seams.h:73
struct str_getdelimfd gdfd
Definition: smtp.c:164
enum smtp_status_code smtp_status_code_get(const struct smtp *const smtp)
Definition: smtp.c:3217
enum smtp_flag flags
Definition: smtp.c:154
static long smtp_str_getdelimfd_read(struct str_getdelimfd *const gdfd, void *buf, size_t count)
Definition: smtp.c:382
static struct smtp g_smtp_error
Definition: smtp.c:2954
static int smtp_connect(struct smtp *const smtp, const char *const server, const char *const port)
Definition: smtp.c:1703
static enum smtp_status_code smtp_initiate_handshake(struct smtp *const smtp, const char *const server, enum smtp_connection_security connection_security)
Definition: smtp.c:2161
const char * cafile
Definition: smtp.c:221
void * user_data
Definition: smtp.h:615
enum smtp_status_code smtp_status_code_set(struct smtp *const smtp, enum smtp_status_code status_code)
Definition: smtp.c:3231
enum smtp_status_code status_code
Definition: smtp.c:208
int sock
Definition: smtp.c:159
int delim
Definition: smtp.h:620
long(* getdelimfd_read)(struct str_getdelimfd *const gdfd, void *buf, size_t count)
Definition: smtp.h:608
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_status_code_clear()

enum smtp_status_code smtp_status_code_clear ( struct smtp *const  smtp)

Clear the current error code set in the SMTP client context.

Parameters
[in,out]smtpSMTP client context.
Returns
Previous error code before clearing.

Definition at line 3222 of file smtp.c.

References smtp_status_code_get(), smtp_status_code_set(), and SMTP_STATUS_OK.

Referenced by smtp_func_test_all_address(), smtp_func_test_all_status_code_get(), test_failure_address_add(), test_failure_attachment_add(), test_failure_header_add(), and test_failure_status_code_set().

3222  {
3223  enum smtp_status_code old_status;
3224 
3225  old_status = smtp_status_code_get(smtp);
3227  return old_status;
3228 }
enum smtp_status_code smtp_status_code_get(const struct smtp *const smtp)
Definition: smtp.c:3217
smtp_status_code
Definition: smtp.h:32
enum smtp_status_code smtp_status_code_set(struct smtp *const smtp, enum smtp_status_code status_code)
Definition: smtp.c:3231
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ smtp_status_code_errstr()

const char* smtp_status_code_errstr ( enum smtp_status_code  status_code)

Convert a standard SMTP client status code to a descriptive string.

Parameters
[in]status_codeStatus code returned from one of the other library functions.
Returns
String containing a description of the status_code. The caller must not free or modify this string.

Definition at line 3241 of file smtp.c.

References SMTP_STATUS__LAST.

Referenced by mailx_send(), smtp_unit_test_smtp_status_code_errstr(), and SMTPMailException::what().

3241  {
3242  const char *const status_code_err_str[] = {
3243  /* SMTP_STATUS_OK */
3244  "Success",
3245  /* SMTP_STATUS_NOMEM */
3246  "Memory allocation failed",
3247  /* SMTP_STATUS_CONNECT */
3248  "Failed to connect to the mail server",
3249  /* SMTP_STATUS_HANDSHAKE */
3250  "Failed to handshake or negotiate a TLS connection with the server",
3251  /* SMTP_STATUS_AUTH */
3252  "Failed to authenticate with the given credentials",
3253  /* SMTP_STATUS_SEND */
3254  "Failed to send bytes to the server",
3255  /* SMTP_STATUS_RECV */
3256  "Failed to receive bytes from the server",
3257  /* SMTP_STATUS_CLOSE */
3258  "Failed to properly close a connection",
3259  /* SMTP_STATUS_SERVER_RESPONSE */
3260  "SMTP server sent back an unexpected status code",
3261  /* SMTP_STATUS_PARAM */
3262  "Invalid parameter",
3263  /* SMTP_STATUS_FILE */
3264  "Failed to read or open a local file",
3265  /* SMTP_STATUS_DATE */
3266  "Failed to get the local date and time",
3267  /* SMTP_STATUS__LAST */
3268  "Unknown error"
3269  };
3270 
3271  if((unsigned)status_code > SMTP_STATUS__LAST){
3273  }
3274  return status_code_err_str[status_code];
3275 }
enum smtp_status_code status_code
Definition: smtp.c:208
+ Here is the caller graph for this function:

◆ smtp_status_code_get()

enum smtp_status_code smtp_status_code_get ( const struct smtp *const  smtp)

Get the current status/error code.

Parameters
[in]smtpSMTP client context.
Returns
See smtp_status_code.

Definition at line 3217 of file smtp.c.

References smtp::status_code.

Referenced by smtp_func_test_all_status_code_get(), smtp_open(), smtp_status_code_clear(), SMTPMail::status_code_get(), and test_failure_status_code_set().

3217  {
3218  return smtp->status_code;
3219 }
enum smtp_status_code status_code
Definition: smtp.c:208
+ Here is the caller graph for this function:

◆ smtp_status_code_set()

enum smtp_status_code smtp_status_code_set ( struct smtp *const  smtp,
enum smtp_status_code  new_status_code 
)

Set the error status of the SMTP client context and return the same code.

This allows the caller to clear an error status to SMTP_STATUS_OK so that previous errors will stop propagating. However, this will only work correctly for clearing the SMTP_STATUS_PARAM and SMTP_STATUS_FILE errors. Do not use this to clear any other error codes.

Deprecated:
Use smtp_status_code_clear instead.
Parameters
[in]smtpSMTP client context.
[in]new_status_codeSee smtp_status_code.
Returns
See smtp_status_code.

Definition at line 3231 of file smtp.c.

References SMTP_STATUS__LAST, SMTP_STATUS_PARAM, and smtp::status_code.

Referenced by smtp_address_add(), smtp_append_address_to_header(), smtp_attachment_add_fp(), smtp_attachment_add_mem(), smtp_attachment_add_path(), smtp_auth(), smtp_close(), smtp_func_test_all_status_code_get(), smtp_getline(), smtp_header_add(), smtp_initiate_handshake(), smtp_mail(), smtp_mail_envelope_header(), smtp_open(), smtp_print_email(), smtp_print_header(), smtp_print_mime_attachment(), smtp_print_mime_header_and_body(), smtp_puts_terminate(), smtp_status_code_clear(), smtp_str_getdelimfd_read_timeout(), smtp_write(), SMTPMail::status_code_set(), test_failure_address_add(), test_failure_attachment_add(), test_failure_auth(), test_failure_header_add(), test_failure_mail(), and test_failure_status_code_set().

3232  {
3233  if((unsigned)status_code >= SMTP_STATUS__LAST){
3235  }
3236  smtp->status_code = status_code;
3237  return status_code;
3238 }
enum smtp_status_code smtp_status_code_set(struct smtp *const smtp, enum smtp_status_code status_code)
Definition: smtp.c:3231
enum smtp_status_code status_code
Definition: smtp.c:208
+ Here is the caller graph for this function: