I’m developping an app where I send mail without the IOS mail API(the mail is sender with SES amazon service without the interaction of the user and attaching files…). In my API I have to send the encoded mail with all the MIME protocol(that’s not the problem). I send well mails encoding the body with charset ascii and the pdf I attach in base64:
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
NSString *uuid = (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidRef);
CFRelease(uuidRef);
//Encapsulation
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableString *rawMime = [[NSMutableString alloc] init];
[rawMime appendFormat:@"To: %@\n", _to];
[rawMime appendFormat:@"From: \"%@\" <boutique@tactill.com>\n", [defaults objectForKey:@"companyName"]];
[rawMime appendFormat:@"Reply-To: %@\n", [defaults objectForKey:@"toEmailForStats"]];
[rawMime appendFormat:@"BCC: %@\n", [defaults objectForKey:@"toEmailForStats"]];
[rawMime appendFormat:@"Subject: %@\n", _subject];
[rawMime appendString:@"Date: Thu, 05 Jan 95 10:53:24 -0500\n"];
[rawMime appendFormat:@"Message-ID: <%@@%@>\n", [(NSString *)uuid stringByReplacingOccurrencesOfString:@"-" withString:@""], @"IETF.CNR I.Reston.VA.US"];
[rawMime appendString:@"Mime-Version: 1.0\n"];
[rawMime appendString:@"Content-type: Multipart/Mixed; boundary=\"NextPart\"\n"];
[rawMime appendString:@"\n"];
[rawMime appendString:@"--NextPart\n"];
//Body
[rawMime appendString:@"Content-type: text/plain; charset=\"us-ascii\"\n"];
[rawMime appendString:@"\n"];
[rawMime appendString:_body];
[rawMime appendString:@"\n"];
[rawMime appendString:@"--NextPart\n"];
//Attach pdf
[rawMime appendString:@"Content-Type: application/pdf; "];
[rawMime appendFormat:@"name=\"%@.pdf\";\n", _pdfName];
[rawMime appendFormat:@"Content-Transfer-Encoding: base64\r\n\r\n"];
[rawMime appendFormat:@"%@\n",_pdfAttached];
[rawMime appendString:@"\n"];
//If necessary attach csv
if (_csvAttached!=nil) {
[rawMime appendString:@"--NextPart\n"];
[rawMime appendString:@"Content-Type: text/plain; "];
[rawMime appendFormat:@"name=\"%@.txt\";\n", _csvName];
[rawMime appendFormat:@"Content-Transfer-Encoding: base64\r\n\r\n"];
[rawMime appendFormat:@"%@\n",_csvAttached];
[rawMime appendString:@"\n"];
[rawMime appendString:@"--NextPart\n"];
}
NSData *rawMessageData = [rawMime dataUsingEncoding:NSUTF8StringEncoding];
When I open the mail with gmail or mail app(OSX or IOS) all is perfect but when I open it with windows mail the special characters are changed for junk. I have read about it and I have see that windows mail reads iso-8859-1(NSISOLatin1StringEncoding). So I have tried with the help of that post: [http://stackoverflow.com/questions/4553388/how-to-convert-utf8-encoding-to-iso-8859-1-encoding][1] do that code:
//add body
char converted[([_body length] + 1)];
[_body getCString:converted maxLength:([_body length] + 1) encoding: NSISOLatin1StringEncoding];
[rawMime appendString:@"Content-type: text/plain; charset=iso-8859-1\n"];
[rawMime appendFormat:@"Content-Transfer-Encoding: quoted-printable\r\n\r\n"];
[rawMime appendFormat:@"%s", converted];
[rawMime appendString:@"\n"];
[rawMime appendString:@"--NextPart\n"];
Now all the mail clients show me this:Merci pour votre achat. ¿ bientÙt! as do NSLog when I print only the body
What I’m doing wrong? how I can encode well the body of the mail?
Here’s a working code to answer this question that adds a pdf and occasionally a csv too
The _pdfAttached is NSString representing the PDF encoded in base64
I hope it helps and sorry to don’t post the solution before
Here’s the code not to send but to create the mail body. Here you could do things that you couldn’t do using SESSendRawEmailRequest, for example put a “show name” with at to and from. I suggest you to use the two parts to define the senders and u will see it offers possibilities that only SESSendRawMessage can’t offer. Finally say that you should not forget to put the to and from addresses at the SESSEndRawEmailRequest if you not want to have an exception.