Search code examples
emailgoogle-apps-scriptgoogle-apigmailgmail-api

Possible to send mail from Google by URL?


I was unable to find any documented way to accomplish to send email through GMAIL API, using url (GET request), something like this:

https://example_api/?action=send&[email protected]&message=hello&[email protected]&pass=xyz-app-pass

any ideas or tricks to accomplish that ? (so, I could use plain or app password).


Solution

  • I believe your goal as follows.

    I thought that your goal can be achieved using Web Apps created by Google Apps Script. In this case, the script might be a simple. So in this answer, I would like to propose to achieve your goal using Web Apps created by Google Apps Script.

    Usage:

    Please do the following flow.

    1. Create new project of Google Apps Script.

    Sample script of Web Apps is a Google Apps Script. So please create a project of Google Apps Script.

    If you want to directly create it, please access to https://script.new/. In this case, if you are not logged in Google, the log in screen is opened. So please log in to Google. By this, the script editor of Google Apps Script is opened.

    2. Prepare script.

    Please copy and paste the following script (Google Apps Script) to the script editor. This script is for the Web Apps.

    function doGet(e) {
      const allowedUsers = [
        {email: "[email protected]", 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. Deploy Web Apps.

    1. On the script editor, Open a dialog box by "Publish" -> "Deploy as web app".
    2. Select "User accessing the web app" for "Execute the app as:".
      • By this, the script is run as the user which accessed to the Web Apps.
    3. Select "Anyone" for "Who has access to the app:".
    • In this case, please access to the Web Apps using the browser. By this, when you login to the Google, the Web Apps can be used.
    • And, in this settings, when "User A" is access to the Web Apps by logging in Google, "User A" can also send the email using own Gmail. In this case, please add {email: "###", password: "###"} of "User A" to allowedUsers in the script. By this, only registered users can send the email.
    • If you don't want to share with other users, please set Execute the app as: Me and Who has access to the app: Only myself.
    1. Click "Deploy" button as new "Project version".
    2. Automatically open a dialog box of "Authorization required".
      1. Click "Review Permissions".
      2. Select own account.
      3. Click "Advanced" at "This app isn't verified".
      4. Click "Go to ### project name ###(unsafe)"
      5. Click "Allow" button.
    3. Click "OK".
    4. Copy the URL of Web Apps. It's like https://script.google.com/macros/s/###/exec.
      • When you modified the Google Apps Script, please redeploy as new version. By this, the modified script is reflected to Web Apps. Please be careful this.

    4. Testing Web Apps.

    Please access to the URL of your Web Apps using your browser by including the query parameters as follows. When you have already been logged in Google, the script of Web Apps is run.

    https://script.google.com/macros/s/###/exec?action=send&[email protected]&message=hello&[email protected]&pass=xyz-app-pass
    
    Result:

    When above script is run and auth_user and pass are included in allowedUsers, Email was sent. is returned.

    Note:

    • When you modified the script of Web Apps, please redeploy the Web Apps as new version. By this, the latest script is reflected to Web Apps. Please be careful this.
    • If you want to access to the Web Apps using a script and curl, it is required to access to it using the access token. In this case, include the scope of https://www.googleapis.com/auth/drive.file (in some cases,might work /drive or /drive.readonly)
    • The Google Apps Script is a simple sample script for explaining the method for achieving your goal. So please modify it for your actual situation.

    References: