-1

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)
    }
}
  • 2
    We could write the same comments here as to your [previous question](https://stackoverflow.com/questions/69626169/how-to-send-a-message-using-a-contact-form-in-golang). Making new account and repeating a slightly different question won't change the fact that you can't send emails in someone else's name without knowing their password. – icza Oct 20 '21 at 06:53
  • @icza If it can't then how other websites are doing it? Is there any other way? –  Oct 20 '21 at 06:57
  • @CeriseLimón I added the import packages. –  Oct 20 '21 at 06:58
  • 1
    Other websites don't do it either. – icza Oct 20 '21 at 06:59
  • @icza Look at the sites that are using the contact form. –  Oct 20 '21 at 07:03
  • 1
    Those sites don't send emails in the user's name. They send an internal message which contains the email address of the user, to where email replies can be sent. – icza Oct 20 '21 at 07:05
  • @icza I didn't understand clearly. Will you please elaborate? If there is another way how can I apply it? Thank you! –  Oct 20 '21 at 07:10
  • Contact forms present a form to the users. Users can enter their message, and provide their email address. When this contact form is submitted, it only saves a message in the webapp's own database, which the admins or the appropriate persons can read. You provided your emails, so if they have a response, they can write you an email. They don't send emails _from_ your email address, but _to_ your email address. – icza Oct 20 '21 at 07:17
  • @icza Now I understood that a person has to send an email to my email address instead of just filling the form that will only send a message. I have two questions. **1.** How can I make this type of functionality to just receive messages. **2.** If I only get a message by the submission of a contact form then how can I know the person's email address to reply. Is there any other way of doing it? –  Oct 20 '21 at 07:31
  • See https://www.rfc-editor.org/rfc/rfc5322#section-3.6.2 for a description of how to set the message headers for you scenario. – Charlie Tumahai Oct 20 '21 at 08:13

1 Answers1

0

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.

icza
  • 389,944
  • 63
  • 907
  • 827
  • You said **On submit, the backend saves this 'message' in the webapps own database.**. Is this message will just be a body or email also. Because if the email is not saved in the database then how can I find and use it to reply back? –  Oct 20 '21 at 13:29
  • @bundaloti Yes, obviously the backend saves everything the user submits in the contact form, so everything is available when email is to be sent. – icza Oct 20 '21 at 13:43
  • I will prefer using Redis as an inside memory to save the data. What do you think a database is preferred or Redis? –  Oct 20 '21 at 13:51
  • @bundaloti For this purpose, both are totally fine. Giving the nature of the task, I'd prefer database. – icza Oct 20 '21 at 14:09
  • Thank you! I applied your method and it is working correctly and giving me the target email address in the **reply-to** section of the email. –  Oct 20 '21 at 14:15
  • @bundaloti Glad to hear it. If this answers your question, consider accepting and upvoting the answer. – icza Oct 20 '21 at 14:17
  • Sure. I am doing it. –  Oct 20 '21 at 14:20
  • I am successfully sending emails without using a database. What is the purpose of using it if the data is present in the email? –  Oct 20 '21 at 16:37
  • @bundaloti You can search / filter previous messages, make statistics. If all these are no value to you, and you send the email on contact form submit, you don't need to save into the database. Also note that there is no 100% guarantee emails are delivered, and if they are lost, saved messages would be the only track. – icza Oct 20 '21 at 16:43