MailTo urls are quite simple to compose. It's just a question of composing a string of the right format, including subject, cc, bcc and body components as required. This should give you an idea.
Here's a javascript constructor to make things easy:
function Mailto_url(){
var encode_mailto_component = function(str){
try{ return encodeURIComponent(str); }
catch(e){ return escape(str); }
}
var AddressList = function(){
var list = [];
this.length = 0;
this.add = function(address){
if(address) {
list.push(address);
this.length = list.length;
}
};
this.get = function(){
return list.join(';');
};
};
var subject = '',
body = '',
mainList = new AddressList(),
ccList = new AddressList(),
bccList = new AddressList();
this.setSubject = function(str){ subject = encode_mailto_component(str); }
this.setBody = function(str){ body = encode_mailto_component(str); }
this.addMain = function(x) { mainList.add(x); }
this.addCC = function(x) { ccList.add(x); }
this.addBCC = function(x) { bccList.add(x); }
this.getURL = function(allow_empty_mainList){
var out = ['mailto:'];
var extras = [];
if(mainList.length === 0 && !allow_empty_mainList){
throw('Mailto_url: no main addressees');
}
else{
out.push(mainList.get());
}
if(subject) { extras.push('subject=' + subject); }
if(ccList.length) { extras.push('cc=' + ccList.get()); }
if(bccList.length) { extras.push('bcc=' + bccList.get()); }
if(body) { extras.push('body=' + body); }
if(extras.length) { out.push('?' + extras.join('&')); }
return out.join('');
}
} I have no specific knowledge of iPhone/touch so have incuded some safety, in case the javascript engine supports only old fashioned escape() and not encodeURIComponent() . I expect it supports the latter but it's best to avoid an uncaught error if possible. For most characters, the result is identical.
And here's how to use it:
var mailTo = new Mailto_url();
mailTo.addMain('airshow@ccc.net');
mailTo.addMain('mrsairshow@ddd.net');
mailTo.addCC('linda@xyz.com');
mailTo.addCC('mandy@abc.com');
mailTo.addBCC('susanne@mno.com');
mailTo.addBCC('chris@mno.com');
mailTo.setSubject('About the party');
mailTo.setBody('Could you remind me of the date please.');
alert(mailTo.getURL()); I just alert the composed mailTo url here but you will use it as a hyperlink's HREF, eg.:
myLink.href = mailTo.getURL(); There's no validation, so be sure only to feed it only with valid email addresses.
To build mailTo urls server-side, then implement something similar in PHP, JSP, ASP etc.Airshow
