The document.write is only run once when the page is generated. It is not a dynamic code that gets triggered every time something changes. You need a different approach for this.
Instead of the document.write, just make a normal html link (a-element) with javascript as the href where you change the document url to the mailto link.
For example:
<a href="javascript:location.href='mailto:my@email.com?subject=Subject&body=Bodytext' + total">Mail Me</a>
This means that when the link is clicked, instead of going to an URL, we execute a piece of JavaScript that changes the document's location to the mailto-link, and only THEN embeds the value of the total -variable into the body.
Even better way is to create an external function that you trigger when that link is clicked:
<script type="text/javascript">
function sendMail() {
var email = 'my@email.com';
var subject = 'My subject';
var body = 'This is the total: ' + total;
location.href = 'mailto:' + email
+ '?subject=' + encodeURIComponent(subject)
+ '&body=' + encodeURIComponent(body);
}
</script>
And then in the link you add the following href:
<a href="javascript:sendMail()">Mail Me</a>
Now the total
variable gets embedded in the email body every time the link is clicked, instead of when the page is loaded.
The encodeURIComponent makes sure the subject and body are encoded properly, so that all the special characters and spaces, etc gets translated correctly in the mail client.
I hope that helps!