1

I am trying to send email using gomail and need to set the send-as emailed address. I found the link to set the reply-to address, which is close, but not exactly what I'm looking for.

Send-as is a function that I know GMail supports, others prob do as well. I have send-as already configured in GMail itself and working correctly, just trying to see if I can set it via gomail

I have tried the following:

Read through the docs for gomail

m.SetHeader("SendAs", emailAddress)
m.SetHeader("Send-As", emailAddress)
m.SetHeader("sendAs", emailAddress)
m.SetAddressHeader("SendAs", emailAddress, "")
m.SetAddressHeader("Send-As", emailAddress, "")
m.SetAddressHeader("sendAs", emailAddress, "")
func TestEmail(to, from, cc, bcc, subject, message, password, fileName string) (string, error) {
    // We need to parse the TO, CC, and BCC lists, which may contain more than one email address each.
    splitToField := strings.Split(to, ",")
    splitCCField := strings.Split(cc, ",")
    splitBCCField := strings.Split(bcc, ",")

    m := gomail.NewMessage()
    m.SetHeader("From", from)
    m.SetHeader("To", splitToField...)
    // If there is a CC address(s), then add them.
    if len(cc) > 0 {
        fmt.Println("CC LEN > 0", len(splitCCField))
        m.SetHeader("Cc", splitCCField...)
    }
    // If there is a BCC address(s), then add them.
    if len(bcc) > 0 {
        fmt.Println("BCC LEN > 0", len(splitBCCField))
        m.SetHeader("Bcc", splitBCCField...)
    }
    m.SetHeader("Subject", subject)
    m.SetBody("text/html", message)
    m.Attach("emailedQuotes/"+fileName)

    // So far, we configured this to ONLY work with GMail accounts.
    // Possibly in the future we can add an input on the front end and have them enter
    // their host/port manually. Or get fancy and parse the email address and have the most common
    // types in a struct.
    d := gomail.NewDialer("smtp.gmail.com", 587, from, password)

    err := d.DialAndSend(m)
    if err != nil {
        fmt.Println("ERROR SENDING EMAIL!", err)
        return "", err
    } else {
        fmt.Println("Email successfully sent to: ", to)
        return "Email successfully sent to:" + to, nil
    }
}

My expectation is that you'll be able to enter in the username/password for the from address and be able to send mail with the from address showing as the send-as address.

If I execute this (with the correct username/password for the from address) it will fire the email correctly, but the send-as doesn't take over the from address. So, doesn't work, but no errors.

  • I've never encountered a 'Send-As' header in emails - where is this documented? Are you sure it exists? Normally extensions to headers have an X- prefix. Also, there is no mention of this in google docs that I can see. You simply set the from address in your client (in this case gomail) and if setup it should work. https://support.google.com/mail/answer/22370 – Kenny Grant Jun 01 '19 at 20:47
  • Found the documentation here: https://developers.google.com/gmail/api/v1/reference/ Search for 'send-as'. – Maxwell Morin Jun 05 '19 at 16:22

1 Answers1

1

The docs for the google api you linked do not talk about setting a mime header - there is no such thing as far as I can see. So setting a "SendAs" mime header on the email is not going to have any effect.

You are using gomail - the github repo for it states 'Gomail can only send emails using an SMTP server' - that means it's not using the api you have linked (which has nothing to do with mime headers).

What may work for you instead is simply setting the from address to the address you have already set up on your google account as a sender, and using gomail to send via your google account.

Kenny Grant
  • 9,360
  • 2
  • 33
  • 47