MailApp.sendEmail Not Working?
Asked Answered
D

5

11

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!

Drenthe answered 28/6, 2013 at 1:21 Comment(2)
I mean, I just tried the most basic debugging script:function myFunction() { MailApp.sendEmail("matt@******.com", "test", "test") } And IT didn't work, either.Drenthe
Has anyone at Google Confirmed this anywhere? we are trying to send email from a Google Spreadsheet Script but it does not work...Trilateration
D
16

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.

Drenthe answered 10/7, 2013 at 20:50 Comment(4)
Ah, that's so annoying. I've got a big sheet with our student administration in it that occasionally sends reminders to students about their projects. However, as it happens most of our students have forwarded those accounts to a gmail account. I was wondering why I never received replies...Grady
There's a chance all of your emails did actually go through! Check another inbox on the list other than your own. Normal behaviour, when sending to a list your are yourself a part of (on gmail, i.e. [email protected]) you would not get a copy since you are the author of that email. It's possible the old script behaviour was bugged, and the fixed version, since it sends mail as the script owner (your account), is actually working as intended. Thus appending your own email, after the list, should be the accepted answer (if wanting a copy sent to yourself). Can test this theory tomorrow at work.Bim
Have similar issue with both GmailApp and MailApp. With the difference that via MailApp it sometimes not sending even to my own email, and with GmailApp it sends to my own but not always sending to other gmail accounts. How to solve?Pansie
this is very frustrating. I am having a similar issue with my bankDuration
K
4

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 body",
      { "cc": "[email protected]" }
    );
    
  • Add a the Gmail API through Services. This options was not explored but documentation can be found here.

Conclusion: Don't use MailApp, instead use GmailApp.

Karp answered 14/1, 2021 at 22:42 Comment(0)
R
2

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, [email protected], within your organization then mailApp.sendEmail() messages will not be delivered to [email protected]. That is, the following code will not send:

var email = '[email protected]';

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

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.

Raina answered 6/5, 2016 at 5:42 Comment(3)
Can anyone confirm that this really does send an email to the list (and thus emails belonging in the list) as well as your own? I am a member of the list I am targeting, and am only getting 1 email. I would expect to get 2, if this code really did work, since I should get one copy sent from the gmail list and 1 copy sent directly to me...Bim
This continues to work with all of our automations. You should see one email thread with both of your to addresses on it, [email protected], [email protected] in your inbox just as if you had emailed yourself and that list via gmail. Are you working with a minimal model that does send to you but then fails when you add the group to the to list?Raina
I just confirmed that the mwm from this post still works in a new script. I can get it to fail by using our aliased domain name if that's a possible issue.Raina
G
1

You can apparently only send emails to the gmail/email account the spreadsheet belongs to.

Glasgow answered 28/6, 2013 at 2:32 Comment(8)
WHAT? Since when?! Can you point me to some documentation on this issue?Drenthe
No documentation, however I tested it and my gmail address was the ONLY one that workedGlasgow
And there was this message on stackoverflow: (#15697791)Glasgow
Hunh. You think that's a BUG? Or that they've changed the features to avoid spam or something like that?Drenthe
It has to be a bug. I have one script that is acting up also. It actually sends and email to me when it's not supposed too. Very strange stuff going on with it.Boo
I thought it was because they were trying to reduce spam or unsolicited emails.Glasgow
@Fashtas, that's what I'm afraid of. If it's not a bug, then it's a major change to the available functionality. And if that's the case, I wish there would be some kind of official word so that I can rethink how I'm going to handle these tasks that I'm currently managing with MailApp.sendEmail.Drenthe
I was able to send to a yahoo accountDuration
C
1

MailApp does not seem to work properly nowadays in a number of cases. I am calling MailApp.sendMail() from an installed trigger and, even if the email looks like it was sent (since it is in the "Sent" folder) it never reaches its destination.

However, using GmailApp.sendMail() instead worked like a charm. The method has a similar signature and the same permissions are required so it's very easy to just replace MailApp with GmailApp.

Cockchafer answered 28/2, 2023 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.