【问题标题】:Possible to send mail from Google by URL?可以通过 URL 从 Google 发送邮件吗?
【发布时间】:2020-12-28 05:25:43
【问题描述】:

我找不到任何记录的方式来完成通过 GMAIL API 发送电子邮件,使用 url(GET 请求),如下所示:

https://example_api/?action=send&to=someone@gmail.com&message=hello&auth_user=myuser@gmail.com&pass=xyz-app-pass

有什么想法或技巧可以做到这一点? (所以,我可以使用普通密码或应用密码)。

【问题讨论】:

标签: email google-apps-script google-api gmail gmail-api


【解决方案1】:

我相信你的目标如下。

  • 您想通过使用 Gmail API 访问像 https://example_api/?action=send&to=someone@gmail.com&message=hello&auth_user=myuser@gmail.com&pass=xyz-app-pass 这样的 URL 来发送电子邮件。

我认为您的目标可以使用由 Google Apps 脚本创建的 Web 应用程序来实现。在这种情况下,脚本可能很简单。所以在这个答案中,我想建议使用 Google Apps Script 创建的 Web Apps 来实现您的目标。

用法:

请执行以下流程。

1。创建 Google Apps 脚本的新项目。

Web Apps 的示例脚本是 Google Apps 脚本。所以请创建一个 Google Apps Script 项目。

如果要直接创建,请访问https://script.new/。在这种情况下,如果您没有登录 Google,则会打开登录屏幕。所以请登录谷歌。这样,Google Apps Script 的脚本编辑器就打开了。

2。准备脚本。

请将以下脚本(Google Apps 脚本)复制并粘贴到脚本编辑器中。此脚本适用于 Web 应用程序。

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 应用程序。

  1. 在脚本编辑器上,通过“发布”->“部署为 Web 应用”打开一个对话框。
  2. “执行应用程序为:”选择“用户访问网络应用程序”
    • 这样,脚本以访问 Web 应用程序的用户身份运行。
  3. “谁有权访问应用程序:”选择“任何人”
  • 在这种情况下,请使用浏览器访问 Web 应用程序。这样,当您登录 Google 时,就可以使用 Web Apps。
  • 并且,在此设置中,当“用户 A”通过登录 Google 访问 Web 应用程序时,“用户 A”也可以使用自己的 Gmail 发送电子邮件。在这种情况下,请将“用户A”的{email: "###", password: "###"} 添加到脚本中的allowedUsers。这样,只有注册用户才能发送电子邮件。
  • 如果不想分享给其他用户,请设置Execute the app as: MeWho has access to the app: Only myself
  1. 单击“部署”按钮作为新的“项目版本”。
  2. 自动打开“需要授权”对话框。
    1. 点击“查看权限”。
    2. 选择自己的帐户。
    3. 点击“此应用未验证”中的“高级”。
    4. 点击“转到###项目名称###(不安全)”
    5. 点击“允许”按钮。
  3. 点击“确定”。
  4. 复制 Web 应用程序的 URL。就像https://script.google.com/macros/s/###/exec
    • 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。

4。测试网络应用。

请通过包含如下查询参数,使用您的浏览器访问您的 Web 应用程序的 URL。当您已经登录 Google 后,就会运行 Web Apps 的脚本。

https://script.google.com/macros/s/###/exec?action=send&to=someone@gmail.com&message=hello&auth_user=myuser@gmail.com&pass=xyz-app-pass
结果:

当运行上述脚本并且allowedUsers 中包含auth_userpass 时,返回Email was sent.

注意:

  • 当您修改 Web Apps 的脚本时,请将 Web Apps 重新部署为新版本。这样,最新的脚本就会反映到 Web 应用程序中。请注意这一点。
  • 如果您想使用脚本和 curl 访问 Web 应用程序,则需要使用访问令牌访问它。在这种情况下,include the scopehttps://www.googleapis.com/auth/drive.file (在某些情况下,可能工作 /drive/drive.readonly
  • Google Apps 脚本是一个简单的示例脚本,用于解释实现目标的方法。所以请根据您的实际情况进行修改。

参考资料:

【讨论】:

  • 出色的答案,远远超出我的预期!非常感谢!! ....正如我所见,如果仅使用来自curl 的链接,则发件人帐户(from)似乎无法更改。在 PHP 中,可以使用 API 授权流程,例如:..->AuthorizeAccount( $_GET['auth_user'], $_GET['pass'] )
  • @T.Todua 感谢您的回复。很高兴我的回答对您的情况有用。虽然我提出的答案是朝着实现目标的方向发展的方法,但当你想扩展它时,我认为this information 可能会有用。
猜你喜欢
  • 2011-10-01
  • 2015-04-26
  • 2020-05-01
  • 1970-01-01
  • 2021-08-19
  • 2016-10-17
  • 2011-08-19
  • 2017-06-14
  • 1970-01-01
相关资源
最近更新 更多