package com.hikcreate.data.client

import java.io.File
import play.api.libs.mailer._

object EmailClient {

  val host = "smtp.qq.com"
  val port = 587
  val user = Option("272558733@qq.com")
  val password = Option("zjthyvgxtyvlbibh")
  val configuration: SMTPConfiguration = new SMTPConfiguration(host, port, user = user, password = password)
  val mailer: SMTPMailer = new SMTPMailer(configuration)

  /**
    * 添加本地文件AttachmentFile
    * @param name 附件名
    * @param filePath 本地文件路径
    * @return
    */
  def generateLocalAttachment(name:String,filePath:String): AttachmentFile = AttachmentFile(name,new File(filePath))

  /**
    * 添加非本地文件AttachmentData
    * @param name 附件名
    * @param data 字节流
    * @param mimetype 文件类型
    */
  def generateNotLocalAttachment(name:String,data:Array[Byte],mimetype:String): AttachmentData = AttachmentData(name,data,mimetype)

  /**
    * @param subject 邮件主题
    * @param to 邮件接收地址
    * @param bodyText 如果bodyText参数和bodyHtml参数同时有,则只会显示bodyHtml中的内容
    * @param bodyHtml 一般情况下这两个参数也只会用一个,另一个用None
    * @param attachments 附件序列
    */
  def sendEmail(subject:String,to:Seq[String], bodyText:Option[String],bodyHtml:Option[String],attachments:Option[Seq[Attachment]]): Unit ={
    val email = attachments match {
      case Some(x) => Email(subject,user.get,to,bodyText,bodyHtml,attachments = x)
      case None => Email(subject,user.get,to,bodyText,bodyHtml)
    }
    mailer.send(email)
  }
}