【发布时间】: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 和我的个人帐户(不同于脚本所有者)为recipient,MailApp 发送电子邮件,我可以在几秒钟内验证它。然而,当我去尝试我的实际用例时,让脚本拥有电子邮件为recipient,另一封电子邮件为sender,我什么也得不到。
我read the documentation,没有注意到这是一件事。我该如何解决这个问题?!
【问题讨论】:
-
这个简单的例子甚至不适合我:developers.google.com/gmail/markup/apps-script-tutorial
-
请说明您遇到的具体问题是什么
-
我可以使用上述脚本将电子邮件发送到另一个帐户,但反之则不行。我正在尝试将此脚本设置为 API,以便以最终用户的身份向我发送电子邮件(访问我的页面、填写联系表并单击“保存”的人)
-
我尝试从 PHP 中执行此操作,但也没有成功。我的电子邮件似乎有问题,例如它不接受程序化电子邮件。不知道如何解决这个问题,更不用说是什么原因造成的了。 (我的电子邮件是 GSuite 电子邮件。)
-
MailApp始终从执行脚本的权限的 Gmail 帐户发送电子邮件 - 您无法更改或设置发件人,只能更改或设置reply-to。因此,如果他们不授权您的脚本以他们的身份运行(即您作为 web 应用程序发布并以用户访问的身份执行),那么您将无法从他们的地址发送电子邮件。 PS:考虑编辑您的问题以明确说明您的问题