Imagine this scenario:
You are creating an air application, destined for both os x and windows. Everything works as it should and you are fixing the last functions. One of them is to create an email in your default mail application with a predefined subject and body.
Since it’s flex and cross platform compatible and all that, you create the email by using navigateToUrl in flex and opening a link with all content like this:
mailto:emailadress@theinternet.com?subject=hello&body=thisisanemail.
In windows, everything works fine and you are happy. You know that cross platform prolems might occur, but you hope that, at least this time, everything is ok.
And everything actually works fine.
That is until you put some special characters in your subject or body. Our application needed some those äöå so we had a problem.
In mac os x, the body and subject where emptied as soon as one single special character appeared. No subject, no body and nothing to do about it. We tried to urlencode, write unicode characters directly with \u etc but nothing worked.
We almost gave up and told our client that they might have to rethink their copy and try to avoid any special characters (which is quite hard in swedish).
But, as our last test, we tried to see if that problem occurred if we only had a html file with s subject with the same characters. That worked and we concluded that the problem had to do with flex and how it handles the navigateToUrl string.
So, to solve everything and make our client happy, we created a work-around, and here it is:
1. Create a HTMLLoader in flex:
var html:HTMLLoader = new HTMLLoader();
2. Create a HTMLHost in it:
html.htmlHost = new HTMLHost();
3. fill the html with an empty page but a javascript redirect to the mailto string:
html.loadString(’<html><script>location.href=”mailto:emailadress@theinternet.com?subject=Hello ÄÖÅ&body=This isatest ÄÖÅ”;</script><body></body></html>’);
Thats it. Very simple, works with both windows and mac and everything i solved.
here is the complete code for it:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<mx:Script>
<![CDATA[ function mailto():void { var html:HTMLLoader = new HTMLLoader(); html.htmlHost = new HTMLHost(); var mailtoString = '<html><script>location.href='; mailtoString += '"mailto:name@domain.com?'; mailtoString += 'subject=tjenaÅÄÖ ÄÖÅ&body=HELLO BODY ÄÖÅ";'; mailtoString += '</script><body></body></html>';
html.loadString(mailtoString); }
]]>
</mx:Script>
<mx:Button label="click to mailto" click="mailto();" />
</mx:WindowedApplication>
This is why we are happy clients. Great work!
Excellent, this was really useful thanks. Is it also possible to add attachments? I’ve tried using this approach but no luck..
private function mailto():void
{
var html:HTMLLoader = new HTMLLoader();
html.htmlHost = new HTMLHost();
var mailtoString:String = ‘location.href=’;
mailtoString += ‘”mailto:name@domain.com?’;
mailtoString += ’subject=hello’;
mailtoString += ‘&attachments=C:\flex3\test-app.xml’;
mailtoString += ”;
html.loadString(mailtoString);
}
Hi Will
Thanks for you comment. I don’t think that you can attach a file that way, since it would mean that you could attach a file with a standard link in a browser. For example, if you have a page with an anchor tag, you could attach a windows system file (since most people have windows installed at c:\windows it would be quite easy to guess where it is) without showing the user too much.
Since the solution i showed creates a standard javascript inside a html page, I would guess that the same restrictions apply there.
But please let me know if you find a way of solving it.
Wonderful info. Will definitely come back again=D