Does MailApp.sendEmail only work for Gmail accounts? - google-apps-script

I have the following code attached to the OnBeforeDelete event in one of my data models in Google App Maker:
MailApp.sendEmail({
to: 'example#email.com',
cc: record.Resource.Manager.ManagerEmail ,
subject: 'Deleted Allocation',
body:
'February 2018: ' + record.February18 + '\n' +
'March 2018: ' + record.March18 + '\n' +
'April 2018: ' + record.April18
});
}
This code, and similar code like it, used to send emails correctly to both my personal Gmail account and an Outlook account. Now, however, they are only being received by my Gmail account. I tested again with a simpler function:
function myFunction() {
MailApp.sendEmail("example#email.com", "test mail", "why isnt this working");
}
Which confirms that it does not work when I type in the Outlook account instead of the placeholder email.
Does this only send email to Gmail accounts now (when it sent to Outlook earlier), or is there another explanation?

MailApp service does not have such restrictions; it can be used to send email to any address. Your test function myFunction worked as intended when I put in my (non-Gmail-related) email address in it.
If email is not being received, look for reasons on the receiving side. An overzealous spam filter may be to blame.

Related

GmailApp.sendEmail 'from' option stopped working

As of yesterday, Nov 10th 2016, my google script stopped working with the following error msg.:
Invalid argument: info#mydomain.ch (line 168, file "Code")
The line in question looks like this:
GmailApp.sendEmail("someone#theirdomain.com",
"Subject title",
"", {
from: "mrbob#mydomain.com",
name: "Mr. Bob",
htmlBody:
"Hello, <br><br> Bla blah blah... <br> Cheers,<br> Mr. Bob"
});
My email address (mrbob#mydomain.com) is set up to 'send mail as' in my gmail account. It is not an alias. The function in my code has been working well for the past year, so I don't know why it has suddenly stopped. Any ideas and solutions?
It's quite important that the from address be this one rather than my gmail address.
Google has recently made some changes in the MailApp / GmailApp service and you can now only send emails from accounts that are set as "aliases" in your main Gmail account.
Go to Gmail -> Settings -> Accounts and ensure that "set as alias" is written against the email address that you are using to send emails in the Google Script.
as of today (June 2017) The opposite of Karl_S response worked for me. I was using MailApp.sendmail(...)
I changed to
GmailApp.sendEmail('to#email.com', 'subject', 'message', {'from':'aliasinmyGmailaccount#otherdomain.com'})
and it worked.
this is the current documentation:
https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String,Object)
Try running the code from the editor with either the Run or Debug menu item. I have recently had a few scripts that have needed me to re-accept permissions. I have not looked into it to see if additional access is needed.
My emails are sending with this:
MailApp.sendEmail({
to: sendToEmailAddress,
bcc: emailAddressCC,
replyTo: emailAddressReplyTo,
name: emailFromName,
subject: emailSubjectText,
body: emailText,
htmlBody: emailHTMLBody
});
Can you try the MailApp class instead. From the documentation page:
This service allows users to send emails with complete control over
the content of the email. Unlike GmailApp, MailApp's sole purpose is
sending email. MailApp cannot access a user's Gmail inbox.
Changes to scripts written using GmailApp are more likely to trigger a
re-authorization request from a user than MailApp scripts.

MailApp/GmailApp fails to send bcc to address alias

I'm using GmailApp/MailApp to send notification emails, and I want to bcc my self using an "address alias", but no email is delivered to my gmail.
Example:
Email is sent to someone#somedomain.com <- Works
BCC is sent to mygmailacct+notifications#gmail.com <- Does not work
If I remove the "address alias" from my BCC, it works;
BCC is sent to mygmailacct#gmail.com <- Works
However this means I can't filter the notifications properly etc.
Sending an email to mygmailacct+notifications#gmail.com from some other email account works fine as expected.
Code:
function sendEmail(to, subject, body) {
// Default options
var options = {
name: 'System notifications',
bcc: 'gmail.accnt+notification#gmail.com'
};
MailApp.sendEmail(to, subject, body, options);
}
Rule:
Matches: to:(mygmailacct+notifications#gmail.com)
Do this: Apply label "notifications", Never send it to Spam, Mark it as important
Your code looks okay, It is probably skipping your inbox, try looking in All Mail or Sent Mail
To prevent clutter, Gmail doesn't route messages that you send to your
own alias to your inbox. You can find the message in Sent Mail or All
Mail. If you need to see messages sent to your alias in the Inbox, you
can configure the alias as an alternate "Send mail as" address for
your account.
— Source

Google Scripts: send email with GMail imported account

I'm creating a script with Google Script to perform a multiple email send with personalized text inside the email body (like name and addresses).
Is it possible to send the emails with an imported account (in GMail) via script?
Thanks
You can use send method of GmailApp:
https://developers.google.com/apps-script/reference/gmail/gmail-app#sendemailrecipient-subject-body
sendEmail(recipient, subject, body)
Sends an email message. The size of the email (including headers) may not exceed 20KB.
// The code below will send an email with the current date an time
var now = new Date();
GmailApp.sendEmail("mikemike#gmail.com", "current time", "The time is: " + now.toString());
Also, GmailApp.sendEmail support send from your alias as well.
sendEmail(recipient, subject, body, options) support use options to set options.from = 'alias#domain.com', as document saying:
from: String, the address that the email should be sent from, which must be one of the values returned by getAliases()

Google app script sends form email from personal email

How to change the email that sends the email of my google form script.
the form is accessible to 4 google accounts. I wrote it on mine. I want it to send from a different account, the one that is nobodies personal email.
Any idea?
for the record, this is, for the most part, the script
function generatePost(e){
var html = "hello";
// sends from my personal email
MailApp.sendEmail(e.values[7], ' ok!, ' + e.values[2], html,{htmlBody: html});
}
https://developers.google.com/apps-script/reference/mail/mail-app
It looks like you need to change the default replyTo address.
Look at the documentation on the function you're using: sendEmail(to, replyTo, subject, body)
The default of replyTo is "the user's email address". Just change it to a string of the email you want to use, i.e.:
sendEmail(e.values[7], "your_email#gmail.com", ' ok!, ' + e.values[2], html,{htmlBody: html})

MailApp.sendEmail Not Working?

I have several scripts in Google Docs spreadsheets that use MailApp.sendEmail to send e-mails incorporating data from the spreadsheets. They've all worked marvelously for years. About 2 weeks ago, they stopped sending the e-mails. The scripts still run and I don't get any errors. The e-mails just never come through. Nothing has changed, except that the e-mails aren't arriving anymore.
Any thoughts?
Thanks!
I think I have an answer to my own question, which may be helpful to anybody else who has this problem. MailApp.sendEmail DOES, indeed, still seem to be working. What has changed is the way Gmail is handling certain types of messages.
If I use MailApp.sendEmail to send an e-mail to a third party, it'll get through, no problem. As pointed out by Fashtas, if I use MailApp.sendEmail to send an e-mail to the Gmail account that the spreadsheet belongs to, no problem.
Here's the problem: if I use MailApp.sendEmail to send the message to a third party account that automatically forwards BACK to the Gmail account that the spreadsheet belongs to, those messages no longer get through.
Therefore, in the sample code I posted above, the matt#.com e-mail address automatically forwards back to my Gmail account. The messages sent by the MailApp.sendEmail make it through to the .com mail server, but for some reason, they don't get forwarded back to my Gmail inbox. They appear in the Gmail Sent Items folder (presumably because they were sent by the Gmail account that the spreadsheet belongs to), but they never hit the inbox.
That behavior is new. In the past, there has been no problem with those messages getting forwarded from the *.com server to my Gmail inbox. I don't know what changed to cause the problem. And I don't know how to fix it.
But I, therefore, think the answer to my question is that MailApp.sendEmail IS working as designed.
As explained by doebtown, MailApp.sendEmail() will refuse to send messages to any address that forwards to your account. For example, if you (the spreadsheet's owner) belong to a group, people#myorg.com, within your organization then mailApp.sendEmail() messages will not be delivered to people#myorg.com. That is, the following code will not send:
var email = 'people#myorg.com';
var subject = 'The Week Ahead';
var body = 'Hi there!';
var htmlbody = "<p>Hi there!</p>";
// this will not send
MailApp.sendEmail(email, subject, body,{
htmlBody : htmlbody
});
HOWEVER, you can work around this issue by appending your email to the addresses.
var email = 'myaddress#myorg.com, people#myorg.com';
var subject = 'The Week Ahead';
var body = 'Hi there!';
var htmlbody = "<p>Hi there!</p>";
// this will send to your address and the full list
MailApp.sendEmail(email, subject, body,{
htmlBody : htmlbody
});
This email will send to your address as well as to the list to which you belong. This is not a perfect solution, but is viable if you do not want to create a specific account for managing scripts.
You can apparently only send emails to the gmail/email account the spreadsheet belongs to.
There are multiple ways to send an email through Google App Scripts.
MailApp: Traditional method, however I could not get a single email to send. Introduction course here.
GmailApp: Same method as MailApp however using the below code worked to send emails.
GmailApp.sendEmail(emailList[index], "New email", "New email", {"cc":"XXXXXXX#gmail.com"});
Add a the Gmail API through Services. This options was not explored but documentation can be found here.
Conclusion: Don't use the MailApp, instead use GmailApp.