Google apps script Google应用程序脚本以最终用户身份向您发送电子邮件
我正试图在谷歌应用程序脚本中构建一个RESTAPI,联系人页面将连接到该API,向我或其他联系人发送电子邮件。Google脚本位于我的Google Suite帐户下,目前为止,看起来如下所示:Google apps script Google应用程序脚本以最终用户身份向您发送电子邮件,google-apps-script,Google Apps Script,我正试图在谷歌应用程序脚本中构建一个RESTAPI,联系人页面将连接到该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
/**
* 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
,我自己作为发送者
,我的个人帐户(与脚本所有者不同)作为接收者
,MailApp
发送电子邮件,我可以在几秒钟内验证。然而,当我去尝试我的实际用例时,让脚本拥有的电子邮件作为收件人
,而另一封电子邮件作为发件人
,我什么也得不到
我,没有注意到这是一件事。我怎么才能避开这个 我能够解决这个问题 问题出在哪里? 通过尝试通过其他编程方式(包括我自己和
收件人
中的另一封电子邮件,用PHP编写脚本)向自己发送消息证实了该问题,这意味着必须是我的域电子邮件设置不正确
您是如何修复的?
因此,我与一位GSuite支持专家通了电话,得知我的域名,我的电子邮件所在的域名,没有任何MX记录,更不用说谷歌的了。我找到了域提供商(在我的例子中是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.
。在完成此操作并等待一个小时网络更改传播后,所有通过运行我的脚本发送的电子邮件最终都到达了我的电子邮件。这个简单的示例对我来说根本不起作用:请澄清您遇到的确切问题,我可以使用上述脚本将电子邮件发送到另一个帐户作为我,但反之亦然。我试图将此脚本设置为API,以便作为最终用户(访问我的页面、填写联系人表单并单击“保存”的人)向我发送电子邮件。我尝试从PHP执行此操作,但它也不起作用。我的电子邮件似乎有问题,无法接受编程电子邮件。idk如何解决这个问题,更不用说是什么导致了它。(我的电子邮件是GSuite电子邮件。)
MailApp
始终从Gmail帐户发送电子邮件,脚本是在该帐户的授权下执行的-您不能更改或设置发件人,只有回复
。因此,如果他们不授权您的脚本以他们的身份运行(即,您以Web应用程序的形式发布并以用户访问的身份执行),则您无法从他们的地址发送电子邮件。PS:考虑编辑你的问题,清楚地陈述你的问题。