views:

2606

answers:

5

This is a little confusing to explain, so bear with me here...

I want to set up a system where a user can send templated emails via my website, except it's not actually sent using my server - it instead just opens up their own local mail client with an email ready to go. The application would fill out the body of the email with predefined variables, to save the user having to type it themselves. They can then edit the message as desired, should it not exactly suit their purposes.

There's a number of reasons I want it to go via the user's local mail client, so getting the server to send the email is not an option: it has to be 100% client-side.

I already have a mostly-working solution running, and I'll post the details of that as an answer, I'm wondering if there's any better way?

+3  A: 

The way I'm doing it now is basically like this:

The HTML:

<textarea id="myText">
    Lorem ipsum...
</textarea>
<button onclick="sendMail(); return false">Send</button>

The Javascript:

function sendMail() {
    var link = "mailto:[email protected]"
             + "[email protected]"
             + "&subject=" + escape("This is my subject")
             + "&body=" + escape(document.getElementById('myText').value)
    ;

    window.location.href = link;
}

This, surprisingly, works rather well. The only problem is that if the body is particularly long (somewhere over 2000 characters), then it just opens a new email but there's no information in it. I suspect that it'd be to do with the maximum length of the URL being exceeded.

nickf
This is a pretty roundabout way of doing this when you can just set href attribute to the same content instead of using javascript.
Ryan Doherty
but the user could change the text...
nickf
Not roundabout if you want to include the contents of the textarea in the email. Also a good method to hide your email from spam harvesters.
Gordon Bell
@Gordon- that is assumed the email harvester doesn't regex inline javascript or follow <script src="">
alex
Use encodeURIComponent in preference to escape which follows arbitrary rules different from URL-encoding. Although Unicode characters are still likely to fail... but then the whole thing is very likely to fail anyway. mailto links with parameters are super-unreliable and shouldn't really be used.
bobince
bobince: yeah I figured it's a dodgy way to do it, but what's the alternative?
nickf
A: 

If this is just going to open up the user's client to send the email, why not let them compose it there as well. You lose the ability to track what they are sending, but if that's not important, then just collect the addresses and subject and pop up the client to let the user fill in the body.

tvanfosson
the idea was that my application fills the body for them. I'll edit the question to make that clearer...
nickf
But why write an email client when you are just going to open one to send the mail?
tvanfosson
it's not an email client, it's just a page on my website which prefills an email message.
nickf
A: 

You don't need any javascript, you just need your href to be coded like this:

<a href="mailto:[email protected]">email me here!</a>
Ryan Doherty
I guess I was expecting that the real code filled in the addresses dynamically.
tvanfosson
+1  A: 

What about having a live validation on the textbox, and once it goes over 2000 (or whatever the maximum threshold is) then display 'This email is too long to be completed in the browser, please <span class="launchEmailClientLink">launch what you have in your email client</span>'

To which I'd have

.launchEmailClientLink {
cursor: pointer;
color: #00F;
}

and jQuery this into your onDomReady

$('.launchEmailClientLink').bind('click',sendMail);
alex