I have a contact form, through which I receive messages. But it is received only from one email address because I only gave that email address in the abc.SetHeader("To", "[email protected]")
.
I want to receive messages from all the people who want to contact me but I have to know their app password also to put it in the code. This is not going to happen.
That's why I gave my another email address in the Reply-To
section in my Gmail account to make it work but it still does not work. What should I do next to make it work?
package main
import (
"log"
"gopkg.in/gomail.v2"
)
func main() {
abc := gomail.NewMessage()
abc.SetHeader("From", "[email protected]")
abc.SetHeader("To", "[email protected]")
abc.SetHeader("Subject", "This is the subject")
abc.SetBody("text/plain", "This is the message")
a := gomail.NewDialer("smtp.gmail.com", 587, "[email protected]", "app password") // Password for "[email protected]"
if err := a.DialAndSend(abc); err != nil {
log.Fatal(err)
}
}
CodePudding user response:
Let's clear the concept of "contact forms".
Web sites usually provide a "Contact us" functionality. The website presents a contact form which the user fills. The user usually provides his/her email address, and the message (and preferably the subjuct / topic).
On submit, the backend saves this message in the webapps own database.
When an admin (or an appropriate person) reads the message (in a restricted page), the admin may decide to reply to this message. Since the user provided his/her email address when submitting the contact form, the reply may happen via email. The admin may fill a form which includes the reply message (preferably quoting the original message too), and when the admin submits this form, the backend can send an email to the address provided by the user (when submitting the contact form).
This email will contain the message entered by the admin as the body. The subject should contain the subject from the contact form as the Subject header. This email will be sent to the address provided by the user. The email will be sent from the address of the admin (or any email address set in the backend, but certainly not from the email provided by the user).
Example of sending a reply email by the admin (from the backend):
m := gomail.NewMessage()
m.SetHeader("From", "[email protected]")
m.SetHeader("To", "[email protected]")
m.SetHeader("Subject", "Re: Issue with purchase")
m.SetBody("text/plain", "Hello Bob! We fixed the issue!")
d := gomail.NewDialer("smtp.mywebapp.com", 587, "admin", "admin's password")
if err := d.DialAndSend(m); err != nil {
panic(err)
}
Note that when the contact form is filled and submitted by the user, the backend may also notify the admin(s) via email about the message. The backend may send an email to the admin(s) including the message, and the "reply to" header of that email may be set to the email address provided by the user. This email will be sent by the backend, from the admin's email address (or any other address set in the backend, but again, not from the user's email address). If this email sent to the admin has the "reply to" header set to the user's email address, the admin may simply reply to the email, and the reply will be directly sent to the user's email address.
Example of an email sent to the admin about a "contact form" submit (sent by the backend):
m := gomail.NewMessage()
m.SetHeader("From", "[email protected]")
m.SetHeader("To", "[email protected]")
m.SetHeader("Reply-To", "[email protected]")
m.SetHeader("Subject", "Issue with purchase")
m.SetBody("text/plain", "Hi, I'm bob. I have this XXX issue when purchasing.")
d := gomail.NewDialer("smtp.mywebapp.com", 587, "admin", "admin's password")
if err := d.DialAndSend(m); err != nil {
panic(err)
}
The admin reads this message in his/her own email client, and hits "Reply". The reply message will go directly to [email protected]
.