Mailapp.sendEmail control from user - google-apps-script

I'm currently using this function in my google open sheets script ...
MailApp.sendEmail(emailAddress,emailAddress, subject, body);
The problem is that I can't control the from email address. Where does the from email get set at ? I can control the reply email address but that doesn't really address my problem.
Any thoughts ?
Thanks!

This question is more complex because you have both MailApp (Which you are using), and GmailApp (Which you are not, but can be used similarly to send mail).
The structure for the .sendEmail function is actually .sendEmail(recipient, subject, body, options), with options providing you the ability to modify things such as the 'from' address.
For the MailApp, you are not able to modify the from address, it's not one of the options. Speculating on the reasons for this are straightforward enough (Spammers would love scripts that allowed them to send mail from any email address they wanted), but pointless.
For the GmailApp, you can specify your from address as one of the options, but this is restricted to only selecting alias addresses from the GMail account currently running the script. You can do it in this format:
// Send an email specifying a from address
GmailApp.sendEmail('mike#example.com', 'Subject example', 'This is the body of the message', {
from: 'one-of-your-alias-addresses#mydomain.com', //Specify a from address, must be an alias of the sending account
name: 'John Doe'//Specify my name if I want
});
Note: Using this requires the user running the script to grant extra permissions I think.

Related

Forward email to a forwarding address manually using Apps Script

I am trying to forward emails to a forwarding address, like a Gmail filter would, using Apps Script. The transferred email should therefore be an exact copy of the original one: same subject, body and from address (that's the tricky one).
My current code is a workaround and uses the 'usual' transfer function with some of its advanced parameters:
message.forward(
'dude#example.com',
{
name: message.getFrom(),
replyTo: message.getFrom(),
subject: message.getSubject()
}
)
The forwarded email however still appears as being sent from the forwarding address, and not the original sender address. This sometimes even messes up the Gmail threads...
[EDIT]
Aliases (see this question) are not the solution here since forwarding addresses can be a completely different account, especially in the context of a Google Workspace.

How do I use the replyTo option parameter of replyAll()?

Scenario
Create a script that can send a email as per user selection on google spread sheet.
When user going to send email first time in a day must send a new email.
If going to send second time check subject line if already exists then must be replyAll to that email.
Attempted code
if (threads[0]) {
threads[0].replyAll(emailBody, {
// 'replyTo': emailAddress, (Removed due to issue with Gmail)
'cc': emailAddressCC,
'bcc': emailAddressBCC,
'htmlBody': emailBody
});
} else {
MailApp.sendEmail(emailAddress, subject, emailBody, {
// 'replyTo': emailAddress,
'cc': emailAddressCC,
'bcc': emailAddressBCC,
'htmlBody': emailBody
}
Link to full script: GitHub
This script creates a menu onOpen "Send Mail".
So, when user selects some area from sheet and click on "Send Mail" button it calls funShowAlert() and sends an email.
Issue
When I attempt to add the recipient's email to replyTo, gmail returns an error.
Question: Is it possible to use replyAll without the replyTo option parameter, and am I doing something wrong with replyTo ?
I am doing something wrong with replyTo ?
Yes.
Read the documentation carefully:
Reply to the sender (using the replyTo address),
The 'replyTo address' here is referring to the replyTo address in the email to which are you replying. It is made apparent that this is NOT the replyTo option parameter by the fact that there is no such parameter passed in the example immediately following it.
replyTo as a parameter option is defined in the next entry:
an email address to use as the default reply-to address (default: the user's email address)
'The user' here being the user the script is running as. This means that the replyTo option should be the address you would want the recipient to reply to. Normally when sending via Gmail's web UI, this must be a Gmail Alias associated with that account; although some testing with Apps Script indicates that is not necessarily the case.
Your code as is, with replyTo removed, is sufficient.

How to set user's email address in From: header using GmailApps class of Google Apps Script

I wrote a script to format the text that input on Google Forms and send it by email. The email could be sent but the email address of the user who entered the text cannot be set in the From: header.
I already read the Google Apps Script API documentation. And, I already know when I use the Gmail Apps class to send emails, I can set only the Google Form owner's email address in the "From:" header.
var options = {from: Session.getActiveUser().getEmail()};
GmailApp.sendEmail(to_address, subject, message, options);
I want to set the email address of the user who entered the text in the From: header. However, my email address who is the owner of the form is set.
GMailApp (and MailApp) only allows you to use your own email address or an alias address linked with your account. This is to prevent people from acting like someone else by putting other people's addresses in the from field. If the script editor can freely change the From field, he could write anything he wants in the body and send it, posing as someone else.
It's very likely that you are using the on form submit installable trigger and in such case the GmailApp/MailApp services will send the email using the settings of the user who created the installable trigger.
To send an email on behalf of another user, instead you should use a more complex script. By one side you should use the Gmail Advanced Service, by the other side you should set this the domain-wide delegation for this script but this is only available for G Suite accounts and the domain-wide delegation requires G Suite Superadmin privileges.

Google script mailapp.sendEmail only works with my email address

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)

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.