MFMailComposeViewController : how do I embed a clickable URL link into the email message body
Asked Answered
H

5

14

I want my app using the MFMailComposeViewController to send an email such that the recipient can click on the embedded url to open the corresponding web site. MFMailComposeViewController does not appear to support this explicitly. Any ideas?

Harpist answered 13/1, 2010 at 16:39 Comment(0)
T
18

:) Yes, you can do this:

MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = self;
[composer setSubject:subject];
[composer setMessageBody:message isHTML:YES];                    

where message is just an NSString with HTML content. Inside you can add all the HTML you want.

Thundercloud answered 13/1, 2010 at 16:52 Comment(0)
C
27

I deleted my previous answer as it was incorrect and irrelevant. After much hair pulling I finally figured out what was going on in my case and is probably what is happening in this question.

When you compose the HTML body for the MFMailComposeViewController you must put line breaks in the HTML. If any line is > 76 chars long, the body will be interpreted as follows:

Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

If you put line breaks in, the Content-Transfer-Encoding: quoted-printable will not happen and everything works as expected. Assuming you have proper HTML.

As an example, build the body as follows:

NSMutableString *body = [NSMutableString string];
// add HTML before the link here with line breaks (\n)
[body appendString:@"<h1>Hello User!</h1>\n"];
[body appendString:@"<a href=\"http://www.mysite.com/path/to/link\">Click Me!</a>\n"];
[body appendString:@"<div>Thanks much!</div>\n"];

Cheers!

Cloying answered 11/11, 2010 at 20:3 Comment(1)
Excellent! good to know about the line breaks!, save me some time man cheersOscular
T
18

:) Yes, you can do this:

MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = self;
[composer setSubject:subject];
[composer setMessageBody:message isHTML:YES];                    

where message is just an NSString with HTML content. Inside you can add all the HTML you want.

Thundercloud answered 13/1, 2010 at 16:52 Comment(0)
S
9

I have the same problem.

My link is HTML, I can see 'blue' but if I click it, doesn't open safari mobile. Is allowed to me edit the text.

In a class I have this:

-(id) init{
    self = [super init];
    if (self) {
            if ([MFMailComposeViewController canSendMail]) {
                self.mailComposeDelegate = self;
                [self setSubject: @"Subject"];
                [self setMessageBody: @"<h2>Body</h2><a href='http://www.google.com'>link example</a>" isHTML: YES];
            }
            else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Mail Accounts" 
                                                                message:@"You don't have a Mail account configured, please configure to send email."
                                                               delegate:nil 
                                                      cancelButtonTitle:@"OK" 
                                                      otherButtonTitles: nil];
                [alert show];
            }
    }
    return self;
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    [controller dismissModalViewControllerAnimated: YES];
}

Here you can see the iPad Screen shot: iPad Screen shot

If I send, and then I go to "Sent" Mailbox the link works, so I think the problem is the event which open the links.

Thanks.

Sedgewake answered 6/6, 2012 at 11:47 Comment(2)
Have you done with click? have you redirect on link if yes then please post here @Oceanicsix ThanksActinism
Hi, it was more than one year ago, if I am honest now I can't remember how I finished this issue. Sorry and luck!Sedgewake
P
6

Use setMessageBody:isHTML: and pass a proper HTML link in the body (<a href="your_url">your link text</a>) and pass YES to the isHTML parameter.

Percept answered 13/1, 2010 at 16:51 Comment(0)
P
5

did you try on your code your suggestion? I tried it before getting to this web site, and sorry to say, it doesn't work at all. The Link appears really in blue, the HTML is read as html, but no link is possible. When I click on the link I can just edit it....

Any better suggestion?

Pennsylvanian answered 4/11, 2010 at 14:44 Comment(1)
Did you clicked on received email or in editing mode?Altarpiece

© 2022 - 2024 — McMap. All rights reserved.