#Selective Mail Forwarding

1 messages · Page 1 of 1 (latest)

cursive jay
#

Detailed steps listed below

#

Go to all settings in your mail

#

This in the top right

#

then this thing

#

In this ribbon you should see the "Forwarding and POP/IMAP" section

#

Under that you have the first option as \

#

under that you should have a creating a filter option

#

so create the filter you should get this

#

Leave things blank that doesnt matter

#

you can list all of the emails you want to forward to under "TO" and the specific email adress you want to recieve messages from.

#

I however would add some keywords just in case you want to avoid specific details being forwarded

barren thorn
#

Thanks for your help!

#

Hmm...I'm not sure how this would work with the emails I get. I'll show you an example

#

You can see here that this email is sent to the student, and I get a copy of it sent directly to me as well. In this example, the student's email is: aaron.refreshwaterco@gmail.com

#

So I have to manually forward this email to that email address.

#

That email address will change every time, since it's sent to different people. But the format of the email will remain identical

#

So I wanted something that automates this process of forwarding the email address that's found on that first line of the email

#

Here is another example

#

@cursive jay

cursive jay
#

yup im here!

#

sorry I was on Robotics for the time

barren thorn
#

No problem

#

I just got home lol

#

did you see the examples I pasted?

cursive jay
#

Yeah

#

Ive been so god damn busy i havent replied

cursive jay
#

And then add then in the list

barren thorn
#

ok, that part is easy. But how do I get it to forward to the email address in each email?

cursive jay
#

You list the emails there

barren thorn
#

Yeah, but how do I pull the correct email address from the body of the email and set it to the To field?

cursive jay
#

Oh ok

#

then its more complex

#

hm

#

Let me think about it for a minute or two

barren thorn
#

Yeah I want to forward each email to the email address listed in the email.

#

Thanks

cursive jay
#

Ill come back to you

#

Ok most likely you will have to create a Google Script

#

if that is fine with you

barren thorn
#

Yeah that seems ok

cursive jay
#

mk

barren thorn
#

I've never used Google script though. Is it hard?

cursive jay
#

It might be a little overwhelming

barren thorn
#

Lovely

cursive jay
#

Of course Ill help you

barren thorn
#

Nice thanks 😄

cursive jay
#

Are the emails labeled?

barren thorn
#

Yes

#

You can see it in the examples I showed

cursive jay
#

I believe its labelled only for that sort of email right

#

can you create a custom label just for the ones to be forwarded?

barren thorn
#

Classroom/Access Email

ALL of these emails I want forwarded to the people its sent to.

cursive jay
#

I see

#

mk

#

no issues

#

I also believe its auto labeled?

barren thorn
#

Yep

cursive jay
#

Sick

barren thorn
#

And auto sorted into the folder

#

I keep my work email organized

#

All regular emails are sorted into labels and their respective folder.

cursive jay
#

real nice

#

Could you do me a favour if youre fine with it, for all the mails forwarded, could you give all of them a form of "Sent" or "Forwarded" Label? that makes it easier for the code

#

it makes the process much much faster too

barren thorn
#

So just an extra label called "Forwarded"?

cursive jay
#

just the ones you have sent out already

barren thorn
#

Yeah I can do that

cursive jay
#

it makes life easier for the code so it doesnt take it hours to search

#

Yeah thanks

barren thorn
#

I'll do that in the morning since I'm in bed now and it's almost 12:30 am 😂 I gotta wake up at 5 am

cursive jay
#

Ah ok

#

take rest

barren thorn
#

Thanks for your help. Night!

cursive jay
#

Ill get your code done with

#

Classroom/Access Email

this is the label I should look forward too right?

#

to*

#

This should be it!

#
function checkNewEmails() {
  var label = GmailApp.getUserLabelByName("Classroom/Access Email");  // Label before forwarding
  var forwardedLabel = GmailApp.getUserLabelByName("Forwarded");  // Label after forwarding
  var threads = label.getThreads(0, 10);  // Get the 10 most recent threads with the "Classroom/Access Email" label
  
  threads.forEach(function(thread) {
    var messages = thread.getMessages();
    
    messages.forEach(function(message) {
      if (!message.isInLabel(forwardedLabel)) {  // Only process emails without the "Forwarded" label
        var body = message.getBody();  // Get the email body
        
        // Regex to find the first email in the body
        var emailMatch = body.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/);
        
        if (emailMatch) {
          var recipientEmail = emailMatch[0];  // Extract the first email found
          
          // Forward the email to the recipient found
          GmailApp.sendEmail(recipientEmail, message.getSubject(), message.getBody(), {
            attachments: message.getAttachments()  // Forward any attachments
          });

          // Add the "Forwarded" label after forwarding
          message.addLabel(forwardedLabel);
        }
      }
    });
  });
}
#

ill let you know how to set it up

#

once your up ofc

cursive jay
#

Ok looking back at my code it just rewitres the email and does not forward it let me try to get an actual forward system

#

I made it much more complex there for no reason

#
function checkNewEmails() {
  var label = GmailApp.getUserLabelByName("Classroom/Access Email");  // Label before forwarding
  var forwardedLabel = GmailApp.getUserLabelByName("Forwarded");  // Label after forwarding
  var threads = label.getThreads(0, 10);  // Get the 10 most recent threads with the "Classroom/Access Email" label
  
  threads.forEach(function(thread) {
    var messages = thread.getMessages();
    
    messages.forEach(function(message) {
      if (!message.isInLabel(forwardedLabel)) {  // Only process emails without the "Forwarded" label
        var body = message.getBody();  // Get the email body
        
        // Regex to find the first email in the body
        var emailMatch = body.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/);
        
        if (emailMatch) {
          var recipientEmail = emailMatch[0];  // Extract the first email found
          
          // Forward the email to the recipient found
          message.forward(recipientEmail);
          
          // Add the "Forwarded" label after forwarding
          message.addLabel(forwardedLabel);
        }
      }
    });
  });
}
#

this should be MUCH easier

barren thorn
#

Nice, thanks!

#

I've created the new label

#

I've added it to all of the emails in the Access Email folder

#

Is it possible to add in "Please see below" as text for the email that is forwarded to them? Like what I do:

cursive jay
#

Hm

#

I think you can send another email in the same thread

#

I cant much today though i have a quick exam tonight so gotta study

#

Ill look into it

barren thorn
#

Thanks

cursive jay
#

Yo so sorry ive been inactive

#

My exams were going on

#

Yeah ill get them done

barren thorn
#

Sorry, I forgot to check this lol

cursive jay
#

Im trying to get my exams done with

#

DAMN

barren thorn
#

Nice

#

What exams are they?

cursive jay
#

My exams just ended

cursive jay
#

They suck

barren thorn
#

Are you in computer science?

cursive jay
#

Yeah

#

So many exams

#

Its ho estly frustrating

#

Ends next week though

cursive jay
#

Im finally free

cursive jay
#

lets get through your things now

#
function checkNewEmails() {
  var label = GmailApp.getUserLabelByName("Classroom/Access Email");  // Label before forwarding
  var forwardedLabel = GmailApp.getUserLabelByName("Classroom/Access Email/Forwarded");  // Label after forwarding
  var threads = label.getThreads(0, 10);  // Get the 10 most recent threads with the "Classroom/Access Email" label
  
  threads.forEach(function(thread) {
    var messages = thread.getMessages();
    
    messages.forEach(function(message) {
      if (!message.isInLabel(forwardedLabel)) {  // Only process emails without the "Forwarded" label
        var body = message.getBody();  // Get the email body
        
        // Regex to find the first email in the body
        var emailMatch = body.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/);
        
        if (emailMatch) {
          var recipientEmail = emailMatch[0];  // Extract the first email found
          
          // Forward the email to the recipient found
          message.forward(recipientEmail);
          
          // Add the "Forwarded" label after forwarding
          message.addLabel(forwardedLabel);
        }
      }
    });
  });
}

this is the final code i believe

#
function checkNewEmails() {
  var label = GmailApp.getUserLabelByName("Classroom/Access Email");  // Label before forwarding
  var forwardedLabel = GmailApp.getUserLabelByName("Classroom/Access Email/Forwarded");  // Label after forwarding
  var threads = label.getThreads(0, 10);  // Get the 10 most recent threads with the "Classroom/Access Email" label
  
  threads.forEach(function(thread) {
    var messages = thread.getMessages();
    
    messages.forEach(function(message) {
      if (!message.isInLabel(forwardedLabel)) {  // Only process emails without the "Forwarded" label
        var body = message.getBody();  // Get the email body
        
        // Regex to find the first email in the body
        var emailMatch = body.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/);
        
        if (emailMatch) {
          var recipientEmail = emailMatch[0];  // Extract the first email found
          
          // Forward the email to the recipient found
          message.forward(recipientEmail);
          
          // Send a follow-up message in the same thread
          thread.reply("Please see below \n\nRegards,\n\nCDS Admin");
          
          // Add the "Forwarded" label after forwarding
          message.addLabel(forwardedLabel);
        }
      }
    });
  });
}
#

This should be it

#

@barren thorn Could you confirm if this does what you need?

barren thorn
#

Thanks, so where do I enter all of that?

cursive jay
#

In google scripts

#

You can create a new project there and just add it in

barren thorn
#

Ok I'll check