PDA

View Full Version : Sending email from my program (quick question)




geoldr
Apr 12, 2011, 01:23 AM
Hey guys, I am having difficulty opening the Mail app and sending a message. I have it to the point where it opens the app, opens the new message dialog, puts in the Subject, and the Body, but when there is a space in the body, it doesnt do anything. Here is code:


NSString *emailMail = @"mailto:?subject=mySubject&body=";
NSString *emailURL = [testU stringValue];
NSString *emailSIG = @"Sig";
NSString *URLFinal = [NSString stringWithFormat:@"%@%@%@", emailMail, emailURL, emailSIG];
NSLog(@"%@",URLFinal);
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:george1]];


This will work without a problem. But say I change:


NSString *URLFinal = [NSString stringWithFormat:@"%@%@%@", emailMail, emailURL, emailSIG];

to this:

NSString *URLFinal = [NSString stringWithFormat:@"%@%@\n%@", emailMail, emailURL, emailSIG];


It will not work. Is there any way to do this ?

I was thinking maybe there is a way to do this with AppleScript and have it not even open the mail program, but I am not sure how to do this.



jiminaus
Apr 12, 2011, 03:47 AM
You're forming a URL, so the URL needs be properly encoded. For example, a space needs to be encoded as %20. But don't do this manually, use NSString's stringByAddingPercentEscapesUsingEncoding:

geoldr
Apr 12, 2011, 07:41 PM
I was able to find this page:

http://www.ianr.unl.edu/internet/mailto.html

And use the code: %0A%0A to make a few line breaks to add the signature!

I read somehwere that stringByAddingPercentEscapesUsingEncoding is bugged right now. I'm not sure if thats true or not

kainjow
Apr 12, 2011, 07:51 PM
I read somehwere that stringByAddingPercentEscapesUsingEncoding is bugged right now. I'm not sure if thats true or not

Link?

geoldr
Apr 12, 2011, 07:54 PM
Link?

I googled the function and clicked the first link and they directed me to this page:

http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/

I have no idea if this is true or not, I have not tried it yet.

EDIT:
I just tried it, and it does work.

jiminaus
Apr 12, 2011, 09:30 PM
The blog post was from 2 years ago!!!

geoldr
Apr 13, 2011, 11:54 AM
The blog post was from 2 years ago!!!

Right, but judging by the comments, since there was one from yesterday, and January 2011, I figured there is still a bug.

And actually, this code did work for me. Because using the default apple code it wouldn't encode the & in URLs and it would mess up my program. But using the code from the blog post it works!