I've got a working Sheets / Google Code script to pull information from a sheet and fire it into a HTML templated e-mail but was wondering if there's any way you can define the senders address beyond the user you're authenticated to.
i.e. User is authenticated into G-Suite and has access to shared mailboxes. Is there a way of defining which mailbox to send from via Google Apps Script?
Couldn't see any documentation on this so hold limited hopes but wanted to check with the community at SO before closing it off.
Google documentation: https://developers.google.com/apps-script/reference/mail/mail-app
Thanks
Using GmailApp instead, you can select one of your available aliases using sendEmail(recipient, subject, body, options).
For example:
GmailApp.sendEmail("recipient#example.com", "Subject", "Body", {from: "alias#example.com"});
Note that to do so, the from address must be an available alias. You can check available aliases with this function:
function listAliases() {
var me = Session.getActiveUser().getEmail();
var aliases = GmailApp.getAliases();
Logger.log(aliases);
}
Related
I have the following code
var mailOptions = {};
mailOptions.attachments = attachmentArray;
mailOptions.htmlBody = htmlbody;
var alias = ??? // I have no idea what to put here. I've tried just putting in a string with the address
mailOptions.from = alias;
if(email){
GmailApp.sendEmail(email, subject,"",mailOptions);
I'm trying to send an email using an alias but I have multiple aliases to choose from. I saw somewhere that I can maybe use this
Gmail.Users.Settings.SendAs.Get
I should mention that while I wrote the script, I am not the owner of the spreadsheet. The original owner has a G Suite account, and wants to use their alias above. So it is not my alias, but they will be the one running the script.
First you should add an alias to Gmail. You can do this from the Gmail web app (see Send emails from a different address or alias) Also you might do this by using the Gmail API (see Managing Aliases
Once your Gmail account has one or more alias, you can use send email from an alias by adding its email address to the corresponing property of the GmailApp.sendEmail options argument.
Related
Email via Google Apps Script from another email address
It it possible to change the from address when using sendEmail?
I have a Google Sheet, its have several internal code (gs) and finish doing a list with mails and text, actually Im using a function to send mails:
function SendMail () {
var vAuxiliar = SpreadsheetApp.getActive().getSheetByName("Auxiliar");
var vLast = vAuxiliar.getDataRange().getNumRows();
var vDataRange = vAuxiliar.getRange("A2:E"+vLast);
var vValues = vDataRange.getValues();
for(var i in vValues) {
var vemailAddress = vValues[i][1];
var vMensaje = vValues[i][4];
var vSubject = 'Contact.';
MailApp.sendEmail(vemailAddress, vSubject, vMensaje);
};
};
Its works great but mails are incoming with my personal account, I need change this function to send mails with my enterprise account, we use MS Outlook, it is possible?
Thanks in advance!!!
You sure cannot use the Gmail service to send messages through your MS Outlook account.
But a possible workaround would be to create aliases for your MS Outlook account in your Gmail account, and then using this alias when sending.
GmailApp.sendEmail(to, 'Subject', 'A message sent from an alias!', {'from': "enterprise#example.com"});
But for using this from parameter in sendEmail you need to be able to retrieve it as an alias with getAliases().
Refer to this page to add your enterprise mail as an aliases to be able to use later.
Note that using the term alias here refers to the Gmail terminology as external email address. Do not check "Treat as alias" when configuring your MS Outlook account. This is another story.
Also as Ruben said in the comments you could try to ask if there are any API you can use to send mail. In that case you should try to use the URL fetch method.
Is it possible to process multiple selected emails by an addon?
The code from Google shows how to process only one selected email. I suppose 'e' would be some kind of array of accesstokens but I don't know how to access it.
function getContextualAddOn(e) {
var accessToken = e.messageMetadata.accessToken;
Logger.log("token:"+ accessToken);
GmailApp.setCurrentMessageAccessToken(accessToken);
var messageId = e.messageMetadata.messageId;
var card = createCard();
return [card.build()];
}
Thanks.
Your Apps Script code never runs on the client device, where the messages are selected in the Gmail UI - it runs on Google's servers. You can't access UI information from add-ons (there's no GmailApp.getSelectedThreads(), CardService.getActiveEmails(), etc.). You are only able to access the opened message / draft, and others in the same thread (or mailbox, depending on your add-on's scopes).
The event object received by your triggered callback function will only have a single access token. Until additional manifest triggers are added (currently only contextual and compose exist), this won't change.
As this is not Google, no one here can tell you if or when other triggers may or may not be added. You best bet is to request the feature.
I'm trying to build a simple log for sheets that makes use of cell notes.
Somehow i can't include the email of the user who triggers the onEdit-event.
function onEdit(e){
var email = Session.getActiveUser().getEmail();
var date = String(Utilities.formatDate(new Date(), 'Europe/Berlin', 'HH:mm dd/MM/yy'));
var range = e.range;
range.setNote(email + " # " + date);
}
The note appears on the cell but the email is empty. Could that have to do with missing permissions? I assume if something would be wrong with my code the note wouldn't appear at all on the edited cell in sheets...
Here's what Google documentation says about the Session class
The Session class provides access to session information, such as the
user's email address (in some circumstances) and language setting.
In regards to 'getActiveUser()' method, it states
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, like a
simple onOpen(e) or onEdit(e) trigger, a custom function in Google
Sheets, or a web app deployed to "execute as me" (that is, authorized
by the developer instead of the user). However, these restrictions
generally do not apply if the developer and the user belong to the
same G Suite domain.
Because you use the simple trigger - onEdit() - the code will not execute unless explicitly authorized by the user. For installable triggers, that would mean executing the function manually from the script editor.
You can publish the project as a sheets add-on. In this case, users will be asked to grant permissions declared in the add-on manifest.
More info https://developers.google.com/apps-script/reference/base/session
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.