Send email from a Google Script with another 'From' address - google-apps-script

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

Related

MailApp.sendEmail() not sending from current user

I have an onEdit triggered appscript that grabs data from a spreadsheet, formats it and then sends it to an email address. Typically these messages would be sent 'from' the user that is triggering the script to run. However, I am experiencing strange behavior where the current user is not used to send the message - or used inconsistently.
A couple of notes:
Occasionally the message is sent by the current user, other times it is not (in those cases it comes from my account, which created the script)
Script trigger is an 'onEdit' script which monitors the sheet for a checkbox status change
Spreadsheet is in a Shared Drive
We are using Google apps for education
The recipient address is a mailing list tool. When I list a different email address as the recipient (internal, external, other) the message consistently comes from the current user as expected. I've no idea why the recipient would affect how Google sends the message.
I realize i could use the GmailApp library to send instead, but want to avoid the re-authorizations that are mentioned.

MailApp.sendEmail function gives a generic error

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

onEdit event object active user is returning my email address instead of the user doing the edit

I have a Google sheet with an installed trigger / app script. The script uses the event object (I call it e) to do various things. One thing I am trying is to get the email address of the user who is doing the editing of the Google sheets. So, if the event object is e, the e.user should return the user id / email address of the person logged into the Google sheet and performing the action. But, what it does instead do is return MY email address. I'm guessing the e.user id is the id of the user the app script is running under. Can any Google engineers chime in? How can I get the user id of the person doing the editing?
Ok, wow, I finally got this working, and that was a weird one. So, if the trigger is installable as opposed to simple, and the users doing the editing belong to my same domain, then the e.user should work. It wasn't. What I eventually figured out is when one of the shared collaborators opens up the apps script editor from the container, for some reason, everything started working. So, I'm now able to get the user id's and emails from all the users in my domain when they perform an action. It seems like this is a bug. I dunno. But it works now.
You can obtain the User's Email with e.user.getEmail();
Here's the documentation comment:
getEmail()
Gets the user's email address, if available. If security policies do not allow access to the user's email address, this method 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 runs the script themselves or belongs to the same G Suite domain as the user.

send sms using Google Scripts on google spreadsheet

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.

GAS MailApp allways sends a copy of email to script owner's email too

I just wanted to know if this behaviour is normal. Is it suppose to send a copy of the mail to me as the script owner, as well as the intended recipient? For example this code:
function emailTest(){
MailApp.sendEmail("someemail#gmail.com","test subject","test body");
}
Will send out an email to someemail#gmail.com. I will also get a copy in my inbox as the script owner. Can I shut this off somehow?
Regards,
Shumway
Because you're sending the mail from your account, you'll see the message in your 'Sent' box. If you're seeing it in your Inbox, it's not related to the script itself, as this is only sending to "someemail#gmail.com".
Things to check:
You don't have a forwarding rule from "someemail#gmail.com" to your account.
You don't have a filter/mail client/ other service accessing your inbox and placing your sent mail in the inbox.
You're not looking at 'All Mail'. Etc, etc.
You can probably get help for this at: https://productforums.google.com/forum/#!categories/apps/mail-settings.