Email 可以通过URL从谷歌发送邮件吗?
我找不到任何有文档记录的方法来完成通过GMAIL API发送电子邮件,使用url(获取请求),类似这样:Email 可以通过URL从谷歌发送邮件吗?,email,google-apps-script,google-api,gmail,gmail-api,Email,Google Apps Script,Google Api,Gmail,Gmail Api,我找不到任何有文档记录的方法来完成通过GMAIL API发送电子邮件,使用url(获取请求),类似这样: https://example_api/?action=send&to=someone@gmail.com&message=hello&auth_user=myuser@gmail.com&pass=xyz-app-pass 有什么想法或技巧来实现这一点吗?(因此,我可以使用普通密码或应用程序密码)。我相信您的目标如下 您想通过访问URL发送电子邮件,如h
https://example_api/?action=send&to=someone@gmail.com&message=hello&auth_user=myuser@gmail.com&pass=xyz-app-pass
有什么想法或技巧来实现这一点吗?(因此,我可以使用普通密码或应用程序密码)。我相信您的目标如下
- 您想通过访问URL发送电子邮件,如
使用Gmail APIhttps://example_api/?action=send&to=someone@gmail.com&message=hello&auth\u用户=myuser@gmail.com&pass=xyz应用程序pass
function doGet(e) {
const allowedUsers = [
{email: "myuser@gmail.com", password: "xyz-app-pass"},
,
,
,
]; // If you want to allow other users to use this script, please add other emails and passwords to the `allowedUsers` array;
const subject = "sample subject";
const {action, to, message, auth_user, pass} = e.parameter;
if (action == "send" && allowedUsers.some(({email, password}) => email == auth_user && password == pass)) {
MailApp.sendEmail({to: to, subject: subject, body: message});
return HtmlService.createHtmlOutput("Email was sent.");
}
return HtmlService.createHtmlOutput("Email was not sent.");
}
3.部署Web应用程序。
- 这样,脚本将作为访问Web应用程序的用户运行
- 在这种情况下,请使用浏览器访问Web应用程序。这样,当你登录谷歌时,网络应用程序就可以使用了
- 而且,在这种设置中,当“用户A”通过登录谷歌访问网络应用时,“用户A”也可以使用自己的Gmail发送电子邮件。在这种情况下,请将“用户A”的
添加到脚本中的{email:######,密码:####}
。这样,只有注册用户才能发送电子邮件allowedUsers
- 如果您不想与其他用户共享,请将
和执行应用程序设置为:Me
谁有权访问该应用程序:仅限我自己
https://script.google.com/macros/s/###/exec
。
- 当您修改Google Apps脚本时,请重新部署为新版本。这样,修改后的脚本将反映到Web应用程序中。请注意这一点。
https://script.google.com/macros/s/###/exec?action=send&to=someone@gmail.com&message=hello&auth_user=myuser@gmail.com&pass=xyz-app-pass
结果:
当运行上述脚本并且auth_user
和pass
包含在allowedUsers
中时,将发送电子邮件。
返回
注:
- 修改Web应用脚本时,请将Web应用重新部署为新版本。这样,最新的脚本就会反映到Web应用程序中。请注意这一点。
- 如果要使用脚本和curl访问Web应用,则需要使用访问令牌访问它。在这种情况下,
(在某些情况下,可能工作https://www.googleapis.com/auth/drive.file
或/drive
)/drive.readonly
- GoogleApps脚本是一个简单的示例脚本,用于解释实现目标的方法。因此,请根据您的实际情况进行修改
curl
使用链接,那么发送方帐户(来自的)似乎不可更改。在PHP中,可以使用API授权流,例如:。->AuthorizeAccount($\u GET['auth\u user'],$\u GET['pass'])
@T.Todua感谢您的回复。我很高兴我的回答对你的情况有用。虽然我建议的答案是为实现目标确定方向的方法,但当你想扩展这一点时,我认为这可能是有用的。