Drafts Action Directory

Email to group

Posted by @davenicholls, Last update 3 months ago

This action allows emails to be sent to one or more groups of email addresses.

Email address groups are stored as Drafts and are tagged with ‘emaillist’ to indicate the contents. The action searches for all Drafts with that tag and presents them as a multi-select list. Once the user has selected the required list(s) and clicked ‘Ok’ the action collates all the email addresses from selected lists.

You can choose whether the email addresses are added to the ‘To’ or ‘BCC’ field (defaults to BCC)

It then uses the current Draft title line as the Subject of the email and renders the body of the Draft in HTML as the body of the email.

The user is then presented with a new email to send with all email addresses added to the chosen field.

An example email list draft:

My Friends
tim@thebigfruitcomputerco.com
animal@themuppetdrummer.com
dave@lovingthescriptinggreg.com

Install

Steps

  • script

    // Send HTML email to multiple recipients. List of recipients picked up from other drafts
    
    // Which tag marks drafts as email lists?
    var listTag ='emaillist';
    
    // Find all drafts that are tagged as email lists
    var emailLists = Draft.query('','inbox',[listTag]);
    
    // Set up prompt so lists can be chosen
    var p = Prompt.create();
    p.title = 'email Lists';
    p.message = 'Choose email list(s)';
    var lists = [];
    
    // Put all the title lines in an array for the prompt
    for (l in emailLists) {
    	lists.push(emailLists[l].title);
    }
    
    // Add multi select for lists
    p.addSelect('emailLists','email Lists',lists,[],true);
    p.addSelect('sendField','How to send?',['To','BCC'],['BCC'],false);
    p.addButton('Ok');
    p.show();
    
    if (p.buttonPressed === "Ok") {
    
    	// Find out which lists were chosen
    	var chosenLists = p.fieldValues["emailLists"];
    
    	if (chosenLists.length === 0) {
    		alert("No lists were chosen");
    	}
    	else {
    		var allAddresses = [];
    
    		// Look at each chosen list in turn
    		for (var l=0;l < chosenLists.length; l++) {
    			var a = lists.indexOf(chosenLists[l]);
    			// Get the content of the draft for the chosen list
    			var addresses = emailLists[a].content.split('\n');
    			// Get rid of title line
    			addresses.shift();
    			// Add the addresses to the main list
    			for (a=0; a<addresses.length;a++) {
    				if (addresses[a].length !=0) {
    					allAddresses.push(addresses[a]);
    				}
    			}
    		}
    
    		var mail = Mail.create();
    
    		// Set up mmd object so we can send an HTML email
    		var mmd = MultiMarkdown.create();
    		mmd.format = "html";
    		mmd.completeDocument = true;
    	
    		// Render the draft body as HTML
    		mail.body = mmd.render(draft.processTemplate("[[body]]"));
    
    		mail.subject = draft.title;
    		switch (p.fieldValues['sendField'][0]) {
    
    			case 'BCC':
    				mail.bccRecipients = allAddresses;
    				break;
    
    			case 'To':
    				mail.toRecipients = allAddresses;
    				break;
    		}
    		
    		mail.isBodyHTML = true;
    
    		var success = mail.send();
    		if (!success) {
    		  console.log(mail.status);
    		  context.fail();
    		}
    	}
    }
    

Options

  • After Success Default
    Notification Info
    Log Level Info

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.