英文:
How to receive messages from all the people who fill the contact form?
问题
我有一个联系表单,通过它我可以收到消息。但是只能从一个电子邮件地址接收,因为我只在 abc.SetHeader("To", "email2@gmail.com")
中提供了该电子邮件地址。
我想要接收所有想要与我联系的人的消息,但是我必须知道他们的应用程序密码,并将其放入代码中。这是不可能的。
这就是为什么我在我的 Gmail 账户的 Reply-To
部分中提供了另一个电子邮件地址,以使其工作,但它仍然不起作用。下一步我该怎么做才能使其工作?
package main
import (
"log"
"gopkg.in/gomail.v2"
)
func main() {
abc := gomail.NewMessage()
abc.SetHeader("From", "email1@gamil.com")
abc.SetHeader("To", "email2@gmail.com")
abc.SetHeader("Subject", "This is the subject")
abc.SetBody("text/plain", "This is the message")
a := gomail.NewDialer("smtp.gmail.com", 587, "email1@gmail.com", "app password") // "email1@gmail.com" 的密码
if err := a.DialAndSend(abc); err != nil {
log.Fatal(err)
}
}
英文:
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", "email2@gmail.com")
.
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", "email1@gamil.com")
abc.SetHeader("To", "email2@gmail.com")
abc.SetHeader("Subject", "This is the subject")
abc.SetBody("text/plain", "This is the message")
a := gomail.NewDialer("smtp.gmail.com", 587, "email1@gmail.com", "app password") // Password for "email1@gmail.com"
if err := a.DialAndSend(abc); err != nil {
log.Fatal(err)
}
}
答案1
得分: 0
让我们澄清一下"联系表单"的概念。
网站通常提供"联系我们"功能。网站呈现一个联系表单,用户填写该表单。用户通常提供他/她的电子邮件地址和消息(最好还有主题/主题)。
提交后,后端将此消息保存在Web应用程序自己的数据库中。
当管理员(或适当的人员)在受限页面上阅读消息时,管理员可以决定回复此消息。由于用户在提交联系表单时提供了他/她的电子邮件地址,回复可以通过电子邮件进行。管理员可以填写一个包含回复消息的表单(最好同时引用原始消息),当管理员提交此表单时,后端可以向用户提供的地址发送电子邮件(在提交联系表单时)。
此电子邮件将包含管理员输入的消息作为正文。主题应包含联系表单中的主题作为主题头。此电子邮件将发送至用户提供的地址。电子邮件将发送自管理员的地址(或在后端设置的任何电子邮件地址,但肯定不是来自用户提供的电子邮件地址)。
管理员通过后端发送回复电子邮件的示例:
m := gomail.NewMessage()
m.SetHeader("From", "admin@mywebapp.com")
m.SetHeader("To", "bob@gmail.com")
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)
}
请注意,当用户填写并提交联系表单时,后端还可以通过电子邮件通知管理员有关该消息。后端可以向管理员发送一封包含消息的电子邮件,并将该电子邮件的**"回复"头设置为用户提供的电子邮件地址。此电子邮件将由后端从管理员的电子邮件地址(或后端设置的任何其他地址)发送,但再次强调,不是来自用户的电子邮件地址。如果发送给管理员的此电子邮件的"回复"**头设置为用户的电子邮件地址,管理员可以直接回复该电子邮件,回复将直接发送到用户的电子邮件地址。
关于"联系表单"提交的电子邮件发送给管理员的示例(由后端发送):
m := gomail.NewMessage()
m.SetHeader("From", "admin@mywebapp.com")
m.SetHeader("To", "admin@mywebapp.com")
m.SetHeader("Reply-To", "bob@gmail.com")
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)
}
管理员在自己的电子邮件客户端中阅读此消息,并点击"回复"。回复消息将直接发送到bob@gmail.com
。
英文:
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", "admin@mywebapp.com")
m.SetHeader("To", "bob@gmail.com")
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", "admin@mywebapp.com")
m.SetHeader("To", "admin@mywebapp.com")
m.SetHeader("Reply-To", "bob@gmail.com")
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 bob@gmail.com
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论