func (mc *MailCon) SendMail(a smtp.Auth, from string, to []string, subject string, body string) (err error) { mc.mutex.Lock() defer mc.mutex.Unlock() // 1) Create new SMTP message and send it m := email.NewMessage(subject, body) m.From = from m.To = to if mc.conf.SkipCertificateVerification { err = mc.sendMailSkipCert(a, from, m.Tolist(), m.Bytes()) } else { err = email.Send(fmt.Sprintf("%s:%d", mc.conf.SMTPAddress, mc.conf.SMTPPort), a, m) } // 2) If that worked well, add this mail to the 'Sent' folder of the users inbox if nil == err { // Todo: Think about having this in its own go-routine -> how to handle a possible error? _, err = mc.createMailInFolder_internal(&Header{ Date: time.Now(), Subject: subject, Sender: from, Receiver: strings.Join(to, ", "), Folder: "Sent", }, &Flags{Seen: true}, body) } return err }
func main() { // compose the message if len(os.Args) < 5 { os.Stdout.WriteString("帮助说明:\n\tsendMail.exe [email protected] passwd smtp.hupu.net 25") return } fromTo := os.Args[1] m := email.NewMessage("Hi", "this is iMan test email.\n监控平台邮件服务器测试成功.") m.From = mail.Address{Name: "监控平台测试邮件", Address: fromTo} m.To = []string{fromTo} // // add attachments // if err := m.Attach("main.go"); err != nil { // log.Fatal(err) // } // send it passwd := os.Args[2] smtpConfig := os.Args[3] port := os.Args[4] // username := base64.StdEncoding.EncodeToString([]byte(fromTo)) // passwd = base64.StdEncoding.EncodeToString([]byte(passwd)) server := smtp.ServerInfo{TLS: false} auth := smtp.PlainAuth("LOGIN", fromTo, passwd, smtpConfig) auth.Start(&server) if err := email.Send(smtpConfig+":"+port, auth, m); err != nil { log.Fatal(err) } os.Stdout.WriteString("测试邮件发送成功!\n") for { } }
func detectedMotion(im *opencv.IplImage) { t := time.Now() d := t.Sub(lastNotified) if d < *notifyInterval { return } lastNotified = t log.Printf("Motion detected!") const tempFile = "/tmp/im.png" opencv.SaveImage(tempFile, im, 0) m := email.NewMessage("Motion detected", fmt.Sprintf("Motion detected at %v\n", t)) m.From = *user + "@gmail.com" m.To = []string{*user + "@gmail.com"} err := m.Attach(tempFile) if err != nil { log.Printf("Couldn't attach image: %v\n", err) } err = email.Send("smtp.gmail.com:587", smtp.PlainAuth("", *user, *password, "smtp.gmail.com"), m) if err != nil { log.Printf("Couldn't send mail: %v\n", err) return } log.Printf("Sent email...\n") }
// take the post, lookup list members, send email func GomezAlert(w http.ResponseWriter, req *http.Request) { name := req.URL.Query().Get(":name") body, err := ioutil.ReadAll(req.Body) ringring := "URL" // FIXME: remove once I can get POSTs from Gomez to work: email_subject := "alert from gomez" email_from := "FROM" smtp_server := "smtp.gmail.com:587" smtp_login := "******" smtp_passwd := "PASSWORD" smtp_host := "smtp.gmail.com" // get the response and error for the "smart contact" url for the list // FIXME: we should define a list of folks to get alerted in case this // call fails - we can send out the regular alert plus a note about not // being able to talk to ringring for the oncall list. resp, err := http.Get(ringring + name) // it looks like if you define something (for this example, err) and you do not use it in any // way, go will throw an error at compile time about it. // FIXME: we should log + move on rather than print to stdout and exit if err != nil { fmt.Printf("%s", err) os.Exit(1) } else { // setting defer on this closes out the connection automatically // if we didn't set this we would have to do this manually. defer resp.Body.Close() email_addresses, err := ioutil.ReadAll(resp.Body) // FIXME: we need to parse the XML in the body from Gomez // FIXME: we should log + move on rather than print to stdout and exit if err != nil { fmt.Printf("%s", err) os.Exit(1) } // send the email m := email.NewMessage(email_subject, string(body)) m.From = email_from m.To = strings.Split(string(email_addresses), "\n") err = email.Send(smtp_server, smtp.PlainAuth("", smtp_login, smtp_passwd, smtp_host), m) } }
func SendEmail(from string, password string, to string, title string, message string, attach_path string) { m := email.NewMessage(title, message) m.From = from m.To = []string{to} err := m.Attach(attach_path) if err != nil { panic(err) } err = email.Send("smtp.gmail.com:587", smtp.PlainAuth("", from, password, "smtp.gmail.com"), m) if err != nil { panic(err) } }
func main() { m := email.NewMessage("Hi", "测试邮件发送,时间、附件都已调通.") m.From = "*****@*****.**" m.To = []string{"*****@*****.**", "*****@*****.**"} // err := m.Attach(`C:\Users\Administrator\Desktop\4点起床:最养生和高效的时间管理.pdf`) // if err != nil { // log.Println(err) // } err := email.Send("192.168.119.141:25", smtp.PlainAuth("", "*****@*****.**", "aptech", "192.168.119.141"), m) if err != nil { log.Fatal(err) } else { log.Println("邮件发送成功...") } }
func main() { mail := email.NewMessage(Conf.Subject, Conf.Text) mail.From = Conf.From mail.To = []string{Conf.To} if len(Conf.Attach) > 0 { files := getfiles(Conf.Attach) for i := 0; i < len(files); i++ { mail.Attach(files[i]) } } err := email.Send(Conf.Server+":"+Conf.Port, smtp.PlainAuth("", Conf.User, Conf.Pass, Conf.Server), mail) checkerr(err, "message not send", "message send to:"+Conf.To+" files:"+strconv.Itoa(len(Conf.Attach))) }
func Example() { // compose the message m := email.NewMessage("Hi", "this is the body") m.From = mail.Address{Name: "From", Address: "*****@*****.**"} m.To = []string{"*****@*****.**"} // add attachments if err := m.Attach("email.go"); err != nil { log.Fatal(err) } // send it auth := smtp.PlainAuth("", "*****@*****.**", "pwd", "smtp.zoho.com") if err := email.Send("smtp.zoho.com:587", auth, m); err != nil { log.Fatal(err) } }
func (a *emailAction) Run(ev *Event, h *Handler) error { if h.Email.Address == "" { return ErrNotConfigured } template := h.Email.Subject if template == "" { template = defaultSubject } subject := fmt.Sprintf(template, ev.Name, ev.Service.Name, Hostname) body := fmt.Sprintf(` Resorcerer has detected an noteworthy event. Time: %s Host: %s Service: %s Event: %s Value: %v `, time.Now(), Hostname, ev.Service.Name, ev.Name, ev.Value) m := email.NewMessage(subject, body) if a.settings.From == "" { m.From = defaultFrom } else { m.From = a.settings.From } m.To = strings.Split(h.Email.Address, ",") show("Sending email:\n%s", string(m.Bytes())) if DryRun { return nil } host, _, _ := net.SplitHostPort(a.settings.Server) auth := smtp.PlainAuth("", a.settings.Username, a.settings.Password, host) return email.Send(a.settings.Server, auth, m) }
func handle(w http.ResponseWriter, r *http.Request) { folderPath, _ := osext.ExecutableFolder() /* Preparing Logging */ log := l4g.NewLogger() flw := l4g.NewFileLogWriter(folderPath+LogFileName, false) clw := l4g.NewConsoleLogWriter() flw.SetFormat("[%D %T] [%L] (%S) %M") log.AddFilter("file", l4g.FINE, flw) log.AddFilter("stdout", l4g.FINE, clw) var items []LogRecord /* Error cecking, data validation */ if r.Method != "POST" { w.WriteHeader(http.StatusMethodNotAllowed) fmt.Fprintf(w, "%s", "Cotnact admin!\n") log.Error("Method Not Allowed: %s", r.Method) return } if r.ParseForm() != nil { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, "%s", "Cotnact admin!\n") log.Error("Parse POST data failed!") return } if r.FormValue("postdata") == "" { w.WriteHeader(http.StatusExpectationFailed) fmt.Fprintf(w, "%s", "POST Data missing!\n") log.Error("POST Data missing") return } /* Deciding file name */ const timeformat = "2006-01-02_15-04-05" var filename = time.Now().Format(timeformat) + ".log" if r.FormValue("username") != "" { filename = r.FormValue("username") + "_" + time.Now().Format(timeformat) + ".log" } /* Json Parsing and data validation */ json.Unmarshal([]byte(r.FormValue("postdata")), &items) if len(items) == 0 { w.WriteHeader(http.StatusExpectationFailed) log.Error("Possible Json Parse Error: Nr of items: %d ", len(items)) fmt.Fprintf(w, "Possible Json Parse Error: Nr of items: %d ", len(items)) return } filebuffer := "" for _, item := range items { b, err := json.Marshal(item) if err != nil { log.Error("Json Parse Error: %s ", err) } filebuffer += string(b) + "\n" } log.Info("writing %d items to %s", len(items), filename) /* File Creation */ f, err := os.Create(os.Args[2] + filename) if err != nil { w.WriteHeader(http.StatusInternalServerError) log.Error("File create error (%s): %s ", filename, err) return } /* File Write */ _, err = io.WriteString(f, filebuffer) if err != nil { w.WriteHeader(http.StatusInternalServerError) log.Error("File write error (%s): %s ", filename, err) return } f.Close() m := email.NewMessage("Socialeyes feedback", "The feedback file is attached!") m.From = "*****@*****.**" m.To = []string{"*****@*****.**", "*****@*****.**"} error := m.Attach(os.Args[2] + filename) if error != nil { log.Info("Successfully sent notification mail.") } error = email.Send("smtp.gmail.com:587", smtp.PlainAuth("", "*****@*****.**", "<password>", "smtp.gmail.com"), m) fmt.Fprintf(w, "%s", filename) log.Info("Successfully wrote data to: " + filename) log.Close() }