I'm curious to learn if the following scenario can be addressed using Google Apps Script (or perhaps another method?). Here's the scenario I'm dealing with.
Perform a specific subject line search when new messages are received. For example:
({subject:(keyword1 keyword2 keyword3) subject:(keyword1 keyword4 keyword5)})
If matched, perform the following actions:
Modify the subject line to include "#action"
Forward this newly renamed mail to another email address
Bonus points if I can somehow then label the original email, and archive said original mail (removing it from my inbox) in one fell swoop.
Help is most appreciated.
As per the Gmail API, you can only modify the labels of a Gmail message but not the subject or body.
To forward an email to another account, or to archive an email, you could use the message.forward() and moveThreadsToArchive() methods in Google Apps Script.
function archiveMessages() {
var query = 'label:archiveme';
var threads = GmailApp.search(query);
GmailApp.moveThreadsToArchive(threads);
threads.forEach(function(thread) {
thread.getMessages().forEach(function(message) {
message.forward("recipient#example.com");
});
});
}
Related
I have a budget sheet, which need to be populated in africa, by people without a google account.
I share the google sheet via a link and they can update the content. When they finished the updating, I would like to give them a possibility to send it to a small distribution list via a script
function sendReport() {
var message = {
to: "user#domain.org",
subject: "Budget proposal",
body: "Hi team,\n\nPlease find the budget attached.\n\nThank you,\n",
name: "Budget",
From: 'user#gmail.com',
attachments: [SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("Budget")]
}
MailApp.sendEmail(message);
}
This works fine as long as I m logged in via any google account, but for a non google account, I get the following error:
Script sendReport experienced an error Details
The amount of data is too much for a form (but in a form I seem to be able to ask them to send a mail, so I wonder where I go wrong...
Thanks
MailApp.sendEmail(message) does not accept a From parameter, so that part of the object message gets ignored.
So if your account executes this script, you send it from your own domain email. Since it requires a google email to send a message, non-google accounts cannot execute this script.
Reference:
MailApp.sendEmail
I am developing a add-on program in google app script which gets all the gmail sent mails with there subject, body, attachment(if any). I have did this for the Inbox mail using getInboxThreads() function. Is there a function which does same for sent mails?
The program that i am writing is for any gmail user or a group of users how wants to save their gmail emails on the google drive for monitoring or any other operations.
You can use the user.messages.list method to get a list of all the message ids for the user. You will then have to use user.messages.get To get the data about the message.
You can use the 'q': 'in:sent' parameter to get only the messages in the sent folder.
function myFunction() {
var myMessages=Gmail.Users.Messages.list("me",{'maxResults': 5 , 'q': 'in:sent' }).messages;
Logger.log(myMessages);
for(var i=0;i<myMessages.length;i++){
var id=myMessages[i].id;
Gmail.Users.Messages.get('me', id).payload.headers.forEach(function(e){
if(e.name=="Subject"||e.name=="From"){
Logger.log(e.name+": "+e.value)
}
}
);
}
}
Thank you #DalmTo
Your post help me lot.
I did some research got a similar but little bit different solution.
I found a method in GmailApp class called search and sent a query(in:sent) as you suggested. This returned me all the sent mails.
I did something like this
var _sentMail = GmailApp.search('in:sent');
This returned me array of GmailThread.
The one thing i did found that sent label in our gmail dashboard is not actually a label, its a method which takes a parameter "in:sent".
My Sheet-bound script is sending an email using MailApp.sendEmail.
The emails are always sent 'from' my own Gmail account. This project is for a customer, and I need his email to be the 'from' on these emails.
Reading similar questions I learn that the I only have flexibility in changing the name and replyTo address, using the advanced options of MailApp.sendEmail. However, the email address is still mine and Google doesn't offer any control over that.
I'm not familiar enough with all of the Google services and options to find the best way to do this. My customer does have a Google Apps for Business, but I don't.
Can I somehow create the email-sending function as a standalone script under his account, and somehow call it from the project under my account?
Any other ideas?
Thanks!
Emails are always sent from the account of the user that executes the script. In case the email is sent by a triggered function (installable triggers are the only ones that are able to send emails since it requires explicit authorization) then the email is sent from the account of the user that created the trigger (and authorized it).
In your case, it seems that the easier solution would be to ask your customer to execute the script himself and initiate all the triggers himself too.
If that should not be possible then you could indeed use a standalone script that would work as a kind of proxy, ie it would receive a request to send a message and actually send it from the customer account while returning an acknowledgement to your script.
But that's a bit tricky... the first solution is more elegant.
Edit :
I found the idea of sending emails through an independent script funny so I gave it a quick try and it seems to do the job pretty easily... test code below (this code should be deployed as a standalone app from your customer account) :
function doGet(e) {
Logger.log('e = e'+JSON.stringify(e));
if(e.parameter.recipient==null){return ContentService.createTextOutput("error, wrong request "+JSON.stringify(e)+"\n\n"+e.parameter.recipient+"\n\n"+e.parameter.subject+"\n\n"+e.parameter.body).setMimeType(ContentService.MimeType.TEXT)};
try{
MailApp.sendEmail(e.parameter.recipient, e.parameter.subject, e.parameter.body)
}catch(err){
return ContentService.createTextOutput('error : '+err).setMimeType(ContentService.MimeType.TEXT);
}
return ContentService.createTextOutput('mail successfully sent').setMimeType(ContentService.MimeType.TEXT);
}
note : the code below goes in your spreadsheet script, the doGet above is an standalone app running from your customer account.
function sendMail(recipient,subject,body){
var sent = UrlFetchApp.fetch("https://script.google.com/macros/s/---------------S381kO1Kqv61E/exec?recipient="+recipient+"&subject="+subject+"&body="+body);
Logger.log(sent);
}
function test(){
sendMail('recipient#gmail.com','test message','hello world')
}
I was able to send messages from my gmail account while being logged as a different user. (the url above is intentionally truncated, I don't want anyone to send email from my account ;-)
i want a script that sends a message to a particular cell number on event of an update in my spreadsheet or through a add menu button option.
i tried to find but couldn't see any option like that.
One of the simplest ways is to write a script to send it via email using your cell carriers email code.
Here's a list of codes https://20somethingfinance.com/how-to-send-text-messages-sms-via-email-for-free/ .
Under tools select script editor and basically just copy the following code in. Change up the EmailTo, Subject, and Body accordingly.
function sendText() {
var EmailTo = "your10digitNumber#CellcarrierCode";
var subject = "Whatever";
var body = "Text";
MailApp.sendEmail(EmailTo, subject, body);
}
In the script editor under Edit select current project triggers and setup however you want.
Adding a menu or setting up an onEdit even function in a document is pretty straightforward (just a few lines of code depending on the complexity of your application). Google Apps Script does not have built in functionality for SMS/Text messaging however.
If you want to accomplish this sms functionality, you will need to identify a company that offers an SMS/Text messaging API and access the API with a custom script. Twilio is a great tool (although not free) or you may find another API that fits your needs based on your region in this list of SMS APIs: http://blog.mashape.com/list-of-50-sms-apis/.
Once you have your service/API selected and set up and have the API Documentation, refer to the following Google Developers page to access the service API with Apps Script: https://developers.google.com/apps-script/guides/services/external.
If you're not well versed in using APIs, you can instead use MailApp to send e-mail or log any activity in another spreadsheet for tracking purposes. One other low-tech solution to consider is that spreadsheets have a built-in edit notification under Tools > Notification Rules.
I attempted to
try{
MailApp.sendEmail(phone+'#txt.att.net',subject,body);
}
catch(err){}
The email sent. It worked. BUT it did not send the text. HOWEVER, if I REPLIED to the email (from sent mail) to the phone plus carrier code it did go through. But only the reply.
Okay this is very strange. I have scripting in a spreadsheet to take information from the currently active sheet, creates a report using a template file and sends it as an attachment.
This all works perfectly, when I use my Google apps domain account email address as the recipient. It doesn't work when I send to any other email address, other than my own. It was working yesterday. No errors generated when the script runs
The only thing that I did was change the owner of the spreadsheet to another user in our domain. It was shared with the other user while I was testing the scripts. I've tried using other email addresses in our domain and created a new spreadsheet with the sendemail function, all with the same behavior.
// Email the specified report
function emailReport(file, recipients, emailSubject, emailMessage) {
MailApp.sendEmail("someone#example.com", emailSubject, emailMessage,
{attachments: file, mimetype: 'application/pdf'});
}
I've been able to reproduce the problem and to find a workaround.
As it's not posible to send email to recipients without google account, I add that kind of emails on a cc field.
// Email the specified report as cc
function emailReport(file, recipients, emailSubject, emailMessage) {
MailApp.sendEmail("my#gmail.com", emailSubject, emailMessage,
{attachments: file, mimetype: 'application/pdf', cc:"someone#example.com"});
}
I noticed this question a while back, and even referenced it in another one. Since then I've noticed no one responded to you so...
It appears as if Google has recently changed (though not documented anywhere I've found) the MailApp.sendEmail function so that it only works when you use the email address belonging to the owner of the spread sheet.
My guess is this is to prevent the system being used for unsolicited mass emailing.
The other question was here
(Sorry, not an answer as such :( more of a confirmation as to what you are seeing seems to be as expected)
For anyone having this issue, use the GmailApp class instead of the MailApp class.
It supports the same functions, for example: GmailApp.sendMail('recipient#gmail.com', 'Subject', 'Message').
https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String)