I am implementing a functionality where I have to send emails through code and have to read the email subject and body from a text file.
This is how my text file looks like. The # switch indicates start of the message body.
Code:
Product Notification Reminder
#
Hello1,
This is a reminder that the attached product notification tasks assigned to you are still open.
This mail has been automatically generated. Please direct further enquiries to the owner of the tasks.
Kind Regards,
Admin
I have implemented the code as
Code:
void get_envelope_subject_body(static std::string &strEnvSubject, static std::string &strEnvBody)
{
FILE *fpEnvelopeConfig = NULL;
char cBuffer[100];
char *pcMessage = NULL;
char *pcSubject = NULL;
int iCount = 0;
bool isSubject = true;
if ((fpEnvelopeConfig = fopen("D:\\Configfile.txt", "r")) == NULL)
{
printf("Cannot open the envelope configuration file.");
}
else
{
pcSubject = (char*)malloc(sizeof(cBuffer));
pcSubject[0] = '\0';
while (fgets(cBuffer, sizeof(cBuffer), fpEnvelopeConfig) != NULL)
{
if (isSubject == true && cBuffer[0] != '#')
{
strcat(pcSubject, cBuffer);
}
//end of subject, start of body
if (cBuffer[0] == '#')
{
isSubject = false;
}
if (isSubject == false && cBuffer[0] != '#')
{
iCount++;
pcMessage = (char*)realloc(pcMessage, iCount *sizeof(cBuffer));
if (iCount == 1)
{
pcMessage[0] = '\0';
}
strcat(pcMessage, cBuffer);
}
}
}
strEnvSubject = pcSubject;
strEnvBody = pcMessage;
free(pcMessage);
free(pcSubject);
}
I am sure there is a better way to write this code and optimize it further so please let me know your comments/suggestions.
Also, the code will be run in Unix whereas I am developing it in windows. So, is the code portable?