Learn-to-send-email-via-google-script-html-no-server: Why does MailApp.sendEmail() send duplicate emails??

Created on 17 Jul 2017  Â·  13Comments  Â·  Source: dwyl/learn-to-send-email-via-google-script-html-no-server

If I set TO_ADDRESS = "example@hotmail.com". An email gets sent to that address and also the email address the google account belongs too. So, two emails are being sent at the same time and with the exact same content to both email addresses. This is incorrect behaviour. Emails should only be sent to the email address specified in the TO_ADDRESS variable. Is anyone else experiencing, what I believe to be odd behaviour? This google script stuff is turning out to be more hassle than it's worth. So far I've not had much success with it. It only works if a user is using Google Chrome.

question

All 13 comments

@how-can-i-help we have not experienced duplicate emails being sent.
Can you please elaborate?

Not 100% sure, but you may be seeing the email showing up as sent rather than getting two copies of it. You can configure rules in Gmail/Inbox to ignore/auto-archive these emails regardless. I haven't seen this behavior where it sends it to two addresses unless you specify it as such.

A Google Script is tied to the account that created it. As such, when you send an email using this API, you are limited by that fact. It is sending it in your name (so you cannot spoof that source ). I think it was mentioned in a previous issue how these emails show up in your sent mail as a result, and they could pop up in an inbox if you have another account linked to the same Gmail inbox due to how it handles threads.

You could look up an alternative API / service if this is absolutely required, but there is no way to change this functionality with the MailApp API to my knowledge. You _can_ change the reply_to if you want a recipient to respond to a different email address.

The same emails are being sent to both Inboxes - the one specified as TO_ADDRESS = "example@hotmail.com" and the account that the Google Script is tied to. This is crappy behaviour. I understand it showing up in the Sent box as it is sending it in my name but it should not be in the Inbox.

What is the header of the email message? It is actually being sent to your gmail address, or does it simply state it is in the from? This "crappy" behavior could be the result of user-defined settings, which makes this sent message always appear in your inbox rather than getting archived automatically. If I recall, it did this in Gmail but not Inbox, so it is a Gmail setting. Could be other things going on too, but hard to tell without seeing a code example or email headers.

Delivered-To: [email protected]
Received: by 10.237.37.55 with SMTP id v52csp2236345qtc;
Mon, 17 Jul 2017 15:38:12 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=googlemail.com; s=20161025;
h=mime-version:message-id:date:subject:from:to;
bh=fhr2oZSlQbIOSEHHTsPPIXINRLPcOI4gnGzHg0/lUvM=;
b=t4oFwXocYKp1Gdyfgt8JEiKOVY6GL7maxJCbxHD+wuLnxE1+sg+4RMJCU/stht8TxN
0ytgC2bdBmVoV95BjFKi0B+fAiMxgzohFGTzq4cLIuzanOhM/1se1LvU6D8360TOHt/z
SvHc3rc+m1Tc11zAdwqiuNlNKdDgLFBGjUc7I+Cp9BmuGuq4qgb1uOe+qdsB7GA92vHf
nj2j3Sr6z+PefMWy3BHz84cdOgUqoIGojFxuNLlQTp+pmOTXQbHc9nQLc8EBL46c8PwV
cFHlkwi7TWLeyN2BCU+a8M3iEvVEVS6heJmT394wNNDQttddSoBI6+2RwXG6LDv6tFG9
LUvQ==
MIME-Version: 1.0
X-Received: by 10.200.0.148 with SMTP id c20mr16672787qtg.24.1500331092308; Mon, 17 Jul 2017 15:38:12 -0700 (PDT)
Message-ID: f403045eed6ccd7d2405548b0d12@google.com
Date: Mon, 17 Jul 2017 22:38:12 +0000
Subject: Contact Form Submitted
From: [email protected]
To: [email protected]
Content-Type: multipart/alternative; boundary="f403045eed6ccd7d1605548b0d0f"

--f403045eed6ccd7d1605548b0d0f
Content-Type: text/plain; charset="UTF-8"; format=flowed; delsp=yes

firstname

lastname

message

email

[email protected]

--f403045eed6ccd7d1605548b0d0f
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

firstname

iv>

lastnam=
e

message

tom: 0'>email


--f403045eed6ccd7d1605548b0d0f--

email script - email.txt

I checked my headers, and they seemed similar. I am guessing this is the default functionality of the Google API.

My TO address is an alias/forwarded account that gets loaded into Gmail, like your Hotmail account. The FROM address is your default Gmail account. You may be seeing duplicates due to the DELIVERED-TO attribute, which may either occur 1) for the API to save an email in your inbox so you have a "sent" copy of the email OR 2) simply because one account (e.g., Hotmail) forwards into the Gmail account.

All I know is that for my two emails, it shows up as a single email outside of my inbox, but this could depend on your particular email settings or if there is no forwarding/aliasing between accounts (in case number 1 above).

You can set up Gmail filters to auto-read and archive these emails so that they do not bug you.

Either way, this sounds like this is the functionality of the MailApp API, so there is nothing more we can do that I know of. You can always use an alternative mailing service for advanced / custom functionality, and if anyone has any they have used successfully, feel free to mention them..

https://stackoverflow.com/questions/17355823/mailapp-sendemail-not-working

According to the post above, emails from an aliased/forwarded account inside Gmail won't spam your inbox. Not sure if this is true, as I feel like this hasn't been the case for me, but posting it just in case.

There was another issue possibly related, #57 though not sure.

Haven't heard back from OP. Haven't been able to reproduce. If anyone else has these issues, feel free to post here so we can figure it out. Thanks!

I too was getting mail sent to my own inbox while using MailApp.sendEmail().
Then i switched to GmailApp.sendEmail instead of MailApp.sendEmail() and it fixed the issue.
Now, I am not getting duplicate emails to my own inbox,

Interesting! I wonder if there is a bug with Google scripts API?

True.I too doubt that.
Well sorry for commenting on a closed issue.
I am new to github and missed out the closed status before commenting :(

Hi,
Have you by any chance encountered or heard about the below issue anywhere :

https://github.com/dwyl/learn-to-send-email-via-google-script-html-no-server/issues/322

Thanks and Regards,
Sree

On Sun, 28 Apr 2019, 22:47 Sean McKenna, notifications@github.com wrote:

Interesting! I wonder if there is a bug with Google scripts API?

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/dwyl/learn-to-send-email-via-google-script-html-no-server/issues/129#issuecomment-487398655,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AL6DC5JTB2CTYDLI7QDRJJLPSXL2RANCNFSM4DTKK5DQ
.

Wanted to thank Sree0808 for his suggestion, which worked for me.

Problem: using MailApp.sendEmail in a script to send an email as the result of a Google Form subscription. Worked fine in scripting environment, but when tested from the form, would result in two copies sent to the address specified in the script, and copies appearing in both the script account sent mail and inbox.

Fix: Switching to GmailApp.sendEmail resulted in correct behavior (one email to address, copy only in sent mail).

Note that I needed to run with GmailApp.sendEmail in the scripting environment first to update the permissions, or it wouldn't work at all from the form.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Doesn't send the e-mail to e-mail address.
onurusluca picture onurusluca  Â·  4Comments

Why I need to Copy your google sheet ?
joshiadvait8 picture joshiadvait8  Â·  3Comments

NPM package out of date
mckennapsean picture mckennapsean  Â·  3Comments

Review alternative methods of form serialization & XHR sending
mckennapsean picture mckennapsean  Â·  4Comments

doGet - Step 8
joseroma picture joseroma  Â·  3Comments