Search code examples
javascripthtmlmailto

how to send mail with mailTo on submit


I'm having two problems with this snipet:

1) for some reason the body is not updating with the innerHTML of #test

2) The client's email is coming up on the SHARE link but I can't get it to open on submit

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('');
	}
}

function getContent() {
	var mailTo = new Mailto_url();
  var test = document.getElementById('test');
mailTo.addMain('[email protected]');
mailTo.addMain('[email protected]');
mailTo.addCC('[email protected]');
mailTo.addCC('[email protected]');
mailTo.addBCC('[email protected]');
mailTo.addBCC('[email protected]');
mailTo.setSubject("test");
mailTo.setBody(test.innerHTML);
window.location=mailTo.getURL(true);
		
	
}
<html>

<body>
  <div id="wrapper">


    <form>
      <ul>
        <li>
          <a class="home" href="#"></a>
        </li>
        <li><a id="share" class="share" href="#" onclick="getContent()">Share Quote</a></li>
        <li>
          <a class="info" href="#"></a>
        </li>
        <li><input id='test' type='text'></input>
        </li>
        <li><input type='submit' onclick="getContent()"></input>
        </li>
      </ul>
    </form>
  </div>


</body>

</html>


Solution

  • Part 2

    <form onsubmit="return false">
    

    change this lines