【问题标题】:Google Apps Script send email to you as the end userGoogle Apps 脚本以最终用户身份向您发送电子邮件
【发布时间】:2018-08-05 07:25:46
【问题描述】:

我正在尝试在 Google Apps 脚本中构建一个 REST API,联系人页面应连接到该 API,该 API 向我或其他联系人发送电子邮件。 Google 脚本在我的 Google Suite 帐户下,到目前为止,看起来像这样:

/**
 * Sends contact email, on behalf of the client, to an email of Open Source Roads.
 * @param data - string : a stringified object with the following fields:
 *  - sender
 *  - subject
 *  - message
 *  - recipient
 **/
function sendContactEmail(data) {
  var emailData = JSON.parse(data) || {};
  Logger.log(Object.keys(emailData).length !== 0)
  // if there's no sender, reject it right away
  if ((!emailData.sender) || (typeof(emailData.sender) !== 'string') && (!emailData.sender.email)) throw forbidden('Sender missing');
  // if there's no recipient, reject it right away
  if ((!emailData.recipient) || ((typeof(emailData.recipient) !== 'string') && (!emailData.recipient.email))) throw forbidden('Recipient missing');
  // if there's no subject or message, it's a bad request
  if (!emailData.subject) throw invalidData('Subject missing');
  if (!emailData.message) throw invalidData('Message missing');
  // validate sender,recipient (these will be emails!)
  var sender = retrievePropFrom(emailData, 'sender'),
    recipient= retrievePropFrom(emailData, 'recipient');
  if (!isValidEmail(sender)) throw invalidData("sender must contain a valid email")
  if (!isValidEmail(recipient)) throw invalidData("recipient must contain a valid email")
  // TODO: additional sanitation on emailData.subject,emailData.body?
  // send that email message!
  MailApp.sendEmail(recipient, 
    sender,
    emailData.subject,
    emailData.message
  )
}


function makeError(message, code) { 
  var error = new Error(message);
  error.code = code;
  return error;
}
makeError.FORBIDDEN    = 403;
makeError.UNAUTHORIZED = 401;
makeError.BAD_REQUEST  = 400;

function forbidden(message) { 
  return makeError(message, makeError.FORBIDDEN);
}

function invalidData(message) { 
  return makeError(message, BAD_REQUEST);
}

function isValidEmail(email) { 
  return /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test(email)
}

/**
 * retrieves the specified property from `obj`. If prop is for either sender or recipient, things get a 
 * little more interesting: 
 * - if the value at the requested `prop` is a string, we return it, like any other `prop`
 * - else if the value at that `prop` contains `email`, we return that
 * 
 **/
function retrievePropFrom(obj, prop) { 
  if (((prop !== "sender")  && (prop !== "recipient")) || (obj[prop] === obj[prop].toString())) 
    return obj[prop];
  if (obj[prop].email) return obj[prop].email;
  return undefined;
}

如果我sendContactEmail 以我自己为sender 和我的个人帐户(不同于脚本所有者)为recipientMailApp 发送电子邮件,我可以在几秒钟内验证它。然而,当我去尝试我的实际用例时,让脚本拥有电子邮件为recipient,另一封电子邮件为sender,我什么也得不到。

read the documentation,没有注意到这是一件事。我该如何解决这个问题?!

【问题讨论】:

  • 这个简单的例子甚至不适合我:developers.google.com/gmail/markup/apps-script-tutorial
  • 请说明您遇到的具体问题是什么
  • 我可以使用上述脚本将电子邮件发送到另一个帐户,但反之则不行。我正在尝试将此脚本设置为 API,以便以最终用户的身份向我发送电子邮件(访问我的页面、填写联系表并单击“保存”的人)
  • 我尝试从 PHP 中执行此操作,但也没有成功。我的电子邮件似乎有问题,例如它不接受程序化电子邮件。不知道如何解决这个问题,更不用说是什么原因造成的了。 (我的电子邮件是 GSuite 电子邮件。)
  • MailApp 始终从执行脚本的权限的 Gmail 帐户发送电子邮件 - 您无法更改或设置发件人,只能更改或设置 reply-to。因此,如果他们不授权您的脚本以他们的身份运行(即您作为 web 应用程序发布并以用户访问的身份执行),那么您将无法从他们的地址发送电子邮件。 PS:考虑编辑您的问题以明确说明您的问题

标签: google-apps-script


【解决方案1】:

我能够解决问题。

出了什么问题?

该问题已通过尝试通过其他编程方式(包括我自己和recipients 中的另一封电子邮件,用 PHP 编写脚本)向自己发送消息来确认,这意味着它必须是我的域电子邮件设置不正确

你是怎么解决的?

因此,我与 GSuite 支持专家通了电话,得知我的域(我的电子邮件所在的域)没有任何 MX 记录,更不用说 Google 的任何记录了。我去了域提供商(在我的例子中是 AWS Route 53),并通过添加这些 MX 记录来解决这个问题:

1 ASPMX.L.GOOGLE.COM.

5 ALT1.ASPMX.L.GOOGLE.COM.

5 ALT2.ASPMX.L.GOOGLE.COM.

10 ALT3.ASPMX.L.GOOGLE.COM.

10 ALT4.ASPMX.L.GOOGLE.COM.

More info here。完成此操作并等待一个小时以传播网络更改后,通过运行我的脚本的所有时间发送的所有电子邮件最终都到达了我的电子邮件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-21
    • 2013-08-15
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    相关资源
    最近更新 更多