Beispiel #1
0
func sendMail(toUser, fromUser string, key string) { //sends email out
	isClean := checkLearningDB(key)
	if isClean == true {
		fmt.Println("Message is in white list")
	} else {
		fmt.Println("Message is not in white list")
		dat := make(map[string]interface{})
		dat["to"] = toUser
		dat["from"] = fromUser
		dat["text"] = key

		hash := md5hash(key)
		htmlTemplate := generateHTML(hash, dat)

		mandrill.Key = mandrillKey2
		// you can test your API key with Ping
		err := mandrill.Ping()
		if err != nil {

			fmt.Println("ERROR")
		}

		msg := mandrill.NewMessageTo(" ", " ")

		msg.HTML = htmlTemplate
		msg.Subject = "Moderation Alert"
		msg.FromEmail = " "
		msg.FromName = " "
		var temp bool
		msg.Send(temp)

	}

}
Beispiel #2
0
func Send() {
	msg := mandrill.NewMessageTo("*****@*****.**", "Vinh Nguyen Test")
	msg.HTML = "HTML content<a href=\"sasas\">sasa</a>a  sasa"
	msg.Text = "plain text content" // optional
	msg.Subject = "Test"
	msg.FromEmail = "*****@*****.**"
	msg.FromName = "Vinh Nguyen"
	res, err := msg.Send(false)
	if nil != err {
		log.Println("Fail to send email")
	}

	log.Println("Mail status: " + res[0].Status)
}
func SendMail(email *models.Email) (bool, string) {
	fmt.Println("SendMail")

	mandrill.Key = mandrill_key
	// you can test your API key with Ping
	err := mandrill.Ping()
	// everything is OK if err is nil
	msg := mandrill.NewMessageTo(email.ToAddress, email.Subject)

	var content string

	if email.UseContent {
		content = email.Content
	} else {
		//reads the e-mail template from a local file
		template_byte, err := ioutil.ReadFile(email.TemplatePath)
		checkErr(err, "File Opening ERROR")
		content = string(template_byte[:])
	}

	if email.HTMLContent {
		msg.HTML = content
	} else {
		msg.Text = content
	}

	msg.Subject = email.Subject
	msg.FromEmail = mail_from
	msg.FromName = email.FromName

	//envio assincrono = true // envio sincrono = false
	res, err := msg.Send(email.Async)
	checkErr(err, "SendMail File Opening ERROR")
	resp, _ := json.Marshal(res[0])

	fmt.Println(string(resp))

	return res[0] != nil, string(resp)
}
Beispiel #4
0
func (this *Sender) send(users []auth.User, vote storage.Vote) {
	funcPrefix := "Sending notifications"
	log.Debug.Printf("%s: start\n", funcPrefix)
	defer log.Debug.Printf("%s: end\n", funcPrefix)

	var devices Devices

	log.Debug.Printf("%s: getting DevIds from users...\n", funcPrefix)
	for i := range users {
		log.Debug.Printf("%s: getting DevId from user %+v...\n", funcPrefix, users[i])
		switch users[i].Device {
		case DEVICE_IOS:
			devices.AppleIds = append(devices.AppleIds, users[i].DevId)
		case DEVICE_ANDROID:
			devices.GoogleIds = append(devices.GoogleIds, users[i].DevId)
		default:
			devices.OtherUsers = append(devices.OtherUsers, users[i])
		}
	}

	if len(devices.GoogleIds) > 0 {
		log.Debug.Printf("%s: trying to send Google device notifications to %v\n", funcPrefix, devices.GoogleIds)
		data := map[string]interface{}{"date": strconv.FormatInt(vote.Date, 10), "id": string(vote.Id), "name": string(vote.Name), "owner": string(vote.Owner), "voted": strconv.FormatBool(vote.Voted)}
		msg := gcm.NewMessage(data, devices.GoogleIds...)
		sender := &gcm.Sender{ApiKey: this.GoogleApiKey}
		_, err := sender.Send(msg, 2)
		if err != nil {
			log.Error.Printf("%s: sending notification to Google device failed: %s\n", funcPrefix, err.Error())
		}
	}

	if len(devices.AppleIds) > 0 {
		log.Debug.Printf("%s: Apple notification Server: %s, Cert: %s, Key: %s\n", funcPrefix, this.AppleServer, this.AppleCertPath, this.AppleKeyPath)
		apn, err := apns.NewClient(this.AppleServer, this.AppleCertPath, this.AppleKeyPath)
		apn.MAX_PAYLOAD_SIZE = 2048
		if err != nil {
			log.Error.Printf("%s: sending notification to Apple device failed: %s\n", funcPrefix, err.Error())
		} else {
			payload := &ApplePayload{}
			payload.Aps.Alert = vote.Name
			payload.Aps.Title = "MOC Pulse"
			payload.Aps.Category = "newVote"
			payload.Aps.Vote = vote
			actions := &SimulatorAction{}
			actions.Title = "Vote"
			actions.Identifier = "voteButtonAction"
			payload.SimulatorActions = append(payload.SimulatorActions, *actions)
			bytes, _ := json.Marshal(payload)
			log.Debug.Printf("%s: Apple notification payload: %v\n", funcPrefix, string(bytes))
			for i := range devices.AppleIds {
				log.Debug.Printf("%s: trying to send Apple device notification to %v\n", funcPrefix, devices.AppleIds[i])
				err = apn.SendPayloadString(devices.AppleIds[i], bytes, 5)
				if err != nil {
					log.Error.Printf("%s: sending notification to Apple device failed: %s\n", funcPrefix, err.Error())
				}
			}
		}
	}

	if len(devices.OtherUsers) > 0 {
		mandrill.Key = this.MandrillKey
		err := mandrill.Ping()
		if err != nil {
			log.Error.Printf("%s: sending notification to Email failed: %s\n", funcPrefix, err.Error())
		} else {
			data := make(map[string]string)
			data["QUESTION"] = vote.Name
			data["VOTE"] = vote.Id
			for i := range devices.OtherUsers {
				data["TOKEN"] = devices.OtherUsers[i].Id
				log.Debug.Printf("%s: trying to send Email notification to %v\n", funcPrefix, devices.OtherUsers[i].Email)
				msg := mandrill.NewMessageTo(devices.OtherUsers[i].Email, devices.OtherUsers[i].FirstName+devices.OtherUsers[i].LastName)
				msg.Subject = this.MandrillSubject
				msg.FromEmail = this.MandrillFromEmail
				msg.FromName = this.MandrillFromName
				_, err := msg.SendTemplate(this.MandrillTemplate, data, false)
				if err != nil {
					log.Error.Printf("%s: sending notification to Email failed: %s\n", funcPrefix, err.Error())
				}
			}
		}
	}
}