Skip to content

Script: TemplateByType

Samuel Plentz edited this page Sep 16, 2023 · 1 revision

This template enables you to apply personalized greetings plus name (nickname) and signatures depending if the mail is new or Re/Fwd.

Script

// get subject from email if possible
if(!this.mWindow.document.getElementById('msgSubject')) return "";
let subject = this.mWindow.document.getElementById('msgSubject').value;


// detect first prefix
let firstPrefix = "";
firstPrefix="";
if(subject.toLowerCase().startsWith("re: "))  firstPrefix = "Re: ";
if(subject.toLowerCase().startsWith("fwd: ")) firstPrefix = "Fwd: ";


// search and remove all prefixes
let previousLength = 0;
while(previousLength != subject.length) { // repeat the process if something gets shortened
  previousLength = subject.length;

  subject = subject.replace(/^(re:\s|r:\s|aw:\s|antw:\s|antwort:\s)/i, "");    
// replace "Re: ", "R: ", "Aw: ", "Antw: ", "Antwort: "
  subject = subject.replace(/^(fwd:\s|fw:\s|wg:\s|wt:\s|wtrlt:\s)/i, "");      
// replace "Fwd: ", "Fw: ", "Wg: ", "Wt: ", "Wtrlt: "
  subject = subject.replace(/^(Re-\d:\s|Re\[\d\]:\s)/i, "");                   
// replace "Re-1: ", "Re-2: ", "Re[1]: ", "Re[2]: "
  subject = subject.replace(/^(\*\*\*\*SPAM\*\*\*\*\s|\[SPAM\]\s)/i, "");      
// replace "****Spam**** ", "[Spam] "
  subject = subject.replace(/^(Automatische Antwort:\s)/i, "");                
// replace "Automatische Antwort: "
  subject = subject.replace(/^(\[Extern]\s-\s|\[Extern]\s|\[Extern]:\s)/i,""); 
// replace "[Extern] - ", "[Extern] ", "[Extern]: "
  subject = subject.replace(/^(\[Probable Suspicious URLs\]\s)/i,"");          
// replace "[Probable Suspicious URLs] "
  subject = subject.trim();
}


// reuse first prefix and change subject
subject = firstPrefix + subject;
this.mWindow.document.getElementById('msgSubject').value = subject;


//GetMailBody script to replece selected text with template + that same text
var editor = this.mWindow.gMsgCompose.editor;
var text = editor.outputToString('text/html', 1);

switch(firstPrefix){
case "":
return "[[SCRIPT=GoodMorning]]"+text+"[[TEXT=greetings|signature]]"
break;
case "Re: ":
return "[[SCRIPT=GoodMorning]]"+text+ "[[TEXT=greetings|response]]"
break;
case "Fwd: ":
return "FYI"+
"[[TEXT=greetings|response]]";

}

Usage

It uses ClearSubject as main template and adds another community scripts such as "GetMailBody"(embebed), "GoodMorning" and "ToNickname"(you need to have them).

You can call it right before start or even after writing the response mail. In the last case, just make sure to select your written text for it to be inserted in the template.(see examples below)

I have a full html signature (signature) and a small one (response) as template that will be applied depending on the email type. This template code will be :[[CURSOR]][[FILE=C:\response.html]]

You need to modify the "GoodMorning" script code to add the "ToNickname" one so that the name or nickname is inside that greeting

var date = new Date();
if (date.getHours() <12) return "Good morning "+"[[SCRIPT=ToNickname]]"+",<br><br>";
if (date.getHours() < 20) return "Good afternoon "+"[[SCRIPT=ToNickname]]"+",<br><br>";
if (date.getHours() > 20) return "Good night "+"[[SCRIPT=ToNickname]]"+",<br><br>";
return "Hello"+[[SCRIPT=to]]+",<br><br>";

Now you need to create a template calling this Script [[SCRIPT=TemplateByType]]

Execution examples

New mail to Tom without text at 10am

Good Morning Tom,

(cursor will be placed here)

full signature

Re: mail with answer written before applying the template at 9pm. Make sure to select text (ex: will be there by 3pm)

Good night Tom,

will be there by 3pm
(cursor will be placed here)

small signature

Fwd: mail

FYI
(cursor will be placed here)

small signature