Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

google apps script - How can I keep the Reply-To setting in Gmail drafts created by GmailApp.createDraft() when sending the draft via Gmail website?

As part of sending a bulk of emails on Gmail, I want to configure the Reply-To address depending on the recipient using Gmail's alias feature and Google Apps Script.

I've come across a problem that the reply-to settings in Gmail drafts created by the Google Apps Script is disabled (nullified) when attempting to send the created draft via the Gmail website, and wondered if anyone can help me with workarounds.

Reproducing

My script looks something like below. My Gmail account, for the sake of providing an example, is [email protected]:

    var subject = 'An Eye-catching Email Subject';
    var recipients = [{
        email: '[email protected]',
        replyTo: '[email protected]',
        body: 'email text for [email protected]'
      },
      {
        email: '[email protected]',
        replyTo: '[email protected]'
        body: 'email text for [email protected]'
      }
      // which goes on for an order of under 100 recipients
    ];
    recipients.forEach(recipient => GmailApp.createDraft(recipient.email, subject, recipient.body, {
      replyTo: recipient.replyTo
    }));

When I open one of the created draft messages on the Gmail website, and then send the message by hitting the Send button, the sent email is set to reply to [email protected] and not the designated Reply-To aliases, like [email protected].

Alternatives

If I use the sendEmail() method instead of the createDraft() used above, everything works fine. The designated replyTo options are reflected in the actual emails sent.

The only reason that I use createDraft() instead of sendEmail() is that after the drafts are created, I want to make sure everything is alright before hitting the Send button, individually. I understand that I should probably just forget using createDraft(); I just can't figure out what Google intended to do with the replyTo option of createDraft(), and if anyone knows some other solution that I could take while sticking to createDraft(), I am more than grateful.

question from:https://stackoverflow.com/questions/65878696/how-can-i-keep-the-reply-to-setting-in-gmail-drafts-created-by-gmailapp-createdr

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Issue and workaround:

In my environment, I have experienced the same situation. At that time, I confirmed the following situations.

  • When the draft mail is manually sent at the browser in Gmail UI, Reply-To is removed from the header of the draft message, the draft message is sent.
  • When I saw the draft message, Reply-To is included in the header.
  • When the draft message including Reply-To is sent using a script like GmailApp.getDraft(id).send(), the message is sent by including Reply-To in the header. This is the same situation with GmailApp.sendEmail().

From above situation, as a simple workaround, how about sending the draft messages using a script as follows?

  1. Create the draft messages.
  2. Confirm each draft messages.
    • This is from The only reason that I use createDraft() instead of sendEmail() is that after the drafts are created, I want to make sure everything is alright before hitting the Send button, individually. in your question.
  3. When you found the modification points in the draft messages, please modify and/or remove them.
  4. After you confirmed all draft messages, and when you want to send them, you can send them using a script.

When above workaround is reflected to the script, it becomes as follows.

Sample script:

function myFunction() {
  GmailApp.getDrafts().forEach(e => e.send());
}
  • When this script is run, the current all draft mails are sent. Please be careful this.

Note:

  • When the draft messages include some draft messages you don't want to send, for example, I think that the filter using the star can be also achieved. When you want to send the draft messages which have the star, you can also use the following script.

      GmailApp.getDrafts().forEach(e => {
        if (e.getMessage().isStarred()) e.send();
      });
    

Reference:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...