Sending email from a linked account (Google Apps Script) - google-apps-script

I have a script which sends an automated email. I want this to be sent from one of my linked accounts' email addresses rather than my main one, and I had understand that the advanced option 'from' could be used to achieve this... but it is not working. Here is the sending code:
MailApp.sendEmail(toEmailAddress, subject, message, {
htmlBody: htmlMessage,
name: "Test Name",
from: "yyy#gmail.com"
});
So the message sends, and the name does indeed show up as 'Test Name' but the email is still my main on and not yyy#gmail.com...
I have double checked and this email address is definitely set up in my main account as linked and shows under the 'send email as' section in the settings.

I think you can use the getAliases() method to do this. It's in the reference guide under GmailApp methods.
getAliases()
Gets a list of the emails that are set up as aliases for this account in Gmail.
You can send a message from any of these aliases by using the "from" optional argument.
// Log the aliases for this Gmail account and send an email as the first one.
var me = Session.getActiveUser().getEmail();
var aliases = GmailApp.getAliases();
Logger.log(aliases);
if (aliases.length > 0) {
GmailApp.sendEmail(me, 'From an alias', 'A message from an alias!', {'from': aliases[0]});
} else {
GmailApp.sendEmail(me, 'No aliases found', 'You have no aliases.');
}

Related

I want to setup manually the 'from' advance parameter on GmailApp object, without using getAliases()

I'm working on a Google Scrip to send an email using GmailApp with the basic parameters (recipient, subject, body). However, I want to use the advance parameter 'from', I'm collecting the email addresses on the spreadsheet (so I have the email from the sender), so I want to use that value on the 'from' advance parameter at the moment to send an email.
Any Ideas?. This is my current code:
function formSubmitReply(e) {
var recipientEmail = 'test#gmail.com';
var subjectEmail = "New User Form Request";
//Here is where I'm using the getAliases but is not working and I already have the sender's Email on the spreadsheet
var fromEmail = GmailApp.getAliases();
Logger.log(fromEmail);
//------------------------------------------------------------------------------------------------------------------
var msg = "<p>Hey IT Team</p>"+
"<p>A request has been submitted</p></br>"+
"<p>Test Field: "+e.values[0]+"</p></br>"+
"<p>Test Field 2: "+e.values[1]+"</p></br>";
if (fromEmail.length > 0){
GmailApp.sendEmail(
recipientEmail,
subjectEmail,
msg,
{
from: fromEmail[0]
});
}else{
GmailApp.sendEmail(
recipientEmail,
subjectEmail,
msg);
}
}
As of August 2020, that is not possible but you could submit a feature request through the Google Apps Script issue tracker.
Resources
https://developers.google.com/apps-script/support#missing_features
As per my testing and documentation of from parameter here, unfortunately, you cannot manually add an alias. An Alias must be one of the values returned by getAliases()
If by any chance you want to use aliases then you would have to add them in your Gmail settings first. You can check how to do that in one of the stack overflow answers here.

How to stop MailApp.sendEmail from sending myself a copy of the email

We are using Google Scripts to send emails after Google Forms are submitted. In the last day or two, we started receiving email delivery failures every time a form was submitted and the script ran. It is only unable to deliver messages to one account(sender#mydomain.com), which happens to be the account that the script is running as. However, this account should not be receiving a copy of this form anyways. All of the email addresses that are in the "To" field are getting the email with no issue, we are just trying to find out why we are getting this error message.
We are using the MailApp.sendEmail function and have been for years without any problem. We can't use the GmailApp functions because this account is not able to use Gmail and we've never needed to be able to in order to send emails.
In the script, when I add sender#mydomain.com to the To list, I receive the email and do not get any error messages. When I remove it from the To list, the rest of the recipients continue to receive the email but I get that error message again for sender#mydomain.com.
function formSubmitReply(e) {
var replyToAddr = "no_reply#mydomain.com";
var emailAddress = e.values[2]; // + ", sender#mydomain.com";
//Removed section that creates PDF, stores as the variable pdf
var subject = "Form Request from " + e.values[1];
var messageHtml = "<font size = 3><font face = arial>Form Request from: " + e.values[1];
var messagePlain = messageHtml.replace(/\<br\/\>/gi, '\n').replace(/(<([^>]+)>)/ig, "");
MailApp.sendEmail(emailAddress, subject, messagePlain, {htmlBody: messageHtml, name: "Sender", attachments: pdf, replyTo: replyToAddr});
}
It sounds weird to me that MailApp was working on an account that doesn't have Gmail enabled as MailApp affects the email sent Gmail quota but then I found No reports a 550 recipient rejected from MailApp. The only answer at this time looks to be wrong as MailApp sent emails are saved as sent emails on Gmail mailbox of the effective user that ran the script.

Sending email to a gmail contact list using a Google App Script

Picked up some code from this answer:
Email a group and not individual addresses with Google App Script
Seems to be working but I'm having this problem with the code. When I substitute emails for the send line... I get the following:
Message details
Invalid email: [L<?>;#11773e2
However, the Browser box msg for the email_list has the proper emails.
The code is as follows:
var email_list = [];
var contacts = ContactsApp.getContactGroup('TESTING').getContacts();
Browser.msgBox(contacts);
for(var i in contacts){
email_list.push(contacts[i].getPrimaryEmail());
}
Browser.msgBox(email_list);
MailApp.sendEmail({
to: email_list,
subject: "SUBJECT",
htmlBody: html2,
inlineImages:emailImages});
}
MailApp.sendEmail method expects to key's value to be a single string(composed of a list of emails separated by a ,), while yours is a array. That might be the problem. Try toString() or join():
email_list.join();
If that's not the case, You'd need to examine all emails in the list for any invalids.

How to let googlemail send an automatic mail -from- a secondary mail adress?

I'd like to send a mail with GoogleScript - not from my main-Gmailaccount, but from a secondary mail account, which is setup correctly in the main Gmail account.
I can send emails from the secondary mail account manually in Gmail, so that works fine. So to make it clear again: it shouldn't send as abc#gmail.com, but abc#domainname.com)
In G-Script I use MailApp.sendEmail({ - in the documentary it seems that there are just options to set the name of the sender and set the replyTo adress. If I do this the abc#gmail.com still appears as the sender. Is there a way to change the sender itself?
Documentary: https://developers.google.com/apps-script/reference/mail/mail-app
If your secondary email is setup as an alias of your email account it is possible. The specification mention:
from: the address that the email should be sent from, which must be
one of the values returned by getAliases()
When you execute getAliases(), do you see your alias?
// Log the aliases for this Gmail account and send an email as the first one.
var me = Session.getActiveUser().getEmail();
var aliases = GmailApp.getAliases();
Logger.log(aliases);
if (aliases.length > 0) {
GmailApp.sendEmail(me, 'From an alias', 'A message from an alias!', {'from': aliases[0]});
} else {
GmailApp.sendEmail(me, 'No aliases found', 'You have no aliases.');
}
https://developers.google.com/apps-script/reference/gmail/gmail-app#getAliases()

MailApp sending gmail account anonymous account

I have a script that sends an email to the person who submitted a form to me. However, I would like to send the email from an "anonymous" account so the confirmation email doesn't come from my account. Is there a way to send it from a different account so it doesn't look like I am manually generating all these emails. Thanks!
My script is something like the following:
if (user = email) {
GmailApp.sendEmail(user, subject, message);
}
Where 'user' is the google account of the person submitting and 'email' is the email address the person submitting wishes to have the confirmation sent to.
Again, is there a way to make the GmailApp.sendEmail(from:'Anonymous') ???
Well, I can't answer fully that it can be anonymous. You could author the app with a "dummy" account to obscure your email address, but if you're on a business or education account that won't really work. Also, you can put a little bit of smoke to distract the recipient by giving the email an "alias" name.
MailApp.sendEmail(userEmail,
"Help Desk Ticket",
"Thanks for submitting your issue. \n\nWe'll start " +
"working on it as soon as possible. \n\nHelp Desk",
{name:"Help Desk"});
}​
The most important part of that is the element in the last line {name:"Help Desk}. This makes your email "name" Help Desk, but you can still see the sending email address if you mouse over it (which is why I recommended a "dummy" email address in the beginning).
You can see the context of the script in this tutorial: https://developers.google.com/apps-script/articles/helpdesk_tutorial
Here is an example of how I used the same thing in a script:
function mailCheatSheet(copyId,userEmail,mrNumber){
var copyId = copyId;
var userEmail = userEmail;
var mrNum = mrNumber;
// Convert temporary document to PDF by using the getAs blob conversion
var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = "SA Funding request by: "+userEmail;
var body = userEmail +" has submitted a SA Funding Inquiry for " +mrNumber +", which is attached to this email. \n"
+"Please ensure there are no errors before printing. \n"
+"If there are errors, please notify: xyz#abc.com. \n\n";
MailApp.sendEmail(userEmail, subject, body, {name: 'SA Worksheet Helperbot', htmlBody: body, attachments: pdf});
// Delete temp file
DocsList.getFileById(copyId).setTrashed(true);
}
If you just want to make it look like you're not sitting around all day typing out email responses to things (I'd assume so your boss thinks you're doing something useful), then you could add the alias name to your GmailApp method AND add a disclaimer line at the bottom of the email body like this:
var body = userEmail +" has submitted a SA Funding Inquiry for " +mrNumber +", which is attached to this email. \n"
+"Please ensure there are no errors before printing."
+"\n\nThis email was automatically authored by Form Helperbot. If there is an error, please report it to: dummyEmail#xyz.com"
MailApp.sendEmail(userEmail, subject, body,{name: 'Form Helperbot',htmlBody: body, attachments: pdf});
If you want to send from an anonymous account only because you don't want to receive the replies, it might be a better idea to specify a dummy reply-to address in your email notifications.
MailApp.sendEmail(userEmail, subject, body, {name: 'SA Worksheet Helperbot', htmlBody: body, attachments: pdf, replyTo: donotreply#example.com});