I'm developing an mail service using golang and I put it on service at google cloud, my app can't send email when it hosted inside gcp, but I can do it in local.
I tried it before in localhost and I got success when sent the email, but when I run it on gcp, it didn't send anything, the log is clear, nothing about the error but log for success. Is there anything should I config in gcp?
This is my code for sending the email:
func (r *Request) SendEmail() (bool, error) {
mime := "MIME-version: 1.0;
Content-Type: text/html;
charset = \"UTF-8\";
"
subject := "Subject: " + r.subject + "!
"
msg := []byte(subject + mime + "
" + r.body)
addr := "smtp.gmail.com:587"
if err := smtp.SendMail(addr, auth, "no-reply@sample.com", r.to, msg); err != nil {
return false, err
}
return true, nil
}
I expect to receive a msg from this mail service
Are you actually sending an e-mail from no-reply@sample.com
? Well Gmail's anti-spam filters are going to catch you assuming the above code works.
sample.com
doesn't have any TXT (i.e. for SPF) or MX records, and your GCE instance is certainly not going to be viewed favourably:
$ host -t txt sample.com.
sample.com has no TXT record
$ host -t mx sample.com.
sample.com has no MX record
If it does actually work, it may be in the spam folder, best case scenario.
If you edited the above e-mail for the code snippet, and you're actually sending from a domain you own, make sure your MX, SPF, and DMARC records are up to scratch. In particular, your SPF record must whitelist the IP of your GCE instance.