0

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</a></li>
<li><a href="http://www.xyz.com">xyz.com</a></li>
</ul>
The end."

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

3 Answers 3

3

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.

2

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.

3
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.