views:

1787

answers:

3

I want to create a link on a UIWebView that emails content to the user. A simple example is:

<a href="mailto:[email protected]?subject=Sarcasm&body=I »
<b>love</b> &lt;html&gt; mail!">Hi!</a>

Which creates a message that looks like this:

-- begin message ---

To: [email protected] Subject: Sarcasm

I love mail!

-- end message --

I need something more elaborate. The subject will contain multiple words with spaces. The body will contain HTML, list (<ul>) and hyperlinks with quotes in their href. How do I create something like that?

Here's an example:

subject= "This is only a test"

body= "This is the body part. Here's a list of links:
<ul>
<li><a href="http://www.abc.com">abc.com&lt;/a&gt;&lt;/li&gt;
<li><a href="http://www.xyz.com">xyz.com&lt;/a&gt;&lt;/li&gt;
</ul>
The end."

Also, why does the simulator do anything when clicking a mailto link?

+2  A: 

The simulator doesn't have Mail.app, as you can see from the home screen of it, so it has nothing to open when it encounters a mailto link.

As far as I know though, there is no way to use mailto: to send an html formatted email.

Seventoes
Download the Fortune Cookie app, they do it.
Garrett
I didn't see this in the example apps. Where do I find it?
4thSpace
This works: http://davidebenini.it/2008/12/19/iphone-sdk-sending-formatted-email/
4thSpace
+3  A: 

Fields are URL-encoded (in Cocoa you can use stringByAddingPercentEscapesUsingEncoding: for that).

4thspace mentioned that Mail.app does allow HTML. This is against the mailto RFC2368, which clearly says that body is supposed to be text/plain.

porneL
+1  A: 

You will need to the MessageUI.framework reference to your project.

Add the following to your .h file

#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

Add the delegate <MFMailComposeViewControllerDelegate>

Create a couple methods similar to the following in your .m file.

-(IBAction)checkCanSendMail:(id)sender{
        Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
        if (mailClass != nil) {
            if ([mailClass canSendMail]) {
                [self displayComposerSheet];
            }
            else {
                //Display alert for not compatible. Need iPhone OS 3.0 or greater. Or implement alternative method of sending email.
            }
        }
        else {
            //Display alert for not compatible. Need iPhone OS 3.0 or greater.  Or implement alternative method of sending email.
        }
    }

    -(void)displayComposerSheet {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
        mailer.mailComposeDelegate = self;

        [mailer setSubject:@"Email Subject"];

        //Set our to address, cc and bcc
        NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"];
        //NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]",@"[email protected]",nil];
        //NSArray *bccRecipients = [NSArray arrayWithObjects:@"[email protected]",@"[email protected]",nil];

        [mailer setToRecipients:toRecipients];
        //[mailer setCcRecipients:ccRecipients];
        //[mailer setBccRecipients:bccRecipients];

        NSString *emailBody = @"\
        <html><head>\
        </head><body>\
        This is some HTML text\
        </body></html>";

        [mailer setMessageBody:emailBody isHTML:YES];

        [self presentModalViewController:mailer animated:YES];
        [mailer release];
        }

Apple sample code with more instructions available at: http://developer.apple.com/iphone/library/samplecode/MailComposer/

I know this doesn't use the webView but it does allow you to create HTML emails from within your application.

ahefner