0
votes

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!

1

1 Answers

3
votes

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('[email protected]','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 ;-)