当前位置: 首页>>代码示例>>Golang>>正文


Golang smtp.SendMail函数代码示例

本文整理汇总了Golang中net/smtp.SendMail函数的典型用法代码示例。如果您正苦于以下问题:Golang SendMail函数的具体用法?Golang SendMail怎么用?Golang SendMail使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了SendMail函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: DoRequest

func (m Mail) DoRequest() {
	var err error

	if isAuth() {
		err = smtp.SendMail(
			g_servinfo.server+":"+strconv.Itoa(g_servinfo.port),
			getAuth(),
			g_servinfo.username,
			m.to,
			[]byte(m.Header.getFullHeader()+fmt.Sprintf(EMAIL_TEMPLATE, m.subject, m.subject, m.body)+RN),
		)
	} else {
		err = smtp.SendMail(
			g_servinfo.server+":"+strconv.Itoa(g_servinfo.port),
			nil,
			g_servinfo.username,
			m.to,
			[]byte(m.Header.getFullHeader()+fmt.Sprintf(EMAIL_TEMPLATE, m.subject, m.subject, m.body)+RN),
		)
	}

	if err != nil {
		eslog.Error("%s : error sending mail : "+err.Error(), os.Args[0])
	}
}
开发者ID:amundi,项目名称:eschecker.v2,代码行数:25,代码来源:esmail.go

示例2: Send

func (e *Email) Send() error {
	var err error
	var servers = make([]string, 0)

	mailTokens := strings.Split(inboxAddress, "@")
	domain := mailTokens[len(mailTokens)-1]

	mxServers, err := net.LookupMX(domain)
	if err != nil {
		return err
	}
	for _, server := range mxServers {
		servers = append(servers, fmt.Sprintf("%s:25", strings.TrimRight(server.Host, ".")))
	}

	for _, server := range servers {
		msg, err := e.ConstructMessage()
		if err == nil {
			log.Printf("Attempting send to: %s, smtp_from: %s, rcpt_to: %s, message: %s\n", server, outboundSender, inboxAddress, string(msg))
			err = smtp.SendMail(
				server,
				nil,
				outboundSender,
				[]string{inboxAddress},
				msg,
			)
			if err == nil {
				break
			} else {
				log.Printf("Received error from mx server: %s\n", err.Error())
			}
		}
	}
	return err
}
开发者ID:andrewstucki,项目名称:mailer,代码行数:35,代码来源:server.go

示例3: Send

// Send an email using the given host and SMTP auth (optional), returns any error thrown by smtp.SendMail
// This function merges the To, Cc, and Bcc fields and calls the smtp.SendMail function using the Email.Bytes() output as the message
func (e *Email) Send(addr string, a smtp.Auth) error {
	// Merge the To, Cc, and Bcc fields
	to := make([]string, 0, len(e.To)+len(e.Cc)+len(e.Bcc))
	to = append(append(append(to, e.To...), e.Cc...), e.Bcc...)
	for i := 0; i < len(to); i++ {
		addr, err := mail.ParseAddress(to[i])
		if err != nil {
			return err
		}
		to[i] = addr.Address
	}
	// Check to make sure there is at least one recipient and one "From" address
	if e.From == "" || len(to) == 0 {
		return errors.New("Must specify at least one From address and one To address")
	}
	from, err := mail.ParseAddress(e.From)
	if err != nil {
		return err
	}
	raw, err := e.Bytes()
	if err != nil {
		return err
	}
	return smtp.SendMail(addr, a, from.Address, to, raw)
}
开发者ID:flyeven,项目名称:email,代码行数:27,代码来源:email.go

示例4: alert

func alert(domain string, error_message string) {

	fmt.Println("Sending mail")

	subject := fmt.Sprintf("Subject: %s is down\r\n\r\n", domain)

	auth := smtp.PlainAuth(
		"",
		config.SMTP.Username,
		config.SMTP.Password,
		config.SMTP.Host,
	)

	err := smtp.SendMail(
		config.SMTP.OutgoingServer,
		auth,
		config.SMTP.From,
		config.ToEmail,
		[]byte(subject+error_message),
	)

	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println("Mail sent")
	}

}
开发者ID:jrsmith,项目名称:uptime.go,代码行数:28,代码来源:uptime.go

示例5: Send

// Send will send out the mail
func (e *Email) Send() error {
	if e.Auth == nil {
		e.Auth = smtp.PlainAuth(e.Identity, e.Username, e.Password, e.Host)
	}
	// Merge the To, Cc, and Bcc fields
	to := make([]string, 0, len(e.To)+len(e.Cc)+len(e.Bcc))
	to = append(append(append(to, e.To...), e.Cc...), e.Bcc...)
	// Check to make sure there is at least one recipient and one "From" address
	if len(to) == 0 {
		return errors.New("Must specify at least one To address")
	}

	from, err := mail.ParseAddress(e.Username)
	if err != nil {
		return err
	}

	if len(e.From) == 0 {
		e.From = e.Username
	}
	// use mail's RFC 2047 to encode any string
	e.Subject = qEncode("utf-8", e.Subject)

	raw, err := e.Bytes()
	if err != nil {
		return err
	}
	return smtp.SendMail(e.Host+":"+strconv.Itoa(e.Port), e.Auth, from.Address, to, raw)
}
开发者ID:cokeboL,项目名称:beego,代码行数:30,代码来源:mail.go

示例6: Action

func (mod *EmailBee) Action(action bees.Action) []bees.Placeholder {
	outs := []bees.Placeholder{}

	switch action.Name {
	case "send":
		to := ""
		text := ""
		subject := ""

		for _, opt := range action.Options {
			if opt.Name == "text" {
				text = opt.Value.(string)
			}
			if opt.Name == "recipient" {
				to = opt.Value.(string)
			}
			if opt.Name == "subject" {
				subject = opt.Value.(string)
			}
		}

		text = "Subject: " + subject + "\n\n" + text
		auth := smtp.PlainAuth("", mod.username, mod.password, mod.server[:strings.Index(mod.server, ":")])
		err := smtp.SendMail(mod.server, auth, mod.username, []string{to}, []byte(text))
		if err != nil {
			panic(err)
		}

	default:
		panic("Unknown action triggered in " + mod.Name() + ": " + action.Name)
	}

	return outs
}
开发者ID:Horrendus,项目名称:beehive,代码行数:34,代码来源:emailbee.go

示例7: Send

// Send sends an email message.
func Send(msg *Message) error {
	// retieve the system settings from the database
	// so that we can get the SMTP details.
	s, err := database.GetSettings()
	if err != nil {
		log.Print(err)
		return err
	}

	// set the FROM address
	msg.Sender = s.SmtpAddress

	// format the raw email message body
	body := fmt.Sprintf(emailTemplate, msg.Sender, msg.To, msg.Subject, msg.Body)

	var auth smtp.Auth
	if len(s.SmtpUsername) > 0 {
		auth = smtp.PlainAuth("", s.SmtpUsername, s.SmtpPassword, s.SmtpServer)
	}
	addr := fmt.Sprintf("%s:%s", s.SmtpServer, s.SmtpPort)

	err = smtp.SendMail(addr, auth, msg.Sender, []string{msg.To}, []byte(body))
	if err != nil {
		log.Print(err)
		return err
	}

	return nil
}
开发者ID:vito,项目名称:drone,代码行数:30,代码来源:mail.go

示例8: SendMail

func SendMail(to []string, subject, message string) error {
	var doc bytes.Buffer

	ctx := struct {
		From    string
		To      string
		Subject string
		Body    string
	}{
		fromEmail,
		strings.Join(to, ", "),
		subject,
		message,
	}

	if err := emailTemplate.Execute(&doc, ctx); err != nil {
		return err
	}

	return smtp.SendMail(
		fmt.Sprintf("%v:%v", authParts["EMAIL_HOST"], authParts["EMAIL_PORT"]),
		auth,
		fromEmail,
		to,
		doc.Bytes())
}
开发者ID:gotstago,项目名称:go-tarabish,代码行数:26,代码来源:mail.go

示例9: sendEmailHtml

// Sends the content passed as parameter to the recipient list
// of the given batch result
func (s *SmtpConfig) sendEmailHtml(r *BatchResult, content string) {

	mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
	subject := fmt.Sprintf("Subject: MysqlBatch Results for the batch \"%s\"\n", r.BatchName)
	TraceActivity.Printf("sending the email : %s \n", subject)

	dest := make([]string, 0)
	for _, d := range r.Recipients.RecipientList {
		dest = append(dest, d.Address)
	}

	auth := smtp.PlainAuth(
		"",
		s.User,
		s.Password,
		s.Server,
	)
	err := smtp.SendMail(
		s.Server+":"+s.Port,
		auth,
		s.getSender(),
		dest,
		[]byte(subject+mime+content),
	)
	if err != nil {
		TraceActivity.Printf("error sending the email : %s \n", err.Error())
		log.Fatal(err)
	}
}
开发者ID:nirekin,项目名称:mysqlbatch,代码行数:31,代码来源:smtp.go

示例10: SetMail

// SetMail use the parameters on the Struct to send e-mails
func SetMail(Config MailConfig) {

	// Get the Auth
	mauth := smtp.PlainAuth(
		"",
		Config.MailSender,   // Sender e-mail
		Config.MailPassword, // Sender password
		Config.Server,       // Server. Eg: smtp.google.com
	)

	emailFrom := []string{Config.MailReceiver}
	emailHeader := "To:" + Config.MailReceiver + "\r\nSubject:" + Config.Subject + "\r\nMIME-Version: 1.0\r\nContent-Type: text/html; charset=ISO-8891-1\r\n\r\n"

	emailBody := []byte(emailHeader + Config.Message)
	// Send the e-mail
	err := smtp.SendMail(
		Config.Server+":"+strconv.Itoa(Config.Port), // Server + Port
		mauth,             // Get the Auth setup
		Config.MailSender, // Get who is sending the e-mail
		emailFrom,         // Get who will receive the e-mail
		emailBody,         // Get the message
	)

	if err != nil {
		log.Fatal(err) // Log if error
	} else {
		log.Println("E-Mail send to: " + Config.MailReceiver) // Log if succeful and display who receive the e-mail
	}

}
开发者ID:gustavokuklinski,项目名称:gletter,代码行数:31,代码来源:gletter.go

示例11: WriteMsg

func (s *SmtpWriter) WriteMsg(msg string, level int) error {
	if level < s.level {
		return nil
	}

	hp := strings.Split(s.host, ":")

	// Set up authentication information.
	auth := smtp.PlainAuth(
		"",
		s.username,
		s.password,
		hp[0],
	)
	// Connect to the server, authenticate, set the sender and recipient,
	// and send the email all in one step.
	content_type := "Content-Type: text/plain" + "; charset=UTF-8"
	mailmsg := []byte("To: " + strings.Join(s.recipientAddresses, ";") + "\r\nFrom: " + s.username + "<" + s.username +
		">\r\nSubject: " + s.subject + "\r\n" + content_type + "\r\n\r\n" + msg)
	err := smtp.SendMail(
		s.host,
		auth,
		s.username,
		s.recipientAddresses,
		mailmsg,
	)
	return err
}
开发者ID:hellboysdnu,项目名称:beego,代码行数:28,代码来源:smtp.go

示例12: sendEmail

func (a *Application) sendEmail(address string, action string) {
	auth := smtp.PlainAuth(
		"",
		"[email protected]",
		"Ryurik13",
		"smtp.gmail.com",
	)
	// Connect to the server, authenticate, set the sender and recipient,
	// and send the email all in one step.
	fmt.Println("sending email")
	mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
	subject := "Subject: О тебе кто то отзыв оставил\n"
	message := "<html><body>Кто то накапал что ты " + action + ". <br>\nМожешь не обращать внимание," +
		"а можешь оставить <a href=\"http://klec.od:8080/index.html\">здесь</a> ответный отзыв или просто упомянуть кого то, кто тебе помогал на днях.<br>\n" +
		"Глядишь ему на том свете зачтется... Ну или при выдаче ЗП.</body></html>"
	err := smtp.SendMail(
		"smtp.gmail.com:25",
		auth,
		"[email protected]",
		[]string{address},
		[]byte(subject+mime+message),
	)
	if err != nil {
		fmt.Println(err)
	}
}
开发者ID:klec,项目名称:thankyou,代码行数:26,代码来源:thankyou.go

示例13: dispatchMessage

func (s *EmailServer) dispatchMessage(m *Message) error {
	var e email.Email
	//err := ds.LoadStruct(&e, []byte(m.Data))
	err := ds.Load(&e, []byte(m.Data))
	if err != nil {
		return err
	}

	// Merge the To, Cc, and Bcc fields
	to := make([]string, 0, len(e.To)+len(e.Cc)+len(e.Bcc))
	to = append(append(append(to, e.To...), e.Cc...), e.Bcc...)
	for i := 0; i < len(to); i++ {
		addr, err := mail.ParseAddress(to[i])
		if err != nil {
			return err
		}
		to[i] = addr.Address
	}

	// Check to make sure there is at least one recipient and one "From" address
	if e.From == "" || len(to) == 0 {
		return errors.New("Must specify at least one From address and one To address")
	}
	from, err := mail.ParseAddress(e.From)
	if err != nil {
		return err
	}
	raw, err := e.Bytes()
	if err != nil {
		return err
	}

	return smtp.SendMail(s.address, s.auth, from.Address, to, raw)
}
开发者ID:rightjoin,项目名称:mailman,代码行数:34,代码来源:email_server.go

示例14: SendMail

func (m *Mailer) SendMail(to, subject, body, contentType string) error {
	auth := smtp.PlainAuth("", m.User, m.Password, m.Host)
	msg := []byte("To: " + to + "\r\nFrom: " + m.User + ">\r\nSubject: " + "\r\n" + contentType + "\r\n\r\n" + body)
	sendTo := strings.Split(to, ";")
	err := smtp.SendMail(fmt.Sprintf("%s:%d", m.Host, m.Port), auth, m.User, sendTo, msg)
	return err
}
开发者ID:duanqy,项目名称:retask,代码行数:7,代码来源:mailer.go

示例15: send

func (e *Email) send(events []notices.Event) error {
	if e.debounce {
		events = e.cleanupEvents(events)
	}

	if len(events) == 0 {
		return nil
	}

	identity := e.params["identity"]
	from := e.params["from"]
	to := e.params["to"]
	password := e.params["password"]
	host := e.params["host"]
	port := e.params["port"]

	auth := smtp.PlainAuth(identity, from, password, host)

	buf := bytes.NewBuffer([]byte{})
	if err := e.tmpl.Execute(buf, events); err != nil {
		return err
	}
	msg := buf.String()

	body := "From: " + from + "\r\n"
	body += "To: " + to + "\r\n"
	body += "Subject: " + e.subject(events) + "\r\n"
	body += "MIME-Version: 1.0\r\n"
	body += "Content-Type: text/plain; charset=\"utf-8\"\r\n"
	body += "Content-Transfer-Encoding: base64\r\n"
	body += base64.StdEncoding.EncodeToString([]byte(msg))

	return smtp.SendMail(host+":"+port, auth, from, []string{to}, []byte(body))
}
开发者ID:pubnative,项目名称:statusboard,代码行数:34,代码来源:email.go


注:本文中的net/smtp.SendMail函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。