Google表单电子邮件通知使用别名而不是gmail地址

时间:2017-07-14 15:46:08

标签: google-apps-script

在这里找到完美的脚本: https://github.com/Markei/Google-Forms-Confirmation-and-thank-you-mailer/blob/master/Code.gs



var ADDON_TITLE = 'Confirmation and thank you mailer';

function onOpen(e) {
  FormApp.getUi()
      .createAddonMenu()
      .addItem('Setup', 'showSetup')
      .addItem('About', 'showAbout')
      .addToUi();
}

function onInstall(e) {
  onOpen(e);
}

function showSetup() {
  var ui = HtmlService.createHtmlOutputFromFile('Setup')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME)
      .setTitle(ADDON_TITLE);
  //FormApp.getUi().showModalDialog(ui, 'Setup ' + ADDON_TITLE);
  FormApp.getUi().showSidebar(ui);
}

function showAbout() {
  var ui = HtmlService.createHtmlOutputFromFile('About')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME)
      .setWidth(420)
      .setHeight(270);
  FormApp.getUi().showModalDialog(ui, 'About ' + ADDON_TITLE);
}

function saveSettings(settings) {
  var form = FormApp.getActiveForm();
  var triggers = ScriptApp.getUserTriggers(form);

  // determine if the addon is active
  settings.active = (settings.emailField !== '' && settings.subject !== '' && settings.body !== '');
  
  // save the settings to google service
  PropertiesService.getDocumentProperties().setProperties(settings);
  
  // find the current trigger if set
  var trigger = null;
  for (var i = 0; i < triggers.length; i++) {
    if (triggers[i].getEventType() == ScriptApp.EventType.ON_FORM_SUBMIT && triggers[i].getHandlerFunction() === 'respondToFormSubmit') {
      trigger = triggers[i];
    }
  }
  
  // add or remove trigger if needed
  if (settings.active === true && trigger === null) {
    // not found but add-on is active, create
    var trigger = ScriptApp.newTrigger('respondToFormSubmit')
      .forForm(form)
      .onFormSubmit()
      .create();
  } else if (settings.active === false && trigger !== null) {
    // found but add-on is deactivated, remove
    ScriptApp.deleteTrigger(trigger);
  }
}

function getSettings() {
  var settings = PropertiesService.getDocumentProperties().getProperties();
  settings.active = settings.active || false;

  // create a list of all the questions/form items for placeholder insertion in the setup page
  var formQuestions = FormApp.getActiveForm().getItems(); // FormApp.ItemType.TEXT
  settings.formQuestions = [];
  for (var i = 0; i < formQuestions.length; i++) {
    settings.formQuestions.push({
      title: formQuestions[i].getTitle(),
      id: formQuestions[i].getId(),
      type: formQuestions[i].getType()
    });
  }
  
  // add map of form types
  settings.formTypes = {
    'TEXT': FormApp.ItemType.TEXT
  };
  
  return settings;
}

function respondToFormSubmit(e) {
  var settings = PropertiesService.getDocumentProperties();
  var authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL);
  // check if authorization is oke
  if (authInfo.getAuthorizationStatus() == ScriptApp.AuthorizationStatus.REQUIRED) {
    sendReauthorizationRequest();
    return;
  }
  // check if addon is setup
  if (settings.getProperty('active') == false) {
    return;
  }
  // check mail quota
  if (MailApp.getRemainingDailyQuota() == 0) {
    return;
  }

  // get form
  var formResponse = e.response;
  var form = FormApp.getActiveForm();
  
  // info for e-mail
  var toAddress = formResponse.getResponseForItem(form.getItemById(settings.getProperty('emailField'))).getResponse();
  var subject = settings.getProperty('subject');
  var ccAddresses = settings.getProperty('ccAddress');
  var message = settings.getProperty('body');
  var extraOptions = {name: form.getTitle()};
  
  // replace placeholders in subject, cc, message
  for (var i = 0; i < form.getItems().length; i ++) {
    var formItem = form.getItems()[i];
    subject = subject.replace('{{' + formItem.getId() + ':' + formItem.getTitle() + '}}', formResponse.getResponseForItem(formItem).getResponse());
    ccAddresses = ccAddresses.replace('{{' + formItem.getId() + ':' + formItem.getTitle() + '}}', formResponse.getResponseForItem(formItem).getResponse());
    message = message.replace('{{' + formItem.getId() + ':' + formItem.getTitle() + '}}', formResponse.getResponseForItem(formItem).getResponse());
  }
  
  // attach cc if set
  if (ccAddresses !== null && ccAddresses !== '') {
    extraOptions.cc = ccAddresses;
  }
  
  // and mail
  MailApp.sendEmail(toAddress, subject, message, extraOptions);
}

function sendReauthorizationRequest() {
  var settings = PropertiesService.getDocumentProperties();
  var authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL);
  var lastAuthEmailDate = settings.getProperty('lastAuthEmailDate');
  var today = new Date().toDateString();
  if (lastAuthEmailDate != today) {
    if (MailApp.getRemainingDailyQuota() > 0) {
      var template = HtmlService.createTemplateFromFile('AuthorizationEmail');
      template.addon_title = ADDON_TITLE;
      var message = template.evaluate();
      MailApp.sendEmail(Session.getEffectiveUser().getEmail(), 'Authorization Required for the ' + ADDON_TITLE + ' add-on', message.getContent(), { name: ADDON_TITLE, htmlBody: message.getContent() });
    }
    settings.setProperty('lastAuthEmailDate', today);
  }
}
&#13;
&#13;
&#13;

但有人可以告诉我在哪里可以输入我发送的:alias而不是使用我的普通电子邮件帐户发送消息。我正在尝试创建一个Google表单电子邮件通知,该通知会将表单副本发送给提交者,并将副本发送给客户服务,仅保留提交者和客户服务电子邮件地址,而不是@ gmail.com地址。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您可以设置发件人姓名和回复地址,如下所示:

     MailApp.sendEmail("whoyoursendingtheemailto@gmail.com", "email subject", "body of email", {
      name: "mytestname", //the name of the sender
      replyTo: "blah@blah.com" //the replyto address
     })