0

I have a script which is working for automatic email sending upon form submission.

User submit form > Email send out to specified email address from my own email that I used to setup the script.

Is there any workaround to have the email sender to specify to be the form submitter?

I look at the MailApp class specification. The best I could find is specifying a noreply as 'true'. https://developers.google.com/apps-script/reference/mail/mail-app#sendEmail(String,String,String,Object)

I want to tweak for my script to play a role as invisible middle man to send the email on form submitter's behalf, and also to discourage replies to the email that I used to setup the script.

Thanks

unacorn
  • 827
  • 10
  • 27

2 Answers2

1

It is possible if the submitter is from your domain

  • In this case, you can use a service account with domain-wide impersonation that allows to run the script on behalf of the specified user.
  • Thereby, the user can be anyone in your domain and the impersonated user can be implemented dynamically into the code
  • This is a library that allows you to implement the authentication with a service account in Apps Script
  • If the suers are not in your domain, they cannot be impersonated by the service account, however, you can still send the emails on behalf of a dummy user on your domain, rather than from your primary email.

See here for a sample on how to sue a service account with impersonation in Apps Script.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • thanks @ziganotschka, i look at your sample but can't really understand how do I use a service account to allow the script to run on behalf of a specificied email address (i saw that was to verify either a list of domain users). (pardon me i'm a beginner) In this case, how can I impersonate with a service account and run the script? (Is that the same as trying to login into another google account with generic or service email and implement the script in that google account? – unacorn Dec 09 '20 at 09:06
  • `function getService(user)` creates an authentication flow for the service account on behalf of the specified `user`. The `user` is set in the line `.setSubject(user)`. Now, if your perform your requests like e.g. sending emails with the `UrlfetchApp` authenticating with `service.getAccessToken()` - any request you perform in this way will run authenticated as the specified `user`, so the sender email will be his. – ziganotschka Dec 09 '20 at 09:13
  • 1
    thanks for the clarification! I tried these 2 days but couldn't get it to work (probably really lack of the required oAuth expertise to understand services and authentication etc.) I found a workaround that works by using Gmail.sendEmail with my other group alias. Uupvoting you as I think this might be the working way for others though I wasn't able to understand it well enough to utilize it, thanks for your input! – unacorn Dec 11 '20 at 17:07
0

I found a workaround to send with another group alias email. Instead of using MailApp class, I combine the usage of GmailApp class.

Prerequisite: adding group alias email as 'Send as' in your Gmail. How-to here

After adding the send as alias, use GmailApp.sendEmail with the option to indicate from. Step:

I'm skipping the steps of form submitting, triggers and retrieving values as this question is not about how-to do those. The steps below assumed all the steps done until retrieved email body to prepare for the email sending

  1. add group alias email as 'Send as' in your Gmail (see prerequisite above).
  2. test whether you can send an email in a particular group account email. (huge credit to this post). Code:

    var aliases = GmailApp.getAliases();
    var num = aliases.length-1;
    if (num<0){
        return false
      }else{
        for (var i = 0;i <= num;i++){
          if (aliases[i] == "yourGroup@Domain.com"){
            var myGroupMail=aliases[i];
            break;
          }
        }
      }
      if (myGroupMail != "yourGroup@Domain.com"){return false}
  1. retrieve email html body if you want to send in html. For example, you can retrieve a template from a google doc with DocumentApp.openByUrl and getId() and convert doc to HTML via code:

function docToHtml(docId) {

  // Downloads a Google Doc as an HTML string.
  var url = "{{insert your doc url}}" +
            docId + "&exportFormat=html";
  var param = {
    method: "get",
    headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
    muteHttpExceptions: true,
  };
  return UrlFetchApp.fetch(url, param).getContentText();
}
  1. Finally send your email :)

sendEmail(recipient, EMAIL_SUBJECT, '', {
    cc: email,
    htmlBody: {{insert your create Email Body Function or use the doctohtml code above}},
    from: myGroupMail
  }
unacorn
  • 827
  • 10
  • 27