Sending email from my program (quick question)

Discussion in 'Mac Programming' started by geoldr, Apr 11, 2011.

  1. macrumors newbie

    Joined:
    Apr 1, 2011
    #1
    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:

    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:

    Code:
    NSString *URLFinal = [NSString stringWithFormat:@"%@%@%@", emailMail, emailURL, emailSIG];
    
    to this:
    Code:
    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.
     
  2. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    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:
     
  3. thread starter macrumors newbie

    Joined:
    Apr 1, 2011
    #3
    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
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Link?
     
  5. thread starter macrumors newbie

    Joined:
    Apr 1, 2011
    #5
  6. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #6
    The blog post was from 2 years ago!!!
     
  7. geoldr, Apr 13, 2011
    Last edited: Apr 13, 2011

    thread starter macrumors newbie

    Joined:
    Apr 1, 2011
    #7
    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!
     

Share This Page