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?