Extra caution is recommended when installing recently uploaded/updated scripts (read more)
Be sure you trust any scripts you install

Gmail from address selector

Updated 6/26/07 Now works for cc & bcc Automatically Select to send from different from addresses based on the addresses you are sending to. For example when sending to a .edu address you can have gmail use your school address as the from address.

I use my Gmail account for all email, and have my school email forwarded to Gmail. Gmail allows me to change the from address so It looks like I sent it from my school account but I always forget to do it.

When the send button is clicked this script checks all addresses in the "To" field and if any of them match a certain criteria then it changes the from address that Gmail uses.

To use it you must install the script and modify it to suit your preferences. There are two things that need to be modified, which from address to use and the regular expression of when to use this address. The default is to use the second from address (index=1) when sending to any .edu email addresses. These parameters can easily be changed by modifying two variables at the top of the script.

Hope someone else finds this useful.






Dec 17, 2007
Du Song User

sorry for misformatted

var emailIndex = 1;
//-----------------------------------------------------------
// Check for certain email addresses
// this can be changed
// currently changes from address if emails are sent to a .edu email address
//-----------------------------------------------------------
var email_regex = "youku\.com";

document.addEventListener('click',function(event) {
 	if (event.target.tagName == 'BUTTON' && event.target.innerHTML == '<b>Send</b>') {
		var to = document.getElementsByName('to')[0].value;
		var cc = document.getElementsByName('cc')[0].value;
		var bcc = document.getElementsByName('bcc')[0].value;
		var from = document.getElementsByName('from')[0].value;
    //console.log('GMail from'+from+' to'+to);
		var myTest=new RegExp(email_regex);
		if(to.match(myTest) || cc.match(myTest) || bcc.match(myTest)){
			var frombox = document.getElementsByName('from')[0]
			frombox.selectedIndex = emailIndex;
 		}		
	}
}, true);

 
Dec 17, 2007
Du Song User

a *working* version for GMail 2.0

//-----------------------------------------------------------
// set this to index of email address you want to use
// index of 1 would select the second email in the drop down list
// index of 2 would select the third email in the drop down list and so on,
//-----------------------------------------------------------
var emailIndex = 1;
//-----------------------------------------------------------
// Check for certain email addresses
// this can be changed
// currently changes from address if emails are sent to a .edu email address
//-----------------------------------------------------------
var email_regex = "company\.com";

document.addEventListener('click',function(event) {
if (event.target.tagName == 'BUTTON' && event.target.innerHTML == 'Send') {
var to = document.getElementsByName('to')[0].value;
var cc = document.getElementsByName('cc')[0].value;
var bcc = document.getElementsByName('bcc')[0].value;
var from = document.getElementsByName('from')[0].value;
//console.log('GMail from'+from+' to'+to);
var myTest=new RegExp(email_regex);
if(to.match(myTest) || cc.match(myTest) || bcc.match(myTest)){
var frombox = document.getElementsByName('from')[0]
/*for (var i=0;i<frombox>
alert(frombox.options[i].value + ' ' + i);
}*/
frombox.selectedIndex = emailIndex;
//following line used for debugging the regular expression
//alert(frombox.length + ' ' + frombox.multiple + ' ' + frombox.options[2].value);
}
}
}, true);

 
Aug 19, 2007
jsheena User

This is very useful! I've made two modifications which I hope you'll find useful:

1. Now there's an array of address patterns

2. There's an optional user confirmation before changing the address.

Here's the code:

// ==UserScript==
// @name           Gmail from address selector
// @namespace      test123
// @description    Selects the from address of same domain your sending to
// @include        http*://mail.google.tld/mail/*
// ==/UserScript==



//-----------------------------------------------------------
// Check for certain email addresses
// this can be changed
// currently changes from address if emails are sent to a .edu email address
// You can add as many checks as you like by copying the line marked "copy this"
// 
// The first argument of the array is the regular expression matching email
// addresses you're sending to (or cc, or bcc).
// For example, ".*(mycompany.com).*" matches any address sent to mycompany.com
//
//
// The second argument of the array is the index of the email you want to use
// as the sender.
//
// set this to index of email address you want to use
// index of 1 would select the second email in the drop down list
// index of 2 would select the third email in the drop down list and so on,
//-----------------------------------------------------------


var emailMatchers = new Array (
	new Array(".*(.edu).*", 1),   /* <--><emailmatchers>

 
Jun 21, 2007
Brett Brothe... Script's author

Oops fixed bug had commented out a line that was needed

You could comment on this script if you were logged in.