1
votes

For some reason I cannot figure out how to send emails using a gmail account, Appengine and Golang.

Here's what I've done:

I went to Google Cloud Platform > Appengine > Settings > Select Project and I added the gmail account on Email API authorized senders.

I' tried to make this work using the code from (https://golang.org/pkg/net/smtp/#pkg-examples) (func SendMail)


package main

import (

    "log"
    "net/smtp"
)

func main() {

    // Set up authentication information.
    auth := smtp.PlainAuth("", "[email protected]", "password", "smtp.gmail.com")

    // Connect to the server, authenticate, set the sender and recipient,
    // and send the email all in one step.
    to := []string{"[email protected]"}
    msg := []byte("To: [email protected]\r\n" +
        "Subject: discount Gophers!\r\n" +
        "\r\n" +
        "This is the email body.\r\n")
    err := smtp.SendMail(smtp.gmail.com:587", auth, "[email protected]", to, msg)
    if err != nil {
        log.Fatal(err)
    }
}

On the front-end (JavaScript) I get an unsuccessful response after trying to run this code.

I've been running this on the appengine staging server

I tried different smtp server, ports, users and it still not work (support.google.com/a/answer/176600?hl=en)

I found a few examples on github and some other blog and I tried them but it didn't make different. github.com/golang/go/wiki/SendingMail

nathanleclaire.com/blog/2013/12/17/sending-email-from-gmail-using-golang/

On all the examples it everything looks straight forward but there's something that I'm definitely missing or misunderstanding.

1
FYI: I've just successfully sent an email with your code using my gmail account.oharlem
Did you use the code locally or on Google Appengine. There are few things that work slightly different on App engine.Moisés Colón Ponce
locally. hm... then my only guess is ports. Will telnet smtp.gmail.com 587 work from your instance?oharlem
I tried it but it won't telnet smtp.gmail.com 587 from the appengine container. I know that there's a gmail api on Google Cloud Platform but I was hoping to send email in a simple way using smtp before trying to there.Moisés Colón Ponce
Yeah, that's what I thought. Additionally, I see that app engine does not allow standard ports (cloud.google.com/compute/docs/tutorials/sending-mail ).oharlem

1 Answers

0
votes

There're some limitations with establishing raw tcp connections on GAE: https://cloud.google.com/appengine/docs/go/sockets/#limitations_and_restrictions

I would recommend to use GAE mail api (which is slightly diffrent from standart smtp package) to send emails: https://cloud.google.com/appengine/docs/go/mail/sending-receiving-with-mail-api