user3612491
user3612491

Reputation: 233

How to send an e-mail from Go

I'm trying to send an e-mail in Golang and I have a lot of problems with it. I'm new in Go so maybe this is very simply but I cannot find the answer on the doc.

This is what I want to do: 1. get an e-mail from the STDIN 2. parse the e-mail (getting from, to, subject, attachments and so on) 3. send this e-mail (put it again to the queue in local postfix)

I did 1 and 2 but I have a problem with 3th one.

This is what I have now:

package main 

import (
  "fmt"
  "github.com/jhillyerd/go.enmime"
  //"github.com/sendgrid/sendgrid-go"
  "net/smtp"
  "github.com/jordan-wright/email"
  "os"
  "net/mail"
  "io/ioutil"
  "bytes"
  )

func main() {

  mail_stdin, err := ioutil.ReadAll(os.Stdin)
  if err != nil {
      return
  }

  // Convert type to io.Reader
  buf := bytes.NewBuffer(mail_stdin)

  msg, err := mail.ReadMessage(buf)
  if err != nil {
    return
  }

  mime, err := enmime.ParseMIMEBody(msg)
  if err != nil {
    return
  }

  # saving attachments
  for _, value := range mime.Attachments {
    fmt.Println(value.FileName())

    err := ioutil.WriteFile(value.FileName(), value.Content(), 0664)
    if err != nil {
      //panic(err)
      return
    }

  fmt.Printf("From: %v\n", msg.Header.Get("From"))
  fmt.Printf("Subject: %v\n", mime.GetHeader("Subject"))
  fmt.Printf("Text Body: %v chars\n", len(mime.Text))
  fmt.Printf("HTML Body: %v chars\n", len(mime.Html))
  fmt.Printf("Inlines: %v\n", len(mime.Inlines))
  fmt.Printf("Attachments: %v\n", len(mime.Attachments))
  fmt.Println(mime.Attachments)
  fmt.Println(mime.OtherParts)
  fmt.Printf("Attachments: %v\n", mime.Attachments)
}

I already did few tests using: net/smtp, sendgrid-go and jordan-wright/email. All I want to do is to send an e-mail (without changing anything) from the server to the queue again. Most of those modules needs to have Auth, but I just want to simply send is using sendmail, in the same way as I can do this from the bash:

# echo "test" | mail {address}

Upvotes: 3

Views: 6721

Answers (1)

evanmcdonnal
evanmcdonnal

Reputation: 48114

Using net/smtp you can do this fairly easily... Assuming you have an smtp server running that you can connect to without authentication. I would guess for what you're trying to accomplish it's actually a lot easier to do through something simple like your gmail ( https://www.digitalocean.com/community/tutorials/how-to-use-google-s-smtp-server )

Anyway, here's a couple code samples to cover either case;

    c, err := smtp.Dial("mail.example.com:25")
    if err != nil {
        log.Fatal(err)
    }
    defer c.Close()
    // Set the sender and recipient.
    c.Mail("[email protected]")
    c.Rcpt("[email protected]")
    // Send the email body.
    wc, err := c.Data()
    if err != nil {
        log.Fatal(err)
    }
    defer wc.Close()
    buf := bytes.NewBufferString("This is the email body.")
    if _, err = buf.WriteTo(wc); err != nil {
        log.Fatal(err)
    }

Alternatively here's a go playground example that uses simple auth; http://play.golang.org/p/ATDCgJGKZ3 unless you've already got an smtp server running on your dev box following something like that will probably be a lot easier.

Upvotes: 3

Related Questions