Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please include or provide with a link of whatever files are required. Full point

ID: 3557545 • Letter: P

Question

Please include or provide with a link of whatever files are required. Full points will be given if the program runs.

You are given a file named data.txt. This file contains first name followed by last name.

Example:

Lisa Smith

Your program should get this data and make an e-mail like

Smith_Lisa@student.smc.edu

And place them in output file named email.txt

Note: the email is last name followed by underscore and first name.

Please include or provide with a link of whatever files are required. Full points will be given if the program runs. Your task as a programmer is to create a menu driven program.

Explanation / Answer

C Program for Email Menu

#include <stdio.h> /* para funcoes como printf, scanf, etc */
#include <stdlib.h> /* para a funcao exit */
#include <string.h>

typedef struct mail
{
char from[120];
char to[120];
char sub[120];
char msg[120];
} MAIL;

void comandom(MAIL *m);
//void comandoC(MAIL *m);
void comandoL(MAIL *m);

int help(){

printf ("Help ");
printf ("Mail Commands ");
// add rest of help text here
return 0;
}

int main (){
char c;
char input[BUFSIZ];
MAIL mailmessage;

fopen("mail.txt","r");

printf("E-mail program. ");


do{
putchar ('&');
fgets( input, BUFSIZ, stdin );
c = input[0];

switch (c)
{
case '?':help();break;
case 't':printf("ok ");break;
case 'd':printf("ok ");break;
case 'R':printf("ok ");break;
case 'r':printf("ok ");break;
case 'm':comandom(&mailmessage);break;
case 'q':printf("ok ");break;
case 'x':printf("Program shutting down! "); break;
case 'h':printf("ok ");break;
case 'C'://comandoC(&mailmessage);break;
case 'L':comandoL(&mailmessage);break;
default: printf("Unknown command! ");break;
}

}while (c!='x');

return 0;
}

void comandom(MAIL *ptr)
{
printf("From: "); fgets(ptr->from,sizeof ptr->from, stdin);
printf("To: "); fgets(ptr->to,sizeof ptr->to, stdin);
printf("Subject: "); fgets(ptr->sub,sizeof ptr->sub, stdin);
printf("Message: "); fgets(ptr->msg,sizeof ptr->msg, stdin);
}

void comandoL(MAIL *ptr)
{
printf("From: %s ",ptr->from);
printf("To: %s ",ptr->to);
printf("Subject:%s ",ptr->sub);
printf("Message:%s ",ptr->msg);
}

C Program To send Email using Linux


#include <stdio.h>
#include <string.h>
#include <errno.h>
int sendmail(const char *to, const char *from, const char *subject, const char *message)
{
int retval = -1;
FILE *mailpipe = popen("/usr/lib/sendmail -t", "w");
if (mailpipe != NULL) {
fprintf(mailpipe, "To: %s ", to);
fprintf(mailpipe, "From: %s ", from);
fprintf(mailpipe, "Subject: %s ", subject);
fwrite(message, 1, strlen(message), mailpipe);
fwrite(". ", 1, 2, mailpipe);
pclose(mailpipe);
retval = 0;
}
else {
perror("Failed to invoke sendmail");
}
return retval;
}

main(int argc, char** argv)
{
int i;

printf("argc = %d ", argc);

for (i = 0; i < argc; i++)
printf("argv[%d] = "%s" ", i, argv[i]);
sendmail(argv[1], argv[2], argv[3], argv[4]);
}