func (m *Mailgun) Call(hubMsg HubMessage) error {
	if len(m.To) == 0 {
		log.Print("MailGun: no recipients configured. Nothing to do.")
		return errors.New("MailGun: no recipients configured. Nothing to do.")
	}
	msg := mailgun.Message{
		FromName:      m.Name,
		FromAddress:   m.From,
		Subject:       "Some Subject",
		ToAddress:     m.To[0],
		CCAddressList: m.To[1:],
	}

	body, err := json.Marshal(hubMsg)
	if err != nil {
		log.Print(err)
		return err
	}
	msg.Body = string(body)
	client := mailgun.NewClient(m.Key, m.Domain)
	_, err = client.Send(msg)
	if err != nil {
		log.Print(err)
		return err
	}
	return nil
}
func (m *Mailgun) Call(hubMsg HubMessage) {
	msg := mailgun.Message{
		FromName:      m.Name,
		FromAddress:   m.From,
		Subject:       "Some Subject",
		ToAddress:     m.To[0],
		CCAddressList: m.To[1:],
	}

	body, err := json.Marshal(hubMsg)
	if err != nil {
		log.Print(err)
		return
	}
	msg.Body = string(body)
	client := mailgun.NewClient(m.Key, m.Domain)
	_, err = client.Send(msg)
	if err != nil {
		log.Print(err)
	}
}
func (j *NewAccountEmailJob) Perform() error {
	account, err := FindAccount(j.AccountId)
	if err != nil {
		return err
	}

	token := genKey("confirm" + account.Email)
	account.ConfirmationToken = &token

	message, err := j.CreateMessage(account)
	if err != nil {
		return err
	}

	_, err = dbmap.Exec(
		"update accounts set confirmation_token = $1, confirmation_sent_at = $2 where id = $3",
		token,
		time.Now().UTC(),
		j.AccountId,
	)
	if err != nil {
		return err
	}

	if os.Getenv("MAILGUN_API_KEY") != "" {
		mg := mailgun.NewClient(os.Getenv("MAILGUN_API_KEY"), os.Getenv("MAILGUN_DOMAIN"))
		_, err := mg.Send(message)
		if err != nil {
			return err
		}
	} else {
		fmt.Println(message)
	}

	return nil
}
示例#4
0
func main() {
	data, err := ioutil.ReadFile("config.json")

	if err != nil {
		log.Fatalln(err)
	}

	var c Config

	err = json.Unmarshal(data, &c)

	db, err := sql.Open("mysql", c.Username+":"+c.Password+"@/"+c.Database)

	if err != nil {
		log.Fatalln(err)
	}

	defer db.Close()

	conn, err := gobeanstalk.Dial(c.Address)

	if err != nil {
		log.Fatalln(err)
	}

	mg_client := mailgun.NewClient(c.Key, c.Mailbox)

	for {
		j, err := conn.Reserve()

		if err != nil {
			log.Fatalln("reserve failed", err)
		}

		err = conn.Delete(j.Id)

		if err != nil {
			log.Fatalln(err)
		}

		email := string(j.Body)

		t := time.Now()
		t.Format("2006-01-02 15:04:05")

		result, err := db.Exec(`INSERT INTO email(email, created_at) VALUES(?,?);`, email, t)

		if err != nil {
			log.Println("db error", err, result)
		}

		message := mailgun.Message{
			Body:        c.Body,
			FromAddress: c.FromAddress,
			FromName:    c.FromName,
			Subject:     c.Subject,
			ToAddress:   email,
		}

		log.Println("Attempting to send to ", mg_client.Endpoint(message))

		body, err := mg_client.Send(message)

		if err != nil {
			log.Println("Got an error:", err)
		} else {
			log.Println(body)
		}
	}
}
示例#5
0
文件: web.go 项目: qSlide/qslide
func Web() {
	var config *app.Configuration
	config = app.LoadConfiguration()
	fmt.Printf("%s is domain", config.Port)
	fmt.Printf("Domain: %s", config.Domain)
	m := martini.Classic()

	f := []template.FuncMap{
		template.FuncMap{
			"eq": func(a, b string) bool {
				return a == b
			},
		},
		template.FuncMap{
			"ne": func(a, b string) bool {
				return a != b
			},
		},
	}

	//m.Use(render.Renderer())
	m.Use(render.Renderer(render.Options{
		Directory:  "templates",                // Specify what path to load the templates from.
		Layout:     "layout",                   // Specify a layout template. Layouts can call {{ yield }} to render the current template.
		Extensions: []string{".tmpl", ".html"}, // Specify extensions to load for templates.
		//Funcs: []template.FuncMap{AppHelpers}, // Specify helper function maps for templates to access.
		//Delims: render.Delims{"{[{", "}]}"}, // Sets delimiters to the specified strings.
		Charset: "UTF-8", // Sets encoding for json and html content-types.
		Funcs:   f,
	}))

	store := sessions.NewCookieStore([]byte(config.CookieSecret))
	m.Use(sessions.Sessions("my_session", store))

	m.Get("/", func(r render.Render, session sessions.Session) {
		type Site struct {
			Domain         string
			VisitCount     uint
			Env            string
			BookmarkletURL string
		}
		BookmarkletURL := "//" + config.Domain + "/javascripts/main.js"
		if config.Env == app.PROD {
			BookmarkletURL = "//" + config.Domain + "/javascripts/main.js"
		}
		site := Site{config.Domain, 12, config.Env, BookmarkletURL}
		rand := rand.New(rand.NewSource(99))
		csrf := fmt.Sprintf("%f", rand.Float64()*1000000)
		session.Set("CSRF", csrf)
		var msg string
		if nil != session.Get("FlashMessage") {
			msg = session.Get("FlashMessage").(string)
			session.Delete("FlashMessage")
		} else {
			msg = ""
		}

		r.HTML(200, "hello", struct {
			A, B, CSRF, FlashMessage string
			Site                     Site
		}{"Guesis", "bar", csrf, msg, site})
		//return "<h1>Hello, world!</h1>"
	})

	m.Get("/qslider/download", func(r render.Render, session sessions.Session) {

	})

	m.Post("/qslider/support", func(res http.ResponseWriter, req *http.Request, params martini.Params, session sessions.Session) {
		var config *app.Configuration
		config = app.LoadConfiguration()
		mg_client := mailgun.NewClient(config.MG_API_KEY, config.MG_DOMAIN)

		req.ParseForm()
		p := req.Form
		log.Println(req.Form)
		if session.Get("CSRF") == p["csrf"][0] {
			message1 := mailgun.Message{
				FromName:    "QSlider",
				FromAddress: "*****@*****.**",
				ToAddress:   "*****@*****.**",
				Subject:     "Ticket on qslide.axcoto.com",
				Body:        "Contact from: " + p["youremail"][0] + " with content: \n<br />" + p["content"][0],
			}

			fmt.Println("Attempting to send to ", mg_client.Endpoint(message1))

			body, err := mg_client.Send(message1)
			if err != nil {
				fmt.Println("Got an error:", err)
			} else {
				fmt.Println(body)
				session.Set("FlashMessage", "Message sent")
				http.Redirect(res, req, "/", 302)
				//http.Redirect(res, req, "http://slide.dev", http.StatusTemporaryRedirect)
			}
		}
	})

	m.Get("/qslider/tutorial", func(r render.Render) {
	})

	//m.Run()
	cmd := exec.Command("pwd", "> /tmp/101")
	//cmd := exec.Command("", "5")
	err := cmd.Start()
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Waiting for command to finish...")
	err = cmd.Wait()
	http.ListenAndServe(fmt.Sprintf(":%s", config.Port), m)
	//http.ListenAndServe(":10005", m)
}