wander
wander

Reputation: 957

how to get around chrome mailto maxlength limit

Chrome's mailto length limit is around 2000 characters. It seems to be enough but in fact it's only enough for English Language. Because the length is calculated after encodeURI.

e.g. for only one Japanese character 'て'. encodeURI('て') gets "%E3%81%A6", which is 9 characters long. So I can only type in around 2000/9 = 200+ Japanese characters in my mail content. This is really too short.

So is there a way to get around this limit? Something like installing chrome plugin is acceptable.

Any suggestion is greatly appreciated.

EDIT

I have finally decided to use a server-side solution: Create a popup page to simulate mail client, with to, cc, subject, content and also a send button. After the user clicks the send button, the form will be submitted and server will send the mail for the user

Upvotes: 16

Views: 13958

Answers (3)

dreamlab
dreamlab

Reputation: 3361

This appears to be a microsoft windows issue. i have tried the following on and it works well with different browsers (safari, chrome 30 & 36, ...) on mac os x.

In windows the request will get truncated to around 2000 characters. this will happen regardless of which browser is being used. it seems windows has a size limit on system uri requests.

I have tried from html <a href="mailto:?body=... and javascript document.location = encodeURI('mailto:?body=' + text) with 100k characters in the message body.

Upvotes: 3

Vijay Jagdale
Vijay Jagdale

Reputation: 2649

This question has been posed several times on StackOverflow over the years without an acceptable answer. Server-side solution is NOT always feasible.

The solution below is probably the best way around the problem. The issue in Chrome/Edge is that the "mailto" URL cannot exceed 2000 encoded characters. Therefore, we first test to see if it exceeds 2000 chars, and if it does, copy the "Body" content to the clipboard, and replace original Body with instructions to paste. Note, you may still will run into issues if you have large "To/CC" recipient list.

function invokeMailClient(to,cc,subj,body) {
  let encodedBody = encodeURIComponent(body);
  let mailTo = "mailto:" + to + "?cc=" + cc + "&Subject=" + subj + "&body=";
  if (mailTo.length + encodedBody.length > 1995) {
    copyToClipboard(body);
    encodedBody = encodeURIComponent("** Please press Ctrl-A Ctrl-V    OR Right-Click and Paste the body template for this email **\n");
  }
  window.open(mailTo + encodedBody);
}

function copyToClipboard(str) {
  let el = document.createElement('textarea');
  el.value = str;
  document.body.appendChild(el);
  el.select();
  document.execCommand('copy');
  document.body.removeChild(el);
};

//TEST IT
let subj = "This is a test subject";
let to="[email protected]";
let cc="[email protected]";

//let body="small body -- no problem";
//invokeMailClient(to,cc,subj,body);

let body = "A quick brown fox jumps over the lazy dog\r\n".repeat(35); 
invokeMailClient(to,cc,subj,body); //large "body" -- copies to clipboard

Upvotes: 5

Martin
Martin

Reputation: 1191

I have no problem with any sized mailto-links on a Firefox (Developer Edition 39.0a2, Windows XP), however Internet Explorer 8 and Chrome do not work with mailto-links over the mentioned size of around 2000 characters. So, I doubt it is a Windows specific problem.

Upvotes: 4

Related Questions