1

I have searched around a bit, but was unable to find an adequate response, so please forgive me if this is already asked on SO.

I would like to do a beautifully formatted/styled email in my MFMailCompseViewController. Is it just a matter of doing a [NSString stringWithFormat...] with a very long HTML form, with the CSS baked in ontop of the head? Something to this effect?

[NSString stringWithFormat:@"<!doctype html> <html> <head> <style type="text/css">
  body {
    color: white;
    background-color: #414042 }
  .p {

    color: #111111;
    text-size: 2em; }
  </style>
  </head>
  <body>
  <h1>This is my beautiful header </h1>
  <p>This is my beautiful paragraph </p>
  </body>
  </html>" ];

Obviously that is a ridiculous example, but is anything like this possible? Maybe even adding fonts and the like?

Thank you!

Maximilian
  • 1,107
  • 11
  • 20

1 Answers1

1

Yep, that is allowed.

To make sure the HTML is properly recognized, call

- (void)setMessageBody:(NSString*)body isHTML:(BOOL)isHTML when setting the message body with YES as the second argument. Simple as that.

Aloha Silver
  • 1,394
  • 17
  • 35
  • Great @Aloha Silver! Before I accept, do you have any idea how I would be able to add fonts or images? I can do images with [messageController addAttachmentData:_imageData typeIdentifier:@"public.png" filename:experimentEmailName]; But any idea about fonts? I have fonts installed locally on the phone, or perhaps make a call to a google font in the ? – Maximilian May 06 '14 at 19:53
  • 1
    I'm not sure if making calls on the header works, but using custom, installed fonts on your HTML is even easier than attaching images. After adding custom fonts to your app bundle (if you need a reference for that, checkout the answer here: http://stackoverflow.com/questions/3837965/how-to-add-custom-fonts-to-an-iphone-app), just set them on CSS like you normally would (using the `font-family` property). – Aloha Silver May 06 '14 at 20:00
  • 1
    Great, I also just tried Google Font API, worked like a charm! Thank you! – Maximilian May 06 '14 at 21:59
  • That's actually great to know. Thanks for reporting it! – Aloha Silver May 06 '14 at 23:39
  • Any chance you know how I could add an image into the HTML? When you do an attachment, it puts it on the bottom of the email, which is fine, but I'd love to put my company logo as a sort of header on top of the email before the contents of the email. Is it as simple as getting the file path of the image on the phone and doing that with an tag? – Maximilian May 07 '14 at 15:00
  • 1
    Unfortunately, inserting images on the HTML code is not as simple. Bundled images need a baseURL set, and there's no such thing on `MFMailComposeViewController` (unlike `UIWebView` and its `loadHTMLString:baseURL:` method). So you're left with two options: base64-encoding images or using links to images stored elsewhere on the web. I've had some success base64-encoding images, although this behavior also depends on the receiver client. – Aloha Silver May 07 '14 at 17:41
  • If you need a reference for base64-encoding images on the go, check out this answer: http://stackoverflow.com/a/2461451/382834. The Base64 class extension (by Matt Gallagher) mentioned is available here: http://www.cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html. But be aware, like I previously mentioned, this depends on the client to interpret the code, and there are some reports that Gmail doesn't support it, for instance (like on the answer I linked). – Aloha Silver May 07 '14 at 17:43
  • such excellent comments, yesterday I was successful in getting images in the body with base64 encoding: I'll add the code to my question: however you are right gmail web app does not display any of the html, which is a bummer. Will have to look at the mail chimp info on it and others, but for now, outlook, apple mail, iOS mail does render it, and I'm happy with that for now. Thanks again for all of your help! – Maximilian May 08 '14 at 18:28
  • I'm glad I could help. Have success on your project! ;) – Aloha Silver May 09 '14 at 19:06