TReK ANSI-C  5.3.3
All ANSI-C APIs
email_example/main.c
// ////////////////////////////////////////////////////////////////////////////
//
// Example program initializes the email library using an imaginary
// email account and attempts to send an email to an imaginary group address.
// The configuration is saved in a file and the configuration file is
// then used to send a second email to an imaginary group address.
// This program does not actually send an email. A real email account
// and group address are required to actually send an email.
//
// ////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include "trek_error.h"
#include <sys/timeb.h>
#include <string.h>
#include "ds_shared.h"
#ifdef __linux__
#include <unistd.h>
#else
#include <Windows.h>
#endif
// ////////////////////////////////////////////////////////////////////////////
//
// PrintTheMessage controls how messages are processed and displayed to
// the user.
//
// ////////////////////////////////////////////////////////////////////////////
void PrintTheMessage(message_struct_type *mess_struct_ptr)
{
printf("%s%s\n", GetMessageCategoryAsPaddedString(mess_struct_ptr->category),
mess_struct_ptr->message);
}
// ////////////////////////////////////////////////////////////////////////////
//
// Example program initializes the email library using an imaginary
// email account and attempts to send an email to an imaginary group address.
// The configuration is saved in a file and the configuration file is
// then used to send a second email to an imaginary group address.
// This program does not actually send an email. A real email account
// and group address are required to actually send an email.
//
// ////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
char smtp_server_address[50];
char email_account_username[50];
char email_account_password[50];
char email_address[50];
char email_name[50];
boolean_type verify_cert_authorities;
boolean_type generate_curl_debug_messages;
char ca_path_and_filename[100];
char to_email_address[50];
char to_email_name[50];
char group_name[50];
char email_subject[50];
char email_message[50];
strcpy(smtp_server_address, "smtp.gmail.com");
strcpy(email_account_username, "mrsock@gmail.com");
strcpy(email_account_password, "liveandprosper");
strcpy(email_address, "mrsock@gmail.com");
strcpy(email_name, "Mr Sock");
verify_cert_authorities = TRUE_OR_YES;
generate_curl_debug_messages = TRUE_OR_YES;
strcpy(ca_path_and_filename,"curl-ca-bundle.crt");
// Register the PrintMessage callback function prior to InitializeToolkitEmail to
// process error messages that may be generated during CFDP initialization
RegisterMessage(&PrintTheMessage);
// Intializes the email and texting library using input parameters. This function creates the
// email thread.
if (InitializeToolkitEmail(smtp_server_address, email_account_username, email_account_password,
email_address, email_name, verify_cert_authorities, generate_curl_debug_messages) == SUCCESS)
{
// Sets an alternate path and/or file name for the Certificate Authorities(CA) file referenced by curl
// if curl has been configured to verify CAs. Curl's default CA file name is "curl-ca-bundle.crt".
if (SetCertAuthoritesPathAndFilename(ca_path_and_filename) == SUCCESS)
{
// Adds "to" address information to a list of email addresses and associate the email address with a
// group name.
strcpy(to_email_address, "captkirt@nasa.gov");
strcpy(to_email_name, "Captain Kirt");
strcpy(group_name, "dogs rule");
if (AddToEmailAddress(to_email_address, to_email_name, group_name) == SUCCESS)
{
// Add second email address and associate the email address with a group name.
strcpy(to_email_address, "drmccroy@nasa.gov");
strcpy(to_email_name, "Dr McCroy");
if (AddToEmailAddress(to_email_address, to_email_name, group_name) == SUCCESS)
{
// Create the email subject and message.
strcpy(email_subject, "Dogs and Cats");
strcpy(email_message, "Dogs are smarter than cats.");
// Sends the email to the designated email addresses associated with the group name.
if (SendEmail(email_subject, email_message, group_name) != SUCCESS)
{
printf("Error Failed to SendEmail.\n");
return 0;
}
}
else
{
printf("Error Failed to AddToEmailAddress.\n");
return 0;
}
}
else
{
printf("Error Failed to AddToEmailAddress.\n");
return 0;
}
}
else
{
printf("Error Failed to SetCertAuthoritesPathAndFilename.\n");
return 0;
}
}
else
{
printf("Error Failed to InitializeToolkitEmail.\n");
return 0;
}
// Sleep 10 second prior to exiting.
#ifdef __linux__
sleep(10);
#else
Sleep(10000);
#endif
char config_pathname[256];
char home_path[256];
if (TCAACGetHomeDirectory(home_path, sizeof(home_path)) != SUCCESS)
{
return(FAIL);
}
sprintf(config_pathname, "%s/email_config.txt", home_path);
// Save the configuration in a file
SaveToolkitEmailConfig(config_pathname);
// Now send an email using the configuration file
if (InitToolkitEmail(config_pathname, "liveandprosper") != SUCCESS)
{
printf("Error Failed to InitToolkitEmail.\n");
}
// Create the email subject and message.
strcpy(email_subject, "Dogs and Cats");
strcpy(email_message, "Dogs are smarter than cats.");
// Sends the email to the designated email addresses associated with the group name.
if (SendEmail(email_subject, email_message, group_name) != SUCCESS)
{
printf("Error Failed to SendEmail.\n");
return 0;
}
// Sleep 10 second prior to exiting.
#ifdef __linux__
sleep(10);
#else
Sleep(10000);
#endif
return 0;
}
Message codes and structure definition for the Device Service library.
boolean_type
Boolean enumeration.
Definition: ds_shared.h:104
@ TRUE_OR_YES
True.
Definition: ds_shared.h:106
Structure of parameters needed for message support.
Definition: trek_toolkit_common_api_ansi_c.h:74
char message[MAX_MESSAGE_SIZE]
Message.
Definition: trek_toolkit_common_api_ansi_c.h:77
enum message_category category
Message category (e.g., MSG_CAT_ERROR, MSG_CAT_ERROR_ALERT, MSG_CAT_WARNING, MSG_CAT_WARNING_ALERT,...
Definition: trek_toolkit_common_api_ansi_c.h:76
An ANSI C Data Service API.
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION DSCleanUp()
Initiates a graceful shutdown of the Device Service library and all supporting device libraries,...
Definition: toolkit_ds_api_ansi_c.cpp:3843
int EXPORT_THIS_TOOLKIT_DS_C_FUNCTION RegisterMessage(void(*function_ptr)(message_struct_type *message_struct_ptr))
Register a callback function to receive and process messages issued by the DS library.
Definition: toolkit_ds_api_ansi_c.cpp:3951
An ANSI C email device API.
int EXPORT_THIS_TOOLKIT_EMAIL_DEVICE_C_FUNCTION InitToolkitEmail(const char *config_pathname, const char *email_account_password)
Intializes the email and texting library using parameters read from a configuration file.
Definition: toolkit_email_device_api_ansi_c.cpp:72
int EXPORT_THIS_TOOLKIT_EMAIL_DEVICE_C_FUNCTION SetCertAuthoritesPathAndFilename(const char *ca_path_and_filename)
Sets an alternate path and file name for the Certificate Authorities (CA) file referenced by curl if ...
Definition: toolkit_email_device_api_ansi_c.cpp:377
int EXPORT_THIS_TOOLKIT_EMAIL_DEVICE_C_FUNCTION SendEmail(const char *subject, const char *message, const char *email_address_or_group_name)
Sends an email to the designated email address or group name of recipients. The maximum length of an ...
Definition: toolkit_email_device_api_ansi_c.cpp:1163
int EXPORT_THIS_TOOLKIT_EMAIL_DEVICE_C_FUNCTION InitializeToolkitEmail(const char *smtp_server_address, const char *email_account_username, const char *email_account_password, const char *email_address, const char *email_name, boolean_type verify_cert_authorities, boolean_type generate_curl_debug_messages)
Intializes the email and texting library using input parameters.
Definition: toolkit_email_device_api_ansi_c.cpp:256
int EXPORT_THIS_TOOLKIT_EMAIL_DEVICE_C_FUNCTION SaveToolkitEmailConfig(const char *config_pathname)
Saves the email and texting configuration parameters in a text file.
Definition: toolkit_email_device_api_ansi_c.cpp:1458
int EXPORT_THIS_TOOLKIT_EMAIL_DEVICE_C_FUNCTION AddToEmailAddress(const char *to_email_address, const char *to_email_name, const char *to_group_name)
Adds "to" address information to a list of email addresses and associates the email address with a gr...
Definition: toolkit_email_device_api_ansi_c.cpp:436
Command codes for TReK.
#define SUCCESS
The function completed successfully.
Definition: trek_error.h:8
#define FAIL
The function failed for an unknown reason.
Definition: trek_error.h:9
The commonly shared macros, structures and functions.
int EXPORT_THIS_TREK_TOOLKIT_COMMON_API_FUNCTION TCAACGetHomeDirectory(char *directory_buffer, int buffer_size)
Gets the path to the users home directory.
Definition: trek_toolkit_common_api_ansi_c.cpp:142
const char EXPORT_THIS_TREK_TOOLKIT_COMMON_API_FUNCTION * GetMessageCategoryAsPaddedString(enum message_category input)
Converts an enumerated message category value into its equivalent character string with padded spaces...
Definition: trek_toolkit_common_api_ansi_c.cpp:90