URL 可以从 Google 发送邮件吗?

Possible to send mail from Google by URL?

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

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

有什么想法或技巧可以实现吗? (所以,我可以使用普通密码或应用密码)。

我相信你的目标如下。

  • 您想使用 Gmail API.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 应用程序来实现您的目标。

用法:

请执行以下流程。

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

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

如果要直接创建,请访问https://script.new/。在这种情况下,如果您未登录 Google,则会打开登录屏幕。所以请登录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. 在脚本编辑器上,通过“发布”->“部署为网络应用程序”打开一个对话框。
  2. Select “用户访问网络应用程序” for “执行应用程序为:”
    • 这样,脚本就运行作为访问Web Apps的用户。
  3. Select “任何人” “有权访问该应用程序的人:”.
  • 在这种情况下,请使用浏览器访问Web Apps。这样,当您登录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. Select自己的账号。
    3. 点击“此应用未验证”处的“高级”。
    4. 点击“转到###项目名称###(不安全)”
    5. 单击“允许”按钮。
  3. 点击“确定”。
  4. 复制 Web 应用程序的 URL。就像 https://script.google.com/macros/s/###/exec
    • 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。

4。测试 Web 应用程序。

请使用浏览器访问您的 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
结果:

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

注:

  • 当您修改Web Apps的脚本时,请重新部署Web Apps为新版本。由此,最新的脚本被反​​映到Web Apps。请注意这一点。
  • 如果您想使用脚本和curl 访问Web 应用程序,则需要使用访问令牌访问它。在这种情况下,include the scope of https://www.googleapis.com/auth/drive.file(在某些情况下,/drive/drive.readonly 可能有效)
  • Google Apps 脚本是一个简单的示例脚本,用于说明实现目标的方法。所以请根据自己的实际情况修改。

参考文献: