Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mailto using Javascript? [duplicate]

I'm new to javascript and the following code isn't working:

<script>
function sendMail()
{
    var yourMessage = document.getElementById("message").value
    var subject = document.getElementById("selectList").value
    var mail="mailto:[email protected]?subject="+subject+"&body="+yourMessage;

    window = window.open(mail, 'emailWindow')
}
</script>

I just want a mail client window to open with the subject and body already done.

Help?

EDIT:

I've also tried this:

<script>
function sendMail()
{
    var yourMessage = document.getElementById("message").value
    var subject = document.getElementById("selectList").value
    var mail="mailto:[email protected]?subject="+subject+"&body="+yourMessage;

    $(this).attr('href', mail);
}
</script>

Ive got that now, still not working.

like image 977
Chris G Avatar asked Oct 29 '25 05:10

Chris G


1 Answers

Your code should look like this instead:

<script>
function sendMail()
{
    var yourMessage = document.getElementById("message").value;
    var subject = document.getElementById("selectList").value;
    document.location.href = "mailto:[email protected]?subject="
        + encodeURIComponent(subject)
        + "&body=" + encodeURIComponent(yourMessage);
}
</script>
like image 176
Doug S Avatar answered Oct 30 '25 21:10

Doug S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!