0

basically, ive been trying to set up this code for my page, however im struggling as im not the best at javascript. ive tried looking up resources online but none of them seem to help. this is part of a form, i want the users to fill a small form and then, after clicking the button, it should open their mail client with a template. i just need to know how to properly add the information to the javascript, especially how to set up a carbon copy gmail, and if theres anything i should change. i can provide the full form code if needed.

<div class="row">
<input type="submit" value="Enviar." onclick="sendMail(); return false">
</div>

heres the javascript that ive been working with

<script>

function sendMail() {
    var link = "mailto:me@example.com"
             + "?cc=myCCaddress@example.com"
             + "&subject=" + encodeURIComponent("This is my subject")
             + "&body=" + encodeURIComponent(document.getElementById('myText').value)
    ;
    
    window.location.href = link;
}

</script>
suibaiter
  • 3
  • 7

1 Answers1

1

mailto is one of many URL Protocols and it's a way for commands to exit the browser and interact with other applications.

you can try this URL API

https://mail.google.com/mail/?view=cm&fs=1&to=[to]&cc=[cc]&bcc=[bcc]&su=[subject]&body=[body]

Although not recommended. Anyone who clicks on it needs to be logged into their gmail account (if they have one), but technically it works if the user has gmail and is logged in.

here is an example on how to combine urls

const combination = (obj) => {
  const parameter = Object.entries(obj).map(o => `&${o[0]}=${o[1]}`).join('');
  return `https://mail.google.com/mail/?view=cm&fs=1${parameter}`
};

const mail1 = {
  su: 'subject',
  to: 'someone',
  body: 'body',
  cc: 'someone',
  bcc: 'someone'
}

const mail2 = {
  su: 'subject',
  to: 'someone',
  body: 'body'
}

console.log('mail1', combination(mail1));
console.log('mail2', combination(mail2));
Ian
  • 1,198
  • 1
  • 5
  • 15
  • ohh, i do kind of understand this. should a part of the javascript with that? or start with a different code? also, can ccs and bccs be just any gmail account? – suibaiter Jul 21 '22 at 09:18
  • 『should a part of the javascript with that? or start with a different code?』It depends on what programming language you are actually using, different programming languages have different ways to write it, since the questions and tags are all about javascript, so I also answer in javascript. In addition, `ccs` and `bcc` can be filled with any email, not only gmail, just like when you usually send email, if there are multiple emails, you can use `;` to separate them – Ian Jul 21 '22 at 09:52
  • This is actually a combination of URLs, which can be used in most environments. When the user logs in to the Google account, the gmail will be automatically filled in. When the user is not logged in to the google account, it will ask to log in first, and then automatically fill in the gmail – Ian Jul 21 '22 at 09:57
  • You can copy the output string of the example and link it with your browser, it should help you understand faster, or click the example link on the right: [mail1](https://mail.google.com/mail/?view=cm&fs=1&su=subject&to=someone&body=body&cc=someone&bcc=someone), [mail2](https://mail.google.com/mail/?view=cm&fs=1&su=subject&to=someone&body=body) – Ian Jul 21 '22 at 10:00