package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	qu "qfw/util"
	"qfw/util/mail"
)

var from,to, smtpHost,user,pwd string
var smtpPort int
var tomail string
var api string

//smtp模式  二选一 皆可
func sendErrMailSmtp(title,body string) {
	smtpMail, _ := sysconfig["smtpMail"].(map[string]interface{})

	if smtpMail != nil {
		from, _ = smtpMail["from"].(string)
		to, _ = smtpMail["to"].(string)
		smtpHost, _ = smtpMail["smtpHost"].(string)
		smtpPort= qu.IntAll(smtpMail["smtpPort"])
		user, _ = smtpMail["user"].(string)
		pwd, _ = smtpMail["pwd"].(string)
	}

	ok := mail.GSendMail_B(user, from, from, from, title, body, "", nil, &mail.GmailAuth{
		SmtpHost: smtpHost,
		SmtpPort: smtpPort,
		User:     user,
		Pwd:      pwd,
	})
	if ok {
		log.Println("邮件发送-成功...")
	}else {
		log.Println("邮件发送-失败...")
	}

}



//api模式 二选一皆可
func sendErrVpsMailApi(title,body string)  {
	jkmail, _ := sysconfig["jkmail"].(map[string]interface{})
	if jkmail != nil {
		tomail, _ = jkmail["tovps"].(string)
		api, _ = jkmail["api"].(string)
	}
	log.Println(tomail,api)
	res, err := http.Get(fmt.Sprintf("%s?to=%s&title=%s&body=%s", api, tomail, title, body))
	if err == nil {
		defer res.Body.Close()
		read, err := ioutil.ReadAll(res.Body)
		log.Println("邮件发送成功：", string(read), err)
	}else {
		log.Println("邮件发送失败：", err)
	}
}

func sendErrExtMailApi(title,body string)  {
	jkmail, _ := sysconfig["jkmail"].(map[string]interface{})
	if jkmail != nil {
		tomail, _ = jkmail["toext"].(string)
		api, _ = jkmail["api"].(string)
	}
	log.Println(tomail,api)
	res, err := http.Get(fmt.Sprintf("%s?to=%s&title=%s&body=%s", api, tomail, title, body))
	if err == nil {
		defer res.Body.Close()
		read, err := ioutil.ReadAll(res.Body)
		log.Println("邮件发送成功：", string(read), err)
	}else {
		log.Println("邮件发送失败：", err)
	}
}