Search code examples
dategoogle-apps-scripttimezonegmailgmail-api

How can I match the time zone of the Date: header of emails sent from Google Apps script to the time zone of the executing user?


I am trying to send an email from a Google Apps Script.
I am using the example below but the time zone of the Date: header of sent email is different from the time zone of executing user.

function sendEmailTest() {
  var to = "[email protected]";
  var subject = "Test";
  var timeZone = Session.getScriptTimeZone();
  var message = Utilities.formatDate(new Date(), timeZone, "yyyy/MM/dd HH:mm:ss");
  GmailApp.sendEmail(to,subject,message);
}

The email sent from that script is as below.

From:     [email protected]
To:       [email protected]
Subject:  Test
Date:     Fri, 9 Oct 2020 16:51:01 -0700

2020/10/10 08:51:01

The time zone of the date of executing user that is in message body is UTC+09:00, but the time zone of the Date: header of email is UTC-07:00.

How can I match the time zone of the Date: header of emails sent from Google Apps script to the time zone of the executing user?


Solution

  • It seems the Date field's timezone cannot be modified from the gmail api or the GmailApp in apps script. It "seems" to be based on the server the api connects to, which may be random. However, emails sent through MailApp's timezone are fixed at 00:00 UTC. The following results of "raw" Date: <date> were seen during testing. Note that clients may display the timezone using any selected language or timezone. Only the raw Date field as shown using "Show original" is recorded below:

    • Emails sent directly from user interface:

      • Timezone offset at your current browser timezone.
    • Emails sent using MailApp:

      • Timezone offset fixed at 0000. No offset. No timezone offset data is revealed.
    • Emails sent using GmailApp:

      • Timezone offset fixed-probably random or the timezone of the server currently accepting your request.(say -0600).
    • Emails sent using GmailAPI using Google API explorer:

      • Timezone offset fixed-probably random or the timezone of the server currently accepting your request.(say -0600).

    The timezone offset did not change with

    • IP address
    • Language/location change at Google settings
    • Timezone of the script project as seen in File> Project properties > Timezone
    • Date field set in the "raw" parameter in gmail api request body