Created
November 24, 2014 18:55
-
-
Save tuhlmann/d2b82572fb9e73cfde78 to your computer and use it in GitHub Desktop.
Extending Lift's Mailer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package code.lib | |
import javax.mail.BodyPart | |
import javax.mail.internet.{MimeMultipart, MimeBodyPart} | |
import net.liftweb.util.Mailer | |
object ExtendedMailer extends Mailer with ExtendedMailer | |
trait ExtendedMailer extends Mailer { | |
final case class PlainPlusImages(text: String, items: PlusImageHolder*) extends MailBodyType | |
/** | |
* Given a MailBodyType, convert it to a javax.mail.BodyPart. You can override this method if you | |
* add custom MailBodyTypes | |
*/ | |
override protected def buildMailBody(tab: MailBodyType): BodyPart = { | |
tab match { | |
case PlainPlusImages(txt, img@_*) => | |
val bp = new MimeBodyPart | |
val multipart = new MimeMultipart("related") | |
val bp2 = new MimeBodyPart | |
bp2.setText(txt, "UTF-8") | |
multipart.addBodyPart(bp2) | |
img.foreach { | |
i => | |
val rel_bpi = new MimeBodyPart | |
rel_bpi.setFileName(i.name) | |
rel_bpi.setContentID(i.name) | |
rel_bpi.setDisposition(if (!i.attachment) "inline" else "attachment") | |
rel_bpi.setDataHandler(new javax.activation.DataHandler(new javax.activation.DataSource { | |
def getContentType = i.mimeType | |
def getInputStream = new java.io.ByteArrayInputStream(i.bytes) | |
def getName = i.name | |
def getOutputStream = throw new java.io.IOException("Unable to write to item") | |
})) | |
multipart.addBodyPart(rel_bpi) | |
} | |
bp.setContent(multipart) | |
bp | |
case _ => super.buildMailBody(tab) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment