Posting eMails w/ Base64 Attachments from SmartApps

I was able to get sending images as an attachment working with Mandrill starting from this code, thanks! I am using the following with the Generic Camera Device from http://community.smartthings.com/t/generic-camera-device-using-local-connection/3269 and it works well so far. I am going to use it to get text messages with a camera snapshot when my driveway sensor is triggered.

def sendEmail(attachedContent) {
      def builder = new groovy.json.JsonBuilder()
      def root = builder(key: 'your_Mandrill_key', 
           message: [text: 'Hi',
           subject: 'Camera Alert',
           from_email: 'your_email@isp.com',
           from_name: 'your_name',
           to: [[email: 'sendto@isp.com',
                name: 'sendto_name',
    	    type: 'to'
	        ]],      
 	        headers: ['Reply-To': 'donotreply@isp.com'],
	        attachments: [[type: 'image/jpeg', 
                 name: 'image.jpg', 
                 content: "$attachedContent",
             ]]
      ]
     )                          
     assert root instanceof Map
     def jsonBody = builder.toString()

     def successClosure = { response ->
	  log.debug "eMail sent, $response"
	  }
        
    def params = [
         uri: "https://mandrillapp.com/api/1.0/messages/send.json", 
         success: successClosure,
         body: jsonBody
    ]
    // log.debug "jsonBody : $jsonBody"
    // log.debug "sendEmail... ${params}"
    
    httpPostJson(params)            
}

// Put the sendEmail before the storeImage is done

    def putImageInS3(map) {

	def s3ObjectContent

	try {
		def imageBytes = getS3Object(map.bucket, map.key + ".jpg")

		if(imageBytes)
		{
			s3ObjectContent = imageBytes.getObjectContent()
			def bytes = new ByteArrayInputStream(s3ObjectContent.bytes)
            
            def strBase64Image = bytes.bytes.encodeBase64()
            sendEmail(strBase64Image)

			storeImage(getPictureName(), bytes)
            
		}
	}
	catch(Exception e) {
		log.error e
	}
	finally {
		if (s3ObjectContent) { s3ObjectContent.close() }
	}
}
1 Like