예제 #1
0
func sendTestMessage() error {
	mail_server := os.Getenv("MAIL_SERVER")
	mail_sender := os.Getenv("MAIL_SENDER")
	mail_passwd := os.Getenv("MAIL_PASSWD")

	message := mail.NewTextAndHtmlMessage(
		[]string{"*****@*****.**"},
		"Test Message",
		"Test Text Body",
		"Test <b>Html</b> <i>Body</i><br>",
	)
	// message.Cc = []string{"*****@*****.**"}
	// message.Bcc = []string{"*****@*****.**"}
	sender := mail.Sender{
		From:    mail_sender,
		ReplyTo: mail_sender,
	}

	mailer := mail.Mailer{
		Server:   mail_server,
		Port:     587,
		UserName: mail_sender,
		Password: mail_passwd,
		// Host: "iassic.com",
		// Auth: smtp.Auth,
		Sender: &sender,
	}

	return mailer.SendMessage(message)
}
예제 #2
0
func (e Account) sendEmail(user *models.User, verifyType, subject string) error {

	var (
		mailerServer    = r.Config.StringDefault("mailer.server", "smtp.example.org")
		mailerPort      = r.Config.IntDefault("mailer.port", 25)
		mailerUsername  = r.Config.StringDefault("mailer.username", "<username>")
		mailerPassword  = r.Config.StringDefault("mailer.password", "<password>")
		mailerFromAddr  = r.Config.StringDefault("mailer.fromaddress", "*****@*****.**")
		mailerReplyAddr = r.Config.StringDefault("mailer.replyaddress", "*****@*****.**")
		callbackHost    = r.Config.StringDefault("http.host", "http://localhost:9000")
	)

	// If mail has not been configured, don't try to send a confirmation email
	if mailerServer == "smtp.example.org" {
		return nil
	}

	mailer := m.Mailer{Server: mailerServer, Port: mailerPort, UserName: mailerUsername, Password: mailerPassword}
	mailer.Sender = &m.Sender{From: mailerFromAddr, ReplyTo: mailerReplyAddr}

	// Generate a new token and store against the user's id
	verifyEmailToken := e.generateVerifyHash(16)

	e.storeVerifyHashRecord(user.Email, verifyType, verifyEmailToken)

	// arguments used for template rendering
	var args = make(map[string]interface{})

	args["user"] = user

	args["Link"] = callbackHost + "/account/" + verifyType + "/" + string(verifyEmailToken)

	message := &m.Message{To: []string{user.Email}, Subject: subject}

	rErr := message.RenderTemplate("email/"+verifyType, args)
	if rErr != nil {
		return rErr
	}

	sErr := mailer.SendMessage(message)
	if sErr != nil {
		return sErr
	}

	fmt.Println("Mail sent to " + string(user.Email))

	return nil
}