smtp-client
SMTP Client C Library
|
SMTP client library. More...
#include <sys/types.h>
#include <stddef.h>
#include <stdio.h>
Go to the source code of this file.
Data Structures | |
struct | smtp_command |
struct | str_getdelimfd |
Macros | |
#define | SIZE_MAX ((size_t)(-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) |
SMTP client library.
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.
#define SIZE_MAX ((size_t)(-1)) |
Maximum value of size_t type.
Definition at line 23 of file smtp.h.
Referenced by smtp_attachment_add_mem(), smtp_auth_cram_md5(), smtp_auth_login(), smtp_base64_decode(), smtp_base64_encode(), smtp_chunk_split(), smtp_ffile_put_contents(), smtp_func_test_attachment_long_text(), smtp_func_test_attachment_mem(), smtp_si_add_size_t(), smtp_si_mul_size_t(), smtp_str_split(), smtp_strnlen_utf8(), smtp_unit_test_all_base64_decode(), smtp_unit_test_all_base64_encode(), smtp_unit_test_all_bin2hex(), smtp_unit_test_all_file_get_contents(), smtp_unit_test_all_reallocarray(), smtp_unit_test_all_si(), smtp_unit_test_all_str_getdelimfd(), smtp_unit_test_all_str_replace(), smtp_unit_test_all_strdup(), smtp_unit_test_all_strnlen_utf8(), test_failure_attachment_add(), test_failure_mail(), and test_smtp_open_default().
enum 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. |
List of supported methods for authenticating a mail user account on the server.
Connect to the SMTP server using either an unencrypted socket or TLS encryption.
enum smtp_flag |
Special flags defining certain behaviors for the SMTP client context.
Definition at line 198 of file smtp.h.
enum smtp_result_code |
SMTP codes returned by the server and parsed by the client.
Definition at line 483 of file smtp.h.
enum 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.
enum str_getdelim_retcode |
Return codes for the getdelim interface which allows the caller to check if more delimited lines can get processed.
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.
[in] | smtp | SMTP client context. |
[in] | type | See smtp_address_type. |
[in] | The email address of the party. Must consist only of printable characters excluding the angle brackets (<) and (>). | |
[in] | name | Name 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. |
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().
void smtp_address_clear_all | ( | struct smtp *const | smtp | ) |
Free all memory related to the address list.
[in] | smtp | SMTP 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().
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.
[in] | smtp | SMTP client context. |
[in] | name | Filename of the attachment shown to recipients. Must consist only of printable ASCII characters, excluding the quote characters (') and ("). |
[in] | fp | File pointer already opened by the caller. |
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().
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.
[in] | smtp | SMTP client context. |
[in] | name | Filename of the attachment shown to recipients. Must consist only of printable ASCII characters, excluding the quote characters (') and ("). |
[in] | data | Raw attachment data stored in memory. |
[in] | datasz | Number of bytes in data , or -1 if data null-terminated. |
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().
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.
[in] | smtp | SMTP client context. |
[in] | name | Filename of the attachment shown to recipients. Must consist only of printable ASCII characters, excluding the quote characters (') and ("). |
[in] | path | Path to file. |
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().
void smtp_attachment_clear_all | ( | struct smtp *const | smtp | ) |
Remove all attachments from the SMTP client context.
[in] | smtp | SMTP 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().
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.
[in] | smtp | SMTP client context. |
[in] | auth_method | See smtp_authentication_method. |
[in] | user | SMTP user name. |
[in] | pass | SMTP password. |
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().
enum smtp_status_code smtp_close | ( | struct smtp * | smtp | ) |
Close the SMTP connection and frees all resources held by the SMTP context.
[in] | smtp | SMTP client context. |
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().
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.
[in] | smtp | SMTP client context. |
[in] | key | Key name for new header. It must consist only of printable US-ASCII characters except colon. |
[in] | value | Value 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. |
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().
void smtp_header_clear_all | ( | struct smtp *const | smtp | ) |
Free all memory related to email headers.
[in] | smtp | SMTP 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().
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.
[in] | smtp | SMTP client context. |
[in] | body | Null-terminated string to send in the email body. |
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().
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.
[in] | server | Server name or IP address. |
[in] | port | Server port number. |
[in] | connection_security | See smtp_connection_security. |
[in] | flags | See smtp_flag. |
[in] | cafile | Path to certificate file, or NULL to use certificates in the default path. |
[out] | smtp | Pointer to a new SMTP context. When finished, the caller must free this context using smtp_close. |
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().
enum smtp_status_code smtp_status_code_clear | ( | struct smtp *const | smtp | ) |
Clear the current error code set in the SMTP client context.
[in,out] | smtp | SMTP client context. |
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().
const char* smtp_status_code_errstr | ( | enum smtp_status_code | status_code | ) |
Convert a standard SMTP client status code to a descriptive string.
[in] | status_code | Status code returned from one of the other library functions. |
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().
enum smtp_status_code smtp_status_code_get | ( | const struct smtp *const | smtp | ) |
Get the current status/error code.
[in] | smtp | SMTP client context. |
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().
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.
[in] | smtp | SMTP client context. |
[in] | new_status_code | 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().