C script Implementing a MIME Multipart/Mixed sends mail with empty body and empty attachment

127 Views Asked by At

I am writing a simple C script to send a Multipart/mixed MIME message , which should send mail body along with attachment.

When I execute the below script , the email comes with an attachment , but bot body and attachment are blank .

Probably I am doing something wrong with the placement of at end of each command. I have tried different permutations combinations of using CRLFS . But it doesn't help . There is something wrong with the way the ----BOUNDARY is being perceived. If I remove the CRLF at end of first ----BOUNDARY , The first part of the message is delivered along with MIME header and body of the 2nd part as below.

HELLO TESTING INPUT --> this is the body in first part

----BOUNDARYCONTENT-TYPE: APPLICATION/OCTET-STREAM; NAME="TESTING.TXT" CONTENT-TRANSFER-ENCODING: 7BIT

hello test data ---> this should have been in attachment but comes in body along with headers

I have looked up the RFC MIME rules and appending a CRLF at end of each message line appears to be correct.

Below is the snippet

sendCommand(s,"subject: this is test \r\n");



sendCommand(s,"mime-version: 1.0\r\n");
  strcpy(cmd,"content-type:MULTIPART/MIXED; BOUNDARY=\"--BOUNDARY\"\r\n");
 
  sendCommand(s,cmd);
  
  strcpy(cmd, "                                            \r\n");
  sendCommand(s,cmd);
  strcpy(cmd,"----BOUNDARY\r\n");
  sendCommand(s,cmd);
  strcpy(cmd,"CONTENT-TYPE: TEXT/HTML; CHARSET=\"US-ASCII\"\r\n");
  
  sendCommand(s,cmd);
  sendCommand(s,"CONTENT-TRANSFER-ENCODING: 7BIT\r\n");
  strcpy(cmd,"                                             \r\n"); 
  sendCommand(s,cmd);
  
  sendCommand(s,"HELLO TESTING INPUT \r\n");
  strcpy(cmd,"                                              \r\n"); 
  sendCommand(s,cmd);
  
  strcpy(cmd,"----BOUNDARY\r\n");
  sendCommand(s,cmd);
  
  strcpy(cmd,"CONTENT-TYPE: APPLICATION/OCTET-STREAM; NAME=\"TESTING.TXT\"\r\n");
  sendCommand(s,cmd);
  sendCommand(s,"CONTENT-TRANSFER-ENCODING: 7BIT\r\n");
  strcpy(cmd,"                                              \r\n"); 
  sendCommand(s,cmd);
  strcpy(cmd,"hello test data \r\n");
  sendCommand(s,cmd);
  
  strcpy(cmd,"                                               \r\n"); 
  sendCommand(s,cmd);
  strcpy(cmd,"----BOUNDARY--");
  
    
    
    strcpy(cmd,"\r\n.\r\n");                                              // Send <CRLF>.<CRLF> to 
    
    sendCommand(s,cmd);
    getResponse(s,buf);
                       
    checkResponse(buf,'2',&input->RC,input->ERRMSG);                     //check for 2xx response
    if (input->RC >0) return (05);
    
    reccnt = reccnt +1;
    strcpy(cmd ,"QUIT\r\n"); 

What can I try next?

1

There are 1 best solutions below

0
alphabet On

I was able to resolve this. Apparently what I was doing wrong was my interpretation of the Encapsulation boundary. The encapsulation boundary begins with CRLF and ends once trailing CRLF is placed after the message headers. This implies the after the end of last message header aka the CONTENT- statement , I needed to put two CRLF instead of one. Changing the code as below made it work

strcpy(cmd,"\r\n----BOUNDARY\r\n");
sendCommand(s,cmd);
strcpy(cmd,"CONTENT-TYPE: TEXT/HTML; CHARSET=\"US-ASCII\"\r\n"); 
sendCommand(s,cmd);
sendCommand(s,"CONTENT-TRANSFER-ENCODING: 7BIT\r\n\r\n");