What Web Service for eMailing from Smart Apps?

Well, getting no answer from the community, nor from SmartThings support, I finally found by myself a way to substitute Mailjet to Mandrill.
I used Mandrill for free during 18 months, was mostly satisfied with it, but having now to pay $540/year to send 200 emails/year was definitely excessive.
Mailjet allows you to send a maximum of 6 000 emails/months for free, which is more than enough for my needs.
And its interface is also based on JSON, which facilitates the migration from Mandrill.

To send emails from your SmartApp you will need :

  1. to create a Mailjet free account
  2. to get from Mailjet your own pair of USERNAME (API KEY) and PASSWORD (SECRET KEY)
  3. to validate your sender email address (or more than one)
  4. to replace all ‘$$’ instances in the example below with your own values

Sadly, SmartThings does not seem interested in providing a built-in email capability for SmartApps, which would save people needing such function lots of grief…
Hope this helps some of you to save the multiple hours I wasted first to integrate Mandrill, then to do it again with Mailjet :slight_smile:

def sendTestEmail() {
	log.debug "sendTestEmail()............."
        def theText = "Non HTML text message"
    	def fileContent = "attached file body"
        def encoded64 = (String) fileContent.bytes.encodeBase64(true)  // (true) required for 76 characters line splitting
        def theSubject = "Test email sent from SmartApp"
    	sendEmail(encoded64, theSubject, theText)
}


/**************************************************************************************
 * Mailjet API Documentation :
 *		http://dev.mailjet.com/guides/?shell#send-transactional-email
 *		http://dev.mailjet.com/guides/?shell#send-api-json-properties
 * JsonBuilder Documentation :
 *		http://groovy.codehaus.org/gapi/groovy/json/JsonBuilder.html <- buggy example
 *		http://jsonlint.com/
 * hpptPostJson() SmartThings Documentation :
 *		http://docs.smartthings.com/en/latest/ref-docs/smartapp-ref.html#smartapp-http-post-json
 *
 * Note : 'attachedContent' input is supposed to already be Base64 encoded
 **************************************************************************************/
def sendEmail(String attachedContent, theSubject, theText) {
    def builder = new groovy.json.JsonBuilder()
	def root = builder(
        	'Text-part': theText,
        	Subject: theSubject,
        	FromEmail: '$$yourSenderAdress@$$yourDomain',
        	FromName: '$$yourSenderName',
        	Recipients: [
            [
 				Email: '$$email1@$$Domain1',
				Name: '"$$email1Name"',
				Type: 'to'
       		],
            [
 				Email: '$$email2@$$Domain2',
				Name: '"$$email2Name"',
				Type: 'to'
        	]
            ],      
			Headers: [
            	'Reply-To': 'donotreply@$$yourDomain'
        	],
        	Attachments: [[
                'Content-type': 'text/plain',
                Filename: 'myfile.txt',
                binary: true,	// ignored by Mailjet ?
                //content: 'YmxhYmxhYmxh' // : "blablabla" Base64 encoded
                content: attachedContent
        	]]
	)
	//assert root instanceof Map
	def jsonBody = builder.toString()

	def successClosure = { response ->
		log.debug "eMail sent, response : ${response}"
	}

	def params = [
		uri: "https://$$mailJetAPIKey:$$mailJetSecretKey@api.mailjet.com/v3/send", // +postParameters,
		success: successClosure,
		body: jsonBody
	]
    
	return httpPostJson(params)
}
2 Likes