Google Apps脚本以最终用户身份向您发送电子邮件

时间:2018-08-05 07:25:46

标签: google-apps-script

我正在尝试在Google Apps脚本中构建一个REST 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,我的个人帐户(与脚本所有者不同)为recipient,则MailApp发送电子邮件,我可以在几秒钟内验证它。但是,当我尝试实际使用的情况时,脚本拥有的电子邮件为recipient,而另一个电子邮件为sender,则我什么也没得到。

read the documentation,并且没有注意到这是一回事。我该如何解决?!

1 个答案:

答案 0 :(得分:2)

我能够解决此问题。

出了什么问题?

该问题已通过尝试通过其他编程方式(包括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。完成此操作并等待一个小时以使网络更改传播后,运行脚本的所有这些时间发送的所有电子邮件最终都到达了我的电子邮件。