douzhang2092 2015-07-07 15:18
浏览 111

如何从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
", msg.Header.Get("From"))
  fmt.Printf("Subject: %v
", mime.GetHeader("Subject"))
  fmt.Printf("Text Body: %v chars
", len(mime.Text))
  fmt.Printf("HTML Body: %v chars
", len(mime.Html))
  fmt.Printf("Inlines: %v
", len(mime.Inlines))
  fmt.Printf("Attachments: %v
", len(mime.Attachments))
  fmt.Println(mime.Attachments)
  fmt.Println(mime.OtherParts)
  fmt.Printf("Attachments: %v
", 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}
  • 写回答

1条回答 默认 最新

  • duanlou2917 2015-07-07 16:28
    关注

    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("sender@example.org")
        c.Rcpt("recipient@example.net")
        // 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.

    评论

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站