Using Gmail, can I create a script that allows me to key off of an address in the To/Cc field to add an address to the BCC field using some browser-side code?
Ex:
Whenever an email with example#example.com is created in the TO/CC field (or created via a reply/reply-all etc) I want for the email address otherName#myCRM.com to be added to the BCC field.
I don't want this for a single address (or this would be much easier) I want to have a dictionary with many To/CC addresses that maps to many BCC addresses. I would also like to update the dictionary on a regular basis.
Here is a link to the API that I was planning on using to create this Google Apps Script: https://developers.google.com/apps-script/reference/gmail/
Is this possible? If so is there an example of doing it? I haven't been able to find one!
Because Google Apps Script runs server side, not client side, you can't do this in the way you have described.
While you can use Apps Script to access messages in the Drafts or Sent labels, it doesn't appear you can set the BCC value of those messages, only retrieve it.
One approach that might work is to poll the Draft or Sent messages folder on a fixed schedule, identify emails meeting your criteria, and then forward them to a given address (the one you wish to BCC). There are a number of challenges to this approach, such as determining how to avoid forwarding a given message multiple times, but it could be made to work.
See the docs on how to run your script on a schedule here:
https://developers.google.com/apps-script/guides/triggers/installable#time-driven_triggers
Edit, additional details in response to comment:
Setting up the triggers is very simple, I've used them many times in the past. You simply write a function that takes the actions you want, and once you have finished testing it manually, you can create the trigger via the "Resources" -> "Current Script's Triggers" menu options in the Script Editor.
You can't simply store processed id's in a variable, state is not shared between instances of the script. So you'll need to track them elsewhere, depending on volume you might want to look at the Properties Service, storing them in a spreadsheet, or my favorite would be to apply a label to processed emails and cross check the label in your script.
The label approach also lets you easily see which messages have been processed in the gmail interface.
If what you want is sending a copy of sent mail automatically to some address, that can be done with filters. I found the solution here:
http://markmail.org/message/nj46fcwlccd6asoj
"Configure the filter like this:
From: your email address
Forward: your email address
Enable never send to spam (IMPORTANT!)
"
Related
I'm trying to come up with a solution that will allow me to (1) pull data from Sheets for only ONE of the rows into a Gmail template and (2) allow for modifying the template with additional information that's not in the spreadsheet before sending it off.
Context: My organization uses an intake/referral log to track key information about referrals. Once info is in the Sheet, they manually send an email assigning the referral to an employee for follow-up. Much of the info in the Sheet is retyped...which initially made me think of automating this fully with AppsScript, so that all the standard info (case #, case type, due dates, etc) is included in that email. However, the emails sometimes have a paragraph or three of contextual information that we don't want to store in the spreadsheets and have pulled into the automated email.
So my goal is to pull the data from the spreadsheet into the email where I can (vs having to retype it) AND allow the addition of contextual info before sending it off to the assigned employee.
Any thoughts on how to accomplish this? I haven't found a mail merge add-on or Apps Script solution that allows doing this one row at a time and adding in extra custom info into the email.
Thanks in advance!
So this question, as currently written, could clearly be accomplished a whole bunch of different ways. With that in mind, here is one quick and dirty way in which it might be accomplished:
On the spreadsheet where the data is stored, create a script via tools>script editor.
Using that script, create a menu which runs a function that does the following, perhaps in an endless loop:
a. Prompt the user for the email address of where to send the email if it isn't in the spreadsheet or a token value, such as an empty string, to end the script.
b. Prompt the user for the number of the row where the info is stored.
c. Prompt the user for any extra contextual information to add to the email.
d. Get the info from the applicable row in the spreadsheet.
e. Create a nicely formatted email and send it off.
From there, the person in charge of doing the referrals would simply run the script and feed it the required information. Clearly human error could fowl things up here, but without more details of exactly how you want this to work, it isn't really possible to define exactly how to best protect against human error. Reading up on Data Validation might give you a start on that front.
And if you are still completely lost, I happen to be a Google Apps Script tutor/consultant who often gives away free tutoring/consulting. See tutoringbyroger.com for more on that.
I hope that helps.
What I have now: I have a static HTML site where I'm creating a dashboard with Google Data Studio. The spreadsheet connected to Google Data Studio has sensitive data of 100s of different users, each identified by their e-mail address in the organization.
Goal: Display only the rows of a table where a user's e-mail is stated. I want to do it retrieving the user e-mail from session, passing it to Google Data Studio and automatically show rows corresponding to the address.
Code snippet:
- Retrieve the user's e-mail from session:
var email = Session.getActiveUser().getEmail();
But this is all I know. I don't know:
- Wether I have to make a connector, write it on Google Apps scripts then link it or write it in the Spreadsheets Apps Script (makes no sense for me).
- How to send the 'email' retrieved to Google Data Studio.
Any lights on this issue would be really useful. Thanks!
It's not possible to retrieve the active user's email unless there are certain conditions. From the documentation we can read that:
If security policies do not allow access to the user's identity,
User.getEmail() returns a blank string.
The circumstances in which the email address is available vary: for
example, the user's email address is not available in any context that
allows a script to run without that user's authorization...
...these restrictions generally do not apply if the developer runs the
script themselves or belongs to the same G Suite domain as the user.
This means you would need to deploy the script as a web app with the option:
Execute the app as: User accessing the web app
Steps:
Data Set: [Done] In the the spreadsheet, create a field (e.g. email) which includes an email address on each row.
Data Source: At the Data Source (editing an existing or creating a new one) the top left now includes an email filter where the field with the email address (email) can be selected as the field to filter the report by.
Edit: The Filter by email address feature was launched last week. This can be a much simpler solution to your requirements.
Original Answer: Implement Row level filtering with Data Studio. However, instead of using BigQuery as your source of data, fetch the data from the associated Sheet.
I am developing a Google Docs Add-on and need to know if the user currently using the add-on has used it before in the current document or any document. I can't figure out which service gives me some kind of unique identifier for the current user that I can use to identify the current user.
The base service's User.getEmail is not good enough as it doesn't give access to the email under certain conditions and is not reliable.
Thoughts on best way to do this?
Unfortunately, there is no such service. Google wants users' personal information to remain private, so AFAIK you won't usually get even user's email, unless your script is written for Google Apps domain and the user is in the same domain as the script's author.
The only thing you can do in the situation is use something like DocumentApp.getActiveDocument().getId() and check if the script was used in this document, but of course this won't give you an idea if user has used your script in other documents or not.
Also you can show a Prompt Dialog to user, asking him to enter email, but of course there is no way you can check if he entered his real email.
Unique user identifier you can get for apps script is Session.getEffectiveUser().getEmail()
This will return the email of user for which the script run. This is better than ActiveUser because if you use trigger EffectiveUser will well return the email of the user who install the script.
I use that for several apps to identify license assignment and it works great.
It looks like the best solution is to roll your own solution using the following to set the userId at an appropriate time:
PropertiesService.getUserProperties().setProperty("userId",<someGeneratedUId>)
then whenever you want to look up if this user is a recognizable user,
var userId = PropertiesServices.getUserProperties().getProperty("userId")
Looks like once set, the UserProperties thus set is available to this particular user (per user) running this add-on everywhere (i.e. all docs, not just the current on) that have this particular add-on enabled/running.
I'll validate this further but seems like this works in my tests.
I'm wondering if there are any options for changing the FROM address of an email sent using MailApp.sendEmail.
I've built a form that's published as a Web App. Students fill out and submit the form, and receive an email. I see how to change the email's reply-to address and sender name. I know that the email's using my address as the From address since I'm the script's owner and the app runs as me.
Is there any way to set the From address to belong to another person, in this case, the instructor? I'm assuming one way to accomplish this is to make him the script owner and walk him through re-publishing the app for us, but I'd rather not hand that over to him. (From what I've seen, only the script owner can publish the script.)
In other words, I want the From address to represent the client the script is being created for, not the programmer who's writing it.
I understand that it's not a desirable feature to allow a script to send email from anyone under the sun. I'm just wondering if anyone has a recommendation for how to solve my problem.
Thanks!
If you use GMailApp.sendMail (instead of MailApp.sendEmail) you can specify a from as an optional additional parameter:
https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String,Object)
I believe you can only specify aliases that you have already configured your Gmail account with.
Google is not flexible on this one - the only "from" address that you can see is the one belonging to the user whose authority is running the script.
Instead of running as "the programmer", you can use a "robot" google account, something like DepartmentRobot#ourdomain.com. You can develop your scripts as yourself, then republish them from the robot.
Within a Google Apps Domain, your admin can create this psuedo-user and edit the settings to hide them from the Google Apps Directory, if that's desireable.
The email that the students receive won't be from their instructor, but it won't be from you, either.
Yes you can and it's quite easy. Use the log to determine which spot in the array your alias is if you have multiple aliases. Then to test that you have the right array location use that in the log.
var aliases = GmailApp.getAliases()
Logger.log(aliases); //returns the list of aliases you own
Logger.log(aliases[0]); //returns the alias located at position 0 of the aliases array
GmailApp.sendEmail('person#aol.com','From an alias', 'A message from an alias!', {'from': aliases[0]});
it's possible only if you add an Alias to the Account you want to send the Mail from.
important: the alias should be added manuelly to the desired account (in Gmail go to "settings", then "Account" then "Send as")
when you do that, you have to use the GmailApp method in your Script called 'GmailApp.SendEmail()'
first you have to read the Alias from the Gmail-Account:
var myAliases = GmailApp.getAliases();
let's assume that you have just one alias, so the first alias will be in the indexd 0 -> myAliases[0]
in this case you can use the method like this:
GmailApp.sendEmail('recipient#yourdomain.con', 'subject', 'body', {from: myAliases[0], name:'Name of the Alias'});
that's it.
To further build on #RAWdaMedia and #mickolasjae answers, if you want to send html email using an alias email, you can do so like this.
// this should contain data to be put in the variable in your html
var data = [data1, data2];
var html = HtmlService.createTemplateFromFile('your-html-file-name');
html.data = data;
var template = html.evaluate().getContent();
var subjectLine = "Your subject line";
var aliases = GmailApp.getAliases()
// this line sends the email using alias email
GmailApp.sendEmail(email, subjectLine, template, {from: aliases[0], htmlBody: template});
A few things to note:
In your Gmail setting, make sure the email alias that you want to use is not marked as default
Check the index of the email alias that you want to use if you have more than one and change the index of aliases accordingly, so it might be aliases[1] or aliases[2]
I am not sure, but I think this might be useful to your question.
I have looked for hours for a solution where a totally stranger can send mail on your behalf but through google script but it didn't work.
Though you can change code and put entire code in if loop where If particular sheet's particular cell has particular word your program will run or Exit.
if program run it at the end you can put command to clear that cell.
then you can instruct user to set A1 of Sheet1 to "Send"
parallelly set time driven trigger to run every minute.
so with in a minute of submission you will get mail from your ID and guess what this time person doesn't even need to login google account!
You can ask users to check if someone else is online on top right of sheet. if it is case they can come back later. Method is bit crude but it works.
I'm new here. just trying to be helpful
I wanted to build on chrisb's answer above, but I am not allowed to comment yet...
As chrisb said:
create an allias in your account just for the form.
Use Gmail.App to send the email "from" the alias that you created.
Then write Google Apps script to process emails sent to that alias such that emails from the instructor appear in your inbox, but emails from anyone else get forwarded to the instructor with the "replyTo" field set to whoever sent the email.
My students all have a Google account, but use different emails (like john#hotmail.com).I tried to write a webapp that would get from my grade spreadsheet the row with the webapp's user's email address, so that each student would only see his or her grades. That webapp uses: Session.getActiveUser().getEmail()
This only works for users in the same domain as the script, I have to run the script as the webapp user (which is ok -- my students trust me that far!). Here is that restriction:
Returns a User object describing the current user. In
limited-privilege executions (such as in response to onOpen or
onEdit), we only return the identity of the active user if both the
user and the script owner are part of the same domain. This is to
protect the privacy of consumer users, who may not want their email
address exposed.
My script works, but I have to put the table of grades directly in the script and update the webapp each time I update my spreadsheet. Yeck!
I can't share my grade spreadsheet because then they would see each other's grades. So now I am brainstorming other workarounds.
Here is one example:
Share a second spreadsheet, copy the grades from my spreadsheet with an update function that encrypts the grades. The webapp sucks up the data from the shared spreadsheet and decrypts. Now a simple update of a spreadsheet is all that is needed -- the webapp doesn't need to be republished. (There are some simple encryption options, like base64encode with maybe a little scrambling...?)
Find a way to automatically update the webapp and republish. (Don't know how to do that.)
????
Any suggestions?
PS: if 1 seems feasible, some suggestions for simple encryption code?
There are some simple encryption options, like base64encode with maybe a little scrambling...?
Yes, the GAS has functions to encode/decode using the Base64 algorithm - Utilities.base64Encode, Utilities.base64Decode, but I think, it is not an option even with a little scrambling. Students are very clever. My opinion is to use a JavaScript implementation of a encryption algorithm, for instance the Blowfish. There are a number of its implementations on JavaScript (here and here). These implementations should work in the GAS environment without any changes or with small modifications. It is should be sufficient to copy and paste the source code to the GAS Editor.
I dont understand why you say "My script works, but I have to put the table of grades directly in the script and update the webapp each time I update my spreadsheet"
It is very easy to create a webapp that shows a part of a spreadsheet using a flextable for example. Each instance of the app will have a user related content automatically and you will only have to update your master spreadsheet to get what you want. I don't know why you want to encode data for this... I think the user identification through the google login should be enough, don't you ?
Here is an example of such a webapp, the numbers you see are taken from a specific column in a master spreadsheet (for this public copy I set the app as running as "me" to avoid the authorization process but in real every user is identified and sees only his data).
The Spreadsheet itself and the code are viewable here, do not hesitate to come back if you need further information.
Is it an option to sync your sheet with a scriptDB?
And then query the DB where user=loginID