Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-30-2009, 01:51 PM   #1 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 622
Question Format Email that Includes Text from String

I am trying to format an email message that includes text from a string. I am able to send an email that includes string values, or format an email that has static text. I cannot seem to format an email that includes string values.

I've gone through several threads, but haven't been able to work out a solution: Nutsmuggling: Sending Fomatted Email, include URL in email, compose mail. Code below. Assistance appreciated.

Code:
- (IBAction)mail:(id)sender {
	NSString *nameString = [UIDevice currentDevice].name;
	NSString *typeString = [UIDevice currentDevice].model;
	NSString *infoStrings = [NSString stringWithFormat: @"Device Name: %@, Device Type: %@", nameString, typeString];
    
	infoCombined.text = [NSString stringWithFormat: @"%@", infoStrings];
	
	NSString *subjString = @"Subject Text";
	NSString *messageString = [NSString stringWithFormat: @"%@", infoStrings];
	
	messageString = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)messageString, CFSTR(""), CFSTR(" %\"?=&+<>;:-"), kCFStringEncodingUTF8);
	
	NSString *urlString = [NSString stringWithFormat: @"mailto:?subject=%@&body=%@", 
						   [subjString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
						   messageString];
	
	NSURL *mailURL = [NSURL URLWithString: urlString];
	[[UIApplication sharedApplication] openURL: mailURL];
}
DenVog is offline   Reply With Quote
Old 04-30-2009, 02:36 PM   #2 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Looks to me like you're creating the same string about three times, then stepping on it when you're trying to add the URL escapes. Try this (not tested):

Code:
- (IBAction)mail:(id)sender {
	NSString *nameString = [UIDevice currentDevice].name;
	NSString *typeString = [UIDevice currentDevice].model;
	NSString *msgString = [NSString stringWithFormat: @"Device Name: %@, Device Type: %@", nameString, typeString];
	infoCombined.text = msgString;
	
	NSString *subjString = @"Subject Text";
		
	NSString *urlString = [NSString stringWithFormat: @"mailto:?subject=%@&body=%@", 
						   [subjString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
						   [msgString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
	NSLog(@"subjString = %@", subjString);
	NSLog(@"msgString = %@", msgString);
	NSLog(@"urlString = %@", urlString);

	NSURL *mailURL = [NSURL URLWithString: urlString];
	[[UIApplication sharedApplication] openURL: mailURL];
}
This still won't actually work, because you haven't specified a destination for the message when you create the urlString. But if it opens in MobileMail properly, it's easy to add that. If this doesn't work, post the console output.

joe
FlyingDiver is offline   Reply With Quote
Old 04-30-2009, 03:19 PM   #3 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 622
Default

Thanks for your reply.
Quote:
Originally Posted by FlyingDiver View Post
Looks to me like you're creating the same string about three times, then stepping on it when you're trying to add the URL escapes. Try this (not tested):
I should clarify that my code example works fine in terms of sending a mail with a long line of text. What it doesn't do is providing any formatting to make it easier to read (e.g. bold, line breaks or returns). Any formatting examples I have tried to apply to it don't like the string inputs.

Quote:
Originally Posted by FlyingDiver View Post
This still won't actually work, because you haven't specified a destination for the message when you create the urlString. But if it opens in MobileMail properly, it's easy to add that. If this doesn't work, post the console output.
I intentionally leave the destination blank. I want the user to input that.
DenVog is offline   Reply With Quote
Old 04-30-2009, 03:37 PM   #4 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Quote:
Originally Posted by DenVog View Post
Thanks for your reply.

I should clarify that my code example works fine in terms of sending a mail with a long line of text. What it doesn't do is providing any formatting to make it easier to read (e.g. bold, line breaks or returns). Any formatting examples I have tried to apply to it don't like the string inputs.
Ah. That wasn't clear. You should be able to get line breaks into the output by putting in carriage returns ('\r') where you want them, before its' URL encoded. If you want anything else, you're going to have to get a LOT more complicated. It'll have to be either MIME or HTML, and you're going to need to put in all the headers to declare a file type for the message body. Have you tried that?

joe
FlyingDiver is offline   Reply With Quote
Old 04-30-2009, 04:23 PM   #5 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 622
Default

Hi Joe. Thanks for the follow-up.
Quote:
Originally Posted by FlyingDiver View Post
You should be able to get line breaks into the output by putting in carriage returns ('\r') where you want them, before its' URL encoded.
Hmm. The carriage return is a great idea, but I am not getting it to work.
Code:
NSString *infoStrings = [NSString stringWithFormat: @"Device Name: %@\rDevice Type: %@\r", nameString, typeString];
I wonder if the "\r" is getting stripped out when it does the string encoding for the URL?
Quote:
Originally Posted by FlyingDiver View Post
If you want anything else, you're going to have to get a LOT more complicated. It'll have to be either MIME or HTML, and you're going to need to put in all the headers to declare a file type for the message body. Have you tried that?
I did start down the HTML path, as this Nutsmuggling thread made it look easy. Which it probably is if you're just formatting static text. I can't find a way to insert a string from a variable within the HTML code though.
DenVog is offline   Reply With Quote
Old 04-30-2009, 04:39 PM   #6 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Quote:
Originally Posted by DenVog View Post
Hi Joe. Thanks for the follow-up.
Hmm. The carriage return is a great idea, but I am not getting it to work.
Code:
NSString *infoStrings = [NSString stringWithFormat: @"Device Name: %@\rDevice Type: %@\r", nameString, typeString];
I wonder if the "\r" is getting stripped out when it does the string encoding for the URL?
Print out that string before and after the URL encoding and see what you have.

joe
FlyingDiver is offline   Reply With Quote
Old 04-30-2009, 04:43 PM   #7 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Quote:
Originally Posted by DenVog View Post
I did start down the HTML path, as this Nutsmuggling thread made it look easy. Which it probably is if you're just formatting static text. I can't find a way to insert a string from a variable within the HTML code though.
Hmm. The posted code is:

Code:
NSString *eMailBody = @"<table>
<tr><td style='text-align:right'><b>Name</b>:</td>
<td>John</td></tr><tr>
<td style='text-align:right'><b>Surname</b>:</td><td>Doe</td></tr>
<tr><td style='text-align:right'>
 <b>Occupation:<b/></td><td>Placeholder</td></tr></table>";

NSString *encodedBody = [eMailBody stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSString *urlString = [NSString stringWithFormat:@"mailto:me@me.com?subject=HiPhone&body=%@", encodedBody];
NSURL *url = [[NSURL alloc] initWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
So what happens when you try something like:

Code:
NSString *eMailBodyTemplate = @"<table>
<tr><td style='text-align:right'><b>Name</b>:</td>
<td>%@</td></tr><tr>
<td style='text-align:right'><b>Surname</b>:</td><td>%@</td></tr>
<tr><td style='text-align:right'>
 <b>Occupation:<b/></td><td>%@</td></tr></table>";

NSString *eMailBody = [NSString StringWithFormat: eMailBodyTemplate, @"First Name", @"Last Name", @"Profession"];
NSString *encodedBody = [eMailBody stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSString *urlString = [NSString stringWithFormat:@"mailto:me@me.com?subject=HiPhone&body=%@", encodedBody];
NSURL *url = [[NSURL alloc] initWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
You could substitute in your own string pointers in place of @"First Name", @"Last Name", @"Profession".

jow
FlyingDiver is offline   Reply With Quote
Old 04-30-2009, 07:57 PM   #8 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 622
Default

Hi Joe. Thanks again for your suggestions.
Quote:
Originally Posted by FlyingDiver View Post
Print out that string before and after the URL encoding and see what you have.
This is really confusing. So when I do a NSLog on the final string which is the URL that triggers the email, the "%0D" which is a carriage return shows up.
Code:
2009-04-30 16:43:33.722 About Device[569:20b] mailto:?subject=My%20Subject%20&body=Device%20Name%3A%20Dennis%E2%80%99s%20iPhone%0DDevice%20Type%3A%20iPhone%0D
But it doesn't display that way in the Mail app on the iPhone, or the email that gets received. Just looks like a run-on text message.
DenVog is offline   Reply With Quote
Old 04-30-2009, 08:45 PM   #9 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Quote:
Originally Posted by DenVog View Post
Hi Joe. Thanks again for your suggestions.
This is really confusing. So when I do a NSLog on the final string which is the URL that triggers the email, the "%0D" which is a carriage return shows up.
Code:
2009-04-30 16:43:33.722 About Device[569:20b] mailto:?subject=My%20Subject%20&body=Device%20Name%3A%20Dennis%E2%80%99s%20iPhone%0DDevice%20Type%3A%20iPhone%0D
But it doesn't display that way in the Mail app on the iPhone, or the email that gets received. Just looks like a run-on text message.
Try '\n' for newline, or '\r\n' for the pair. I forget which is required in the mail spec.

joe
FlyingDiver is offline   Reply With Quote
Old 05-03-2009, 10:35 AM   #10 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 622
Default

Thanks Joe.
Quote:
Originally Posted by FlyingDiver View Post
So what happens when you try something like:

You could substitute in your own string pointers in place of @"First Name", @"Last Name", @"Profession".
I think that got it. I did end up combining a couple lines into one. The result is
Code:
NSString *eMailBody = [NSString stringWithFormat: @"<table><tr><td style='text-align:right'><b>Name</b>:</td><td>%@</td></tr><tr><td style='text-align:right'><b>Type</b>:</td><td>%@</td></tr></table>", nameString, typeString];
For anyone playing along at home, there are two adjustments you will want to make to the code.
  1. Remove the carriage returns from the HTML table code
  2. Need a lowercase "S" on stringWithFormat

I appreciate your help and patience Joe. I will roll these improvements into my free About Device App. It throws all the device info into an email, so hopefully it will benefit lots of folks in the community (i.e. those collecting info from ad hoc/beta testers).
DenVog is offline   Reply With Quote
Old 07-29-2010, 09:11 AM   #11 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 2
Default HELP HERE!!

Maybe someone is able to help me.

I am trying to do the same. attach string to an email from iphone app.

The part of facebook and twitter is already working fine...but i havent been able to do it in the part of message.

thanks for your help! i really appreciate it...

I attach first the email part, and then bellow the twitter code...(maybe it is usefull for you).

email part:
-----------
-(IBAction)send:(id)sender
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Check this!"];


// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"Default"];


// Fill out the email body text
NSString *emailBody = [NSString stringWithString:[webView stringByEvaluatingJavaScriptFromString:@"document. documentElement.innerHTML"]];

[picker setMessageBody:emailBody isHTML:NO];

[self presentModalViewController:picker animated:YES];
[picker release];
}
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewCont roller*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
webView.hidden = NO;

[self dismissModalViewControllerAnimated:YES];
}

----------


twitter part:

-(IBAction)twitStatus:(id)sender
{
NSString *content=[NSString stringWithString:[webView stringByEvaluatingJavaScriptFromString:@"document. documentElement.innerHTML"]];
NSRange data=[content rangeOfString:@"</font></center>"];
NSString *author=[content substringToIndex:data.location];
data=[author rangeOfString:@"<br></i>"];
author=[author substringFromIndex:data.location+8];

NSRange data2=[content rangeOfString:@"<br></i>"];
NSString *phrase = [content substringToIndex:data2.location];
data2=[phrase rangeOfString:@"<i><br>"];
phrase=[phrase substringFromIndex:data2.location+7];

phrase=[NSString stringWithFormat:@"%@ - %@",phrase,author ];
[[TwitterAgent defaultAgent] twit:phrase];
}
-----
camiech is offline   Reply With Quote
Reply

Bookmarks

Tags
email, format, string

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 245
23 members and 222 guests
ADY, AragornSG, bookesp, chillyh, dacapo, Dani77, Davey555, Dominus, dre, glenn_sayers, HemiMG, JasonR, karlam963, M.A.S., marshusensei, mer10, nobre84, Oral B, prchn4christ, Raggou, Rudy, spiderguy84, themathminister
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,765
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:25 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0