Search code examples
google-apps-scriptgmailgoogle-workspace

GmailApp.sendEmail 'from' option not working


I can't get

GMailApp.sendEmail('[email protected]','Subject of this email',Logger.getLog(),
     {name:'Save attachments from triggered attachment script',from:'[email protected]'} 

to actually put the from email address in send emails from my Google Workspace account. All emails from the script have [email protected] (which is the default address for my domain) as the from email address.

I can send with the [email protected] from the Gmailapp without a problem. It is just that within GAS it doesn't work. I also tried different aliases. Same results.

[email protected] is setup as an alias. I tried with both 'treat as an alias' checked and unchecked

Do you have an idea as to what the problem might be? Thanks in advance.

Regards, Peter Berkhout

EDIT: I copied and pasted the code to the inputbox here and after that I noticed that the G from MailApp was missing so I added the G manually. Turns out that there also is a function MailApp.sendEmail(to,subject,body,options)... When I changed my code to GmailApp.sendEmail(to,subject,body,options) it worked. So there was no bug, it was me all the time...

recap:

MailApp.sendEmail('[email protected]','Subject of this email',Logger.getLog(),
     {name:'Save attachments from triggered attachment script',from:'[email protected]'} 

doesn't allow the from to be overridden. (It just ignores it)

And

GmailApp.sendEmail('[email protected]','Subject of this email',Logger.getLog(),
     {name:'Save attachments from triggered attachment script',from:'[email protected]'} 

Works like a charm.


Solution

  • Since the issue was resolved above, I'm writing this response as a community wiki.

    The option to send emails from an alias is within the class GmailApp, you can check in the advanced parameters list, you will find the from parameter.

    from - String - the address that the email should be sent from, which must be one of the values returned by getAliases()

    The class MailApp does not list the from parameter, this class has other advanced parameters, which are listed here.