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
Airshow
WiFi Lounge Lizard
2,782 posts since Apr 2009
Reputation Points: 370
Solved Threads: 388
Skill Endorsements: 9
Basamdamdu,
OK, first install my Mailto_url() constructor. Copy from my post above and paste between <script>...</script> tags in your page's <head>.
Then replace getContent() with this version:
function getContent(link) {
var quote = document.getElementById('quote');
if(quote && quote.innerHTML){
var mailTo = new Mailto_url();
mailTo.setSubject("Here's a great quote I just saw");
mailTo.setBody(quote.innerHTML);
link.href = mailTo.getURL(true);
return true;
}
return false;
}
Then adjust the HTML as follows:
<li><a id="share" class="share" href="#" onclick="return getContent(this)">Share Quote</a></li>
Tested in Opera 11.61, with Windows Live mail client.
Airshow
Airshow
WiFi Lounge Lizard
2,782 posts since Apr 2009
Reputation Points: 370
Solved Threads: 388
Skill Endorsements: 9
Question Answered as of 1 Year Ago by
Airshow Basamdamdu,
Pleased to be of help. At the risk of upsetting you, that's probably no more than Beginner+ level.
If you want to get a better feel for the language, then you could do worse than go here and scroll down to the "videos" section. Douglas Crockford is extremely knowledgeable and although you may not understand everything he says, you will find his enthusiasm is infectious.
Reputation (rep) on Daniweb is given against posts and accrues to the poster. Typically, you go to a post that provided you with an answer or was otherwise useful or stimulating and up-vote it (up arrow in top-right corner) - you will be invited to enter a comment. Down-voting is also possible. The amount of rep points given is determined automatically by one's own rep level - see your profile.
Thanks in advance
Airshow
Airshow
WiFi Lounge Lizard
2,782 posts since Apr 2009
Reputation Points: 370
Solved Threads: 388
Skill Endorsements: 9