Greg
Greg

Reputation: 311

401 Unauthorized error when trying to send Gmail from a different email address

I am trying to generate Google Doc using script and then send it to email. Everything works fine when I'm giving my email as "from" parameter(I generate this doc with my Google account).

GmailApp.sendEmail


(
   invoice.customerEmail, 
   'title', 
   invoice.emailBody, 
   {
     attachments: [doc.getAs(MimeType.PDF)],
     name: 'subject title',
     htmlBody: invoice.emailBody, 
     from: '[email protected]'
   }
 );

Problem arises when I type in other email in 'from'. I published it as web app. Here are the settings : settingsgoogle

Is there any way to fix it?

Upvotes: 0

Views: 311

Answers (1)

ale13
ale13

Reputation: 6072

According to the GmailApp documentation:

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

Therefore, you can use the sendEmail with other email addresses only if these email addresses are aliases of the email executing the script.

However, if you don't want to add this other email as your alias, you can simply create the script on this other account and execute it from there.

Reference

Upvotes: 1

Related Questions