dsfhd78978 2013-07-17 22:59
浏览 38

smtp无法在生产环境中使用,即使具有凭据也未经身份验证

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条回答 默认 最新

  • ds2010630 2013-07-17 23:42
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题