Here you go:
http://code.google.com/p/go-wiki/wiki/SendingMail
Andrew
This example "works", but in the real world you almost always need to
authenticate and before you do that you usually need to start TLS.
It's probably also better to have an example that uses smtp.SendMail
since that covers the vast majority of use cases. The equivalent
(including authentication) would be:
package main
import (
"log"
"smtp"
)
func main() {
auth := smt.PlainAuth("", "us...@example.com", "password",
"mail.example.com")
err := smtp.SendMail("mail.example.com:25", auth,
"sen...@example.org", []string{"reci...@example.net"}, []byte("This
is the email body."))
if err != nil {
log.Fatal(err)
}
}
- Evan
You have write access to the wiki. :-)
"Normal password" sounds like PlainAuth would work. If you use
smtp.SendMail, it will switch to TLS automatically once it detects
that the server supports it.
It's possible, though unlikely, that your server requires TLS from the
very beginning. In that case you'd have to create your own TLS
connection, then use smtp.NewClient, and run through the SendMail
steps manually.
- Evan
// Set the sender and recipient.
c.Mail("sen...@example.org")
c.Rcpt("reci...@example.net")
c. Subject("Saying hello")
package mainimport ("crypto/tls""fmt""log""net""net/mail""net/smtp")func main() {// the basicsfrom := mail.Address{"senders name", "user...@sender.com"}to := mail.Address{"recipients name", "user...@recipient.com"}body := "this is the body line1.\nthis is the body line2.\nthis is the body line3.\n"subject := "this is the subject line"// setup the remote smtpserver & auth infosmtpserver := "remote.mailserver.com:25"auth := smtp.PlainAuth("", "user...@sender.com", "senders-password", "remote.mailserver.com")// setup a map for the headersheader := make(map[string]string)header["From"] = from.String()header["To"] = to.String()header["Subject"] = subject// setup the messagemessage := ""for k, v := range header {message += fmt.Sprintf("%s: %s\r\n", k, v)}message += "\r\n" + body// create the smtp connectionc, err := smtp.Dial(smtpserver)if err != nil {log.Panic(err)}// set some TLS options, so we can make sure a non-verified cert won't stop us sendinghost, _, _ := net.SplitHostPort(smtpserver)tlc := &tls.Config{InsecureSkipVerify: true,ServerName: host,}if err = c.StartTLS(tlc); err != nil {log.Panic(err)}// auth stuffif err = c.Auth(auth); err != nil {log.Panic(err)}// To && Fromif err = c.Mail(from.Address); err != nil {log.Panic(err)}if err = c.Rcpt(to.Address); err != nil {log.Panic(err)}// Dataw, err := c.Data()if err != nil {log.Panic(err)}_, err = w.Write([]byte(message))if err != nil {log.Panic(err)}err = w.Close()if err != nil {log.Panic(err)}c.Quit()}
Use something like Mandrill (https://mandrillapp.com/api/docs/messages.html) + the gochimp library (https://github.com/mattbaird/gochimp) instead.
You can then use templates on your side (e.g. template/text) or theirs (using merge variables) so you don't have to worry about format strings.
I've been using this for a small project and it's been pretty robust. Easy to track bounces as well, which can be useful.
You want something like this
body := []byte(fmt.Sprintf("Subject: %s\r\n\r\n%s", subject, body))
Hi, I wanted to ask a question about sending a mail with Golang, I think the question can fit here as this is a general thread about SMTP in Go.My question is : How can I set the "Object" field when i send a mail ?
Hi, Sorry it was a bad translation. I meant the "Subject" field. I can set up the "To:", the "From:" and the message itself, but I can not set up a Subject description.How can I do this ?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.