If you aren't using Google Workspace or If you don't have admin access to your Google Workspace then the only option left for you is to use Google SMTP which is not recommended way as you need to compromise recommended security best practices.

In this post we are going to discuss about sending emails using Google Apps Script by creating Google Apps Script Public Web App. The logics and methodologies are completely based on my personal preferences and feel free to modify it.

So let's get Started!


Create Google Apps Script

Go to https://script.google.com and click on New Project


Let's Rename the project by clicking on Untitled Project



Add GMail Service

Click on the + Sign near services tab and then choose Gmail API to add it. In the Identifier section, you can use any Identifier. I have used "GmailApp", we need to refer this when we use functions from this library. 



Code Time - Code your logic

Here is my code:

function doGet(e) {
  return ContentService.createTextOutput("Hi");
}

function doPost(e) {
  if(e.parameter && e.parameter.emails && e.parameter.body && e.parameter.subject){
    let senderName = "Your Name";
    let replyTo = "[email protected]";

    if(e.parameter.senderName) {
      senderName = e.parameter.senderName;
    }
    if(e.parameter.replyTo) {
      replyTo = e.parameter.replyTo;
    }

    sendMail(e.parameter.emails, e.parameter.body, e.parameter.subject, senderName, replyTo);

    return ContentService.createTextOutput("OK");
  }
  return ContentService.createTextOutput("NOT OK");
}

function sendMail(emails, body, subject, senderName, replyTo) {
  let options = {
    htmlBody: body,
    name: senderName,
    replyTo: replyTo
  };
  GmailApp.sendEmail(emails, subject, body, options);
}
Change the values of senderName and replyTo in above code.

Explanation:

doGet function will be invoked automatically by Google Apps Script whenever the URL is accessed using GET Method. In above case, I'm just displaying "Hi" message. But that's optional. If you omit this function, users will be seeing an error if the URL is accessed using GET Method, but that's fine.

doPost function will be invoked automatically by Google Apps Script whenever the URL is accessed using POST Method. In above case, I have written my logic to send email in this function because POST method is normally used for "create" actions. But it's not wrong even if you used doGet to write your logic. Above function checks if I have "emails" and "subject" parameter in the body and optional "senderName" and "replyTo" parameters.

sendMail function is my custom function, whose job is to send email using GmailApp Service which we included in the previous step(Note: I have used GmailApp.sendEmail because I had specified the identifier as GmailApp. If you used something else, then you need to use that).

I didn't use descriptive error message and if you want, you can even write an additional if condition to password protect it. But in my opinion, the long unpredictable URL is enough.

Now click on Save Icon to Save the project.

Deployment

Initial Deployment

Since you just created the script, this is going to be your first deployment. Click on Deploy and then Choose New Deployment.

Then click on the Setting Icon and select "Web app"

And then enter "Description", "Execute as" and "Who has access" fields. Who has access should be "Anyone"



And then click on Deploy. Then click on Authorize Access to grant Apps Scripts to send mail as you.

Then Choose the Google Account and proceed with granting permissions. You may get warnings if you are not on Google Workspace. But that's fine. Finally, you will get your URL as below.

Now you can use this URL to send mail. If you open this URL in the browser, you will see "Hi" message, because we specified it in doGet() function. We will look into send email using this URL in the next section.

Subsequent Deployments

After the first deployment you may need to make some changes to the script. In that case you have two options.
  • Deploy new version - So that you will get new URL without affecting current version
  • Modify existing version - URL won't be changed.
You may need to create new version if you have breaking changes that breaks existing one.
You need to edit the existing version if it doesn't break and if you feel difficult to change the Webhook URL in all the places where it's being used.

Deploy new version: Click on "Deploy" and then Click on "New Deployment". Then follow the steps as you followed above.
Modify existing version without changing URL: Click on "Deploy" and then Click on "Manage Deployments". Then Select the "New Version" in the Configuration section as below.


And then enter "Description" and Click "Deploy"

That's it! Now you have deployed your new changes without Changing URL.

Send Email Using Curl

In order to simplify the process we are going to use curl command to send email using the URL we got above. Feel free to use your favorite programming language to achieve the same.

curl -L 'https://script.google.com/macros/s/rest-of-your-webhook-url/exec' \
     -d "senderName=YourName" \
     -d "subject=Welcome" \
     -d "body=Hii" \
     -d "[email protected]"

I have used -L parameter above, because Google Apps Script URL would do some redirects. In order to follow those redirects, I have used -L

So, what if you need to send the email to multiple recipients? You can simply add semicolons to add more recipients like below.

curl -L 'https://script.google.com/macros/s/rest-of-your-webhook-url/exec' \
     -d "senderName=YourName" \
     -d "subject=Welcome" \
     -d "body=Hii" \
     -d "[email protected];[email protected]"

Things to Note:

That's all for this tutorial. If you run into any issues, feel free to leave a comment. Your feedbacks are appreciated!