discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Sending MIME Mails


From: Andreas Höschler
Subject: Sending MIME Mails
Date: Sat, 25 Aug 2007 00:08:01 +0200

Hi all,

I used to send MIME mails with attachments as follows:

#define SENDMAIL "/usr/lib/sendmail -t"

+ (void)sendMailFrom:(NSString *)from to:(NSString *)to subject:(NSString *)subject body:(NSString *)body attachments:(NSArray *)attachments
{
   FILE *out = popen(SENDMAIL, "w");
   fprintf(out, "From: %s\n", [from cString]);
   fprintf(out, "To: %s\n", [to cString]);
   fprintf(out, "Subject: %s\n", [subject cString]);

   if ([attachments count]) // MIME message
     {
      NSEnumerator *enumerator = [attachments objectEnumerator];
      NSDictionary *dic;
NSString *mime_boundary = @"-1747901728-1448367683-913849620=:4553";

      fprintf(out, "MIME-Version: 1.0\n");
fprintf(out, "Content-Type: multipart/mixed; BOUNDARY=\"%s\"\n", [mime_boundary cString]);
      fprintf(out, "\n");

      // write dummy message body
fprintf(out," This message is in MIME format. The first part should be readable text,\n"); fprintf(out," while the remaining parts are likely unreadable without MIME-aware tools.\n"); fprintf(out," Send mail to mime@docserver.cac.washington.edu for more info.\n");
      fprintf(out,"\n");

      // write message text
      fprintf(out, "--%s\n", [mime_boundary cString]);
      fprintf(out, "Content-Type: text/plain; charset=US-ASCII\n");
      fprintf(out,"\n");
      fprintf(out, [body cString]);
      fprintf(out,"\n");

      while (dic = [enumerator nextObject])
        {
         NSString *filename = [dic objectForKey:@"filename"];
         NSData *data = [dic objectForKey:@"data"];
         NSData *encodedData = [GSMimeDocument encodeBase64:data];

         fprintf(out,"--%s\n", [mime_boundary cString]);
fprintf(out,"Content-Type: %s; name=\"%s\"\n", [[self guessMIMEType:filename] cString], [[self basename:filename] cString]);
         fprintf(out,"Content-Transfer-Encoding: base64\n");
         fprintf(out,"Content-Description:\n");
         fprintf(out,"\n");

         // mime kodierte Daten des attachments ausgeben
         if (encodedData)
           {
            int i, length = [encodedData length];
            const char *pChar = [encodedData bytes];
            for (i = 0 ; i < length ; i++)
              {
               putc(*pChar, out);
               pChar++;
              }
            fprintf(out,"\r\n");
           }
        }

      fprintf(out, "--%s--\n", [mime_boundary cString]);
     }
   else
     {
      fprintf(out, "\n");
      fprintf(out, [body cString]);
     }
   pclose(out);
}

If I remember correctly this worked with an older version of GNUstep (but I could also be wrong here). I just tried that with the latest GNUstep release. The mail is sent (of course) but the attachment does not get through correctly. PDF or TIFFs or whatever cannot be opened on the other end. It seems that Mail.app does not do the back conversion from base64 to binary format. What am I missing?

Thanks a lot!

Regards,

  Andreas





reply via email to

[Prev in Thread] Current Thread [Next in Thread]