Reputation: 13286
I am using the following dictionary to create a MIME header, to send an email that attaches a text file. It produces the email below the code. Why is everything garbled? Any MIME experts out there?
Also, the answer is not as simple as you are Base64 encoding it. If I change it from "inline" to "attachment," the email attachment is corrupt when sent to web Gmail, but the file is ok and can be opened fine if it goes through GMail, but is opened by Mail on a Mac. Also, if the text file is very small, it works fine on GMail too.
Here is a previous formulation of this unanswered question. I have changed the code a bit in this example, to avoid what I think might be red herrings: GMail won't open .txt attachment created by iPhone App, Depending on Size - I think There's a Bug in my MIME
NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:
@"text/plain", kSKPSMTPPartContentTypeKey,
@"Hey Kevin,\nHere are some gps readings for you to filter.\nLove,\nTrailBehind", kSKPSMTPPartMessageKey,
@"8bit", kSKPSMTPPartContentTransferEncodingKey, nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *gpsFilePath = [[@"/tracks/" stringByAppendingString:[track title]] stringByAppendingString:@"/accelerometerReadings.txt"];
gpsFilePath = [[paths objectAtIndex:0] stringByAppendingString: gpsFilePath];
NSData *fileData = [NSData dataWithContentsOfFile:gpsFilePath];
NSDictionary *attached = [NSDictionary dictionaryWithObjectsAndKeys:
@"text/plain;", kSKPSMTPPartContentTypeKey,
@"inline;\r\tfilename=\"accelerometerReadings.txt\"", kSKPSMTPPartContentDispositionKey,
[fileData encodeBase64ForData], kSKPSMTPPartMessageKey,
@"base64", kSKPSMTPPartContentTransferEncodingKey, nil];
testMsg.parts = [NSArray arrayWithObjects:plainPart, attached, nil];
[testMsg send];
THE EMAIL
--SKPSMTPMessage--Separator--Delimiter
Content-Type: text/plain
Content-Transfer-Encoding: 8bit
Hey Kevin,
Here are some gps readings for you to filter.
Love,
TrailBehind
--SKPSMTPMessage--Separator--Delimiter
Content-Disposition: attachment; filename="track.gpx"
Content-Type: text/plain;
Content-Transfer-Encoding: base64
PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz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gICA8dHJrcHQgbGF0PSIzOS4zMTYyMzkiIGxvbj0iLTEyMC4xNjc2MjEiPjwvdHJrcHQ+CgogICAgICA8dHJrcHQgbGF0PSIzOS4zMTYyMzkiIGxvbj0iLTEyMC4xNjc2MjEiPjwvdHJrcHQ+CgogICAgICA8dHJrcHQgbGF0PSIzOS4zMTYyMTQiIGxvbj0iLTEyMC4xNjc1MDkiPjwvdHJrcHQ+CgogICAgICA8dHJrcHQgbGF0PSIzOS4zMTYyMDgiIGxvbj0iLTEyMC4xNjczNjgiPjwvdHJrcHQ+CgogICAgICA8dHJrcHQgbGF0PSIzOS4zMTYyMTUiIGxvbj0iLTEyMC4xNjcyOTciPjwvdHJrcHQ+CgogICAgICA8dHJrcHQgbGF0PSIzOS4zMTYyMTUiIGxvbj0iLTEyMC4xNjcyOTciPjwvdHJrcHQ+CgogICAgICA8dHJrcHQgbGF0PSIzOS4zMTYxOTQiIGxvbj0iLTEyMC4xNjcxODEiPjwvdHJrcHQ+CgogICAgICA8dHJrcHQgbGF0PSIzOS4zMTYwNzciIGxvbj0iLTEyMC4xNjcyNTMiPjwvdHJrcHQ+CgogICAgICA8dHJrcHQgbGF0PSIzOS4zMTYwMzIiIGxvbj0iLTEyMC4xNjcyMzQiPjwvdHJrcHQ+CgogICAgICA8dHJrcHQgbGF0PSIzOS4zMTU5ODMiIGxvbj0iLTEyMC4xNjcxOTEiPjwvdHJrcHQ+CgogICAgPC90cmtzZWc+CiAgPC90cms+CjwvZ3B4Pgo=
--SKPSMTPMessage--Separator--Delimiter
--SKPSMTPMessage--Separator--Delimiter--
Upvotes: 3
Views: 583
Reputation: 51197
If that's what your Base64 output looks like, its wrong; it should be wrapped to 76-character lines for email. Also, it looks like something added spaces (probably due to it not being wrapped)
Also, are you setting content-transfer-encoding? I see the lines setting the content-type and the content-disposition, but without the content-transfer-encoding, you'll get the default (7bit)... which will just show the encoded MIME. Which is what's happening.
I suggest checking your raw message output (e.g., "show original" in gmail) against RFC 2045 §6 and §6.8 in particular.
If this doesn't solve your question, please edit to include the raw message.
Upvotes: 2