首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google脚本作为最终用户向您发送电子邮件

问Google脚本作为最终用户向您发送电子邮件
EN

Stack Overflow用户
提问于 2018-08-05 07:25:46
回答 1查看 457关注 0票数 2

我正试图在Google脚本中构建一个REST,这是一个联系人页面应该连接到的,它发送电子邮件给我或其他联系人。Google脚本在我的Google帐户下,到目前为止,如下所示:

代码语言:javascript
复制
/**
 * 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,我什么也得不到。

我阅读文件,并且没有注意到这是一件事。我怎么才能避开这一切?!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-06 01:59:08

我解决了这个问题。

出了什么问题?

这个问题通过尝试通过其他编程方式(包括我自己和recipients中的另一封电子邮件,用PHP编写脚本)向自己发送消息而得到证实,这意味着必须是我的域电子邮件没有正确设置。

你怎么修好的?

因此,我和一位GSuite支持专家跳过电话,得知我的电子邮件所在的域名没有MX记录,更别提谷歌的任何一条了。我去了域名提供商(在我的例子中,是AWS路由53),通过添加这些MX记录来修正这个问题:

代码语言:javascript
复制
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.

这里有更多的信息。在完成此操作并等待一个小时的网络更改后,所有通过运行我的脚本发送的电子邮件最终都到达了我的电子邮件。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51692296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档