Send email to distribution group stored in a SharePoint list using local email client

29.06.2012 Fixed a missing “&” in line 48 – thanks to Jim.


I got a request some time ago from Brett Anderson, asking for a solution for sending a group email to users stored in a SharePoint list.

This solution can be used to harvest email addresses from a SharePoint list, and sending an email using the users local email client. It requires the email addresses to be stored in plain text.

When setup it will look like this:
IMG

Step 1

Create a list (or reuse an existing) with two fields: One for storing the email (in this example the Title field), and one for storing the distribution group name:
IMG

Add this code to a CEWP using the content link option linking to a file containing the code, or in a HTML Form web part in the sourcecode editor, somewhere in your portal:

<select id="distributionGroupSelector"></select>	
<input type="radio" name="cc_bcc" id="radio_to" value="to"><label for="radio_to">To</label>
<input type="radio" name="cc_bcc" id="radio_cc" value="cc"><label for="radio_cc">CC</label>
<input type="radio" name="cc_bcc" id="radio_bcc" value="Bcc" checked="checked"><label for="radio_bcc">BCC</label>
<input type="button" onclick="doSendGroupEmail()" value="Open email client">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://spjsfiles.com/SharePoint%20JavaScripts/spjs-utility/07.03.2012/spjs-utility.js"></script>
<script type="text/javascript">

var emailgroupArgObj = {"listGuid":"{2D0107EE-83E3-4588-8173-DDAAE6D82605}",
						"listBaseUrl":L_Menu_BaseUrl,
						"groupNameField":"GroupName",
						"emailField":'Title',
						"subject":"My email subject",
						"body":"My email body"};
						
/*************************************************/						
/*********** No changes below this line **********/
/*************************************************/	
var emailObj = {};

function getEmailGroups(argObj){
	var res, qb, hb;
	qb = [];
	qb.push("<Where>");
	qb.push("<IsNotNull>");
	qb.push("<FieldRef Name='"+argObj.emailField+"' />");
	qb.push("</IsNotNull>");
	qb.push("</Where>");
	qb.push("<OrderBy><Fieldref Name='"+argObj.groupNameField+"' /></OrderBy>");
	res = spjs_QueryItems({listName:argObj.listGuid,listBaseUrl:argObj.listBaseUrl,query:qb.join(''),viewFields:['ID',argObj.emailField,argObj.groupNameField]});
	hb = [];
	$.each(res.items,function(i,item){
		if(emailObj[item[argObj.groupNameField]]===undefined){
			emailObj[item[argObj.groupNameField]] = [];
			hb.push("<option value='"+item[argObj.groupNameField]+"'>"+item[argObj.groupNameField]+"</option>");
		}
		emailObj[item[argObj.groupNameField]].push(item[argObj.emailField]);
	});
	$("#distributionGroupSelector").html(hb.join(''));
}

function doSendGroupEmail(){
	var groupId, link; 
	groupId = $("#distributionGroupSelector").val();
	link = "";
	if($("#radio_to").prop('checked')){
		link += "mailto:"+emailObj[groupId].join('; ')+"&";
	}else if($("#radio_cc").prop('checked')){
		link += "mailto:?cc="+emailObj[groupId].join('; ')+"&";
	}else{
		link += "mailto:?bcc="+emailObj[groupId].join('; ')+"&";
	}
	location.href = link+"subject="+emailgroupArgObj.subject+"&body="+emailgroupArgObj.body;
}

getEmailGroups(emailgroupArgObj);
</script>
Step 2

Change the properties in the variable “emailgroupArgObj” to match your setup

  • listGuid: The list GUID of the list containing the email addresses.
  • listBaseUrl: The base url of the list containing the email addresses. If the code is in the same site, no need to change the variable L_Menu_BaseUrl.
  • groupNameField: The FieldInternalName of the field containing the group name.
  • emailField: The FieldInternalName of the field containing the email address.
  • subject: Enter a value here to have the subject field prefilled in the email client.
  • body: Enter a value here to have the body field prefilled in the email client.

Read here on how to find the list GUID and the FieldInternalNames

The script src in the code above should be changed to point to local copies of both jQuery and spjs-utility.js.
You find jQuery here, and spjs-utility.js here – ensure you use the latest version.

Ask if anything is unclear.

Alexander

8 Responses to “Send email to distribution group stored in a SharePoint list using local email client”


  • Hi Alexander,
    Great work on this, I’ll be trying this solution out today.
    Very much appreciate your time and effort with your fantastic blog, I don’t know how you find the time. I’ll be sending a beer/flowers donation your way for answering my request.
    Thank You,
    Brett

  • Hello Alexander,

    Is it possible to make the single choice dropdown a multiple choice dropdown field?

    Kind regards,
    Mario

  • Hi Alexander. Annother great solution cheers! Just one bug I spotted. I think Line 48 is missing +”&”; at the end as without this, it concatentates the subject with the emails when you use the ”To” option.

    Cheers

    Jim

  • I have done some work on group e-mailing myself. One issue you’ll run into is that browsers have a URL length limit (~2000 characters for some). It means that when you reach 100 to 150 recipients you’ll have to switch to a different technique.

  • Hi Alex,

    Great piece of code – very useful. Thanks!

    Is there any way that you can apply this same code on a DispForm and have it pull names from a field? I have a form where three names are picked (in seperate fields) – can there be a button that once clicked opens the email client and populates the To: field with the three names that were picked on the form?

  • Hi Alex

    I’m trying to use your very helpful piece of code but I keep getting errors:

    SCRIPT5009: ‘$’ is undefined
    spjs-utility.js, line 196 character 1
    SCRIPT5009: ‘$’ is undefined
    spjs-utility.js, line 211 character 2
    SCRIPT5009: ‘$’ is undefined
    DLs.aspx, line 650 character 2
    SCRIPT5009: ‘$’ is undefined
    DLs.aspx, line 650 character 2
    SCRIPT5009: ‘$’ is undefined
    DLs.aspx, line 650 character 2
    SCRIPT5009: ‘$’ is undefined
    DLs.aspx, line 650 character 2

    Do you know what may be causing this? Its SP2013

    Many Thanks

    GK

Leave a Reply




%d bloggers like this: