smtp在生产环境中无法工作,即使使用凭据也无法进行身份验证。

huangapple go评论121阅读模式
英文:

smtp won't work in production, Unauthenticated even with credentials

问题

我在这里遇到了很大的问题。我的代码在本地实例(OSX)上运行正常,配置如下:

mail.host = smtp.gmail.com
mail.port = 25
mail.from = mysite.com
mail.username = email@gmail.com

但在生产环境(Ubuntu 12.0.1)中显示未经身份验证:

以下是我的代码(你可以在github.com/tanema/revel_mailer找到所有代码)。身份验证错误发生在c.Mail(username)这一行。

func (m *Mailer) Send(mail_args map[string]interface{}) error {
  m.renderargs = mail_args
  pc, _, _, _ := runtime.Caller(1)
  names := strings.Split(runtime.FuncForPC(pc).Name(), ".")
  m.template =  names[len(names)-2] + "/" + names[len(names)-1]

  host, host_ok := revel.Config.String("mail.host")
  if !host_ok {
    revel.ERROR.Println("mail host not set")
  }
  port, port_ok := revel.Config.Int("mail.port")
  if !port_ok {
    revel.ERROR.Println("mail port not set")
  }

  c, err := smtp.Dial(fmt.Sprintf("%s:%d", host, port))
  if err != nil {
    return err
  }

  if ok, _ := c.Extension("STARTTLS"); ok {
    if err = c.StartTLS(nil); err != nil {
      return err
    }
  }

  from, from_ok := revel.Config.String("mail.from") 
  if !from_ok {
    revel.ERROR.Println("mail.from not set")
  }


  username, username_ok := revel.Config.String("mail.username") 
  if !username_ok {
    revel.ERROR.Println("mail.username not set")
  }

  if err = c.Auth(smtp.PlainAuth(from, username, getPassword(), host)); err != nil {
       return err
  }

  if err = c.Mail(username); err != nil {
    return err
  }

  if mail_args["to"] != nil {
    m.to = makeSAFI(mail_args["to"]) //makeSAFI == make string array from interface
  }
  if mail_args["cc"] != nil {
    m.cc = makeSAFI(mail_args["cc"])
  }
  if mail_args["bcc"] != nil {
    m.bcc = makeSAFI(mail_args["bcc"])
  }

  if len(m.to) + len(m.cc) + len(m.bcc) == 0 {
    return fmt.Errorf("Cannot send email without recipients")
  }

  recipients := append(m.to, append(m.cc, m.bcc...)...)
  for _, addr := range recipients {
    if err = c.Rcpt(addr); err != nil {
      return err
    }
  }
  w, err := c.Data()
  if err != nil {
    return err
  }

  mail, err := m.renderMail(w)
  if err != nil {
    return err
  }

  if revel.RunMode == "dev" {
    fmt.Println(string(mail))
  }

  _, err = w.Write(mail)
  if err != nil {
    return err
  }
  err = w.Close()
  if err != nil {
    return err
  }
  return c.Quit()
}

我无法弄清楚问题出在哪里,请帮忙。

编辑:

所以我决定注册Google Apps并设置了域名和电子邮件。这让我相信实际上是我没有更改MX记录。所以我再次尝试使用我的个人电子邮件发送,现在MX记录已经设置好了。没有成功。有没有办法为个人Gmail电子邮件设置MX记录,这样我就不必为Google Apps付费了?

英文:

I am having a hell of a time here. My code works on the local instance (OSX) with this config:

mail.host = smtp.gmail.com
mail.port = 25
mail.from = mysite.com
mail.username = email@gmail.com

But says it is unauthenticated in production (Ubuntu 12.0.1):

Here is my code (and you can find all of it at github.com/tanema/revel_mailer). The authentication error is thrown at the c.Mail(username) line.

func (m *Mailer) Send(mail_args map[string]interface{}) error {
  m.renderargs = mail_args
  pc, _, _, _ := runtime.Caller(1)
  names := strings.Split(runtime.FuncForPC(pc).Name(), ".")
  m.template =  names[len(names)-2] + "/" + names[len(names)-1]

  host, host_ok := revel.Config.String("mail.host")
  if !host_ok {
    revel.ERROR.Println("mail host not set")
  }
  port, port_ok := revel.Config.Int("mail.port")
  if !port_ok {
    revel.ERROR.Println("mail port not set")
  }

  c, err := smtp.Dial(fmt.Sprintf("%s:%d", host, port))
  if err != nil {
    return err
  }

  if ok, _ := c.Extension("STARTTLS"); ok {
    if err = c.StartTLS(nil); err != nil {
      return err
    }
  }

  from, from_ok := revel.Config.String("mail.from") 
  if !from_ok {
    revel.ERROR.Println("mail.from not set")
  }


  username, username_ok := revel.Config.String("mail.username") 
  if !username_ok {
    revel.ERROR.Println("mail.username not set")
  }

  if err = c.Auth(smtp.PlainAuth(from, username, getPassword(), host)); err != nil {
       return err
  }

  if err = c.Mail(username); err != nil {
    return err
  }

  if mail_args["to"] != nil {
    m.to = makeSAFI(mail_args["to"]) //makeSAFI == make string array from interface
  }
  if mail_args["cc"] != nil {
    m.cc = makeSAFI(mail_args["cc"])
  }
  if mail_args["bcc"] != nil {
    m.bcc = makeSAFI(mail_args["bcc"])
  }

  if len(m.to) + len(m.cc) + len(m.bcc) == 0 {
    return fmt.Errorf("Cannot send email without recipients")
  }

  recipients := append(m.to, append(m.cc, m.bcc...)...)
  for _, addr := range recipients {
    if err = c.Rcpt(addr); err != nil {
      return err
    }
  }
  w, err := c.Data()
  if err != nil {
    return err
  }

  mail, err := m.renderMail(w)
  if err != nil {
    return err
  }

  if revel.RunMode == "dev" {
    fmt.Println(string(mail))
  }

  _, err = w.Write(mail)
  if err != nil {
    return err
  }
  err = w.Close()
  if err != nil {
    return err
  }
  return c.Quit()
}

I cannot figure out what the problem is; please help.

EDIT:

So I bit the bullet and signed up for google apps and set it all up with the domain and email. That lead me to believe that it was actually the MX records that I didnt change. So I tested out sending with my personal email again now that the MX records are set. No Luck. Any idea if I can setup the MX records for a personal gmail email so I dont have to pay for google apps?

答案1

得分: 3

我开发了一个 Gmail 前端,并遇到了类似的问题。请尝试以下方法:

  1. 尝试使用 ssl://smtp.gmail.com 和端口 465。

  2. 登录您的 Gmail 帐户,查看是否显示类似于“未经授权的第三方尝试访问此帐户”的消息。如果是这样,请授权您的应用程序允许您登录。

英文:

I developed a Gmail front-end and encountered a similar problem. Try the following:

  1. Try using ssl://smtp.gmail.com and port 465.

  2. log into your GMail account to see if it says something like 'An unauthorised third-party tried to accesss this account'. If you do, then grant your app permission to log you in.

huangapple
  • 本文由 发表于 2013年7月18日 06:59:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/17711724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定