Google Script - Send Email using alias - google-apps-script

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?

Related

Sending MS Outlook mail from Google script

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.

Google Apps Script (MailApp Class / sendEmail Method) - Sending from Shared Mailbox

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);
}

Update user mail address via apps script with admin SDK

I'm trying to update users of my domain to a new adress ,basically the same command than gam which is : " gam update user userx username mailadress#x.com "
My goal is to make the same thing but using google apps script, the documentation isn't very helpful because "AdminDirectory.Users.update(resource, userKey)" is even not present in it.
function updateUserName() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var mailAdress= sheet.getRange(1, 1).getValue();
var user = AdminDirectory.Users.get(colAdresseMail);
user.emails = [address='newtest#mydomainname.com', primary=true];
Logger.log(user);
}
When I'm looking at logs, the address is now the new adress I want, but when I'm sending a mail to this adress, I have 2 mails , one with the mail I needed to have, and another mail delivery subsystem..
Maybe I forgot something?
In the web UI, the alias is added automatically when you change an email address. Based on my previous work with this and the results you're seeing, I believe GAM does it programmatically (GETs original address and inserts it as an alias during the change) if you're seeing that behavior. You could check this in the source for GAM here if you'd like to be 100% sure.
You will need to use something like the following insert to set the alias like you've updated the email address in your comment:
AdminDirectory.Users.Aliases.insert(alias, email)
The documentation on this can be found here within the Directory API section of the Google Developers site.

GmailApp.getAliases() returns a blank list

I have a user with aliases. In admin.google.com I can view all the aliases, but when with that user, I create a Google Apps Script with only this code:
var aliases = GmailApp.getAliases();
Logger.log(aliases);
The log is empty: "[]".
I try to create the script also with an Super Admin User and Admin Directory API enabled but the result is the same.
I need this method to send email like this:
GmailApp.sendEmail(email , "Subj.. ", "body....", {from: aliases[0]});
Why isn't this working?
There are account aliases for a Google Apps domain (e.g. Business, Education), which are for authentication during login to Google Apps.
There are also account aliases for GMail, which are separate from the Google Apps account aliases. These are the aliases that would show up when you call GmailApp.getAliases().
You need to log in to you gmail account, go to settings, and then Accounts. Here you add the alias that you have set up in admin.google.com. Click on "Add another email account that you own", and in the popup-window add the alias you created. Now the new alias will be listed in getAliases(), and can be used as from-address in the options of GmailApp.sendEmail().
One cool thing: this also works if you remove your alias in admin.google.com, and then add it as a group-address instead. Then set this as your alias (this proved useful for me)

It it possible to change the from address when using sendEmail?

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.