#Selective Mail Forwarding
1 messages · Page 1 of 1 (latest)
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
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
Try to find a common word specific to these mails
And then add then in the list
ok, that part is easy. But how do I get it to forward to the email address in each email?
Yeah the field To:
You list the emails there
Yeah, but how do I pull the correct email address from the body of the email and set it to the To field?
Oh I see you want to send it to a mail SPECIFIC in the mail
Oh ok
then its more complex
hm
Let me think about it for a minute or two
Ill come back to you
Ok most likely you will have to create a Google Script
if that is fine with you
Yeah that seems ok
mk
I've never used Google script though. Is it hard?
It might be a little overwhelming
Lovely
Of course Ill help you
Nice thanks 😄
Are the emails labeled?
I believe its labelled only for that sort of email right
can you create a custom label just for the ones to be forwarded?
Classroom/Access Email
ALL of these emails I want forwarded to the people its sent to.
Yep
Sick
And auto sorted into the folder
I keep my work email organized
All regular emails are sorted into labels and their respective folder.
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
So just an extra label called "Forwarded"?
Yeah pretty much
just the ones you have sent out already
Yeah I can do that
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
Thanks for your help. Night!
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
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
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:
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
Thanks
Sorry, I forgot to check this lol
My exams just ended
Are you in computer science?
Im finally free
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?
Thanks, so where do I enter all of that?
Ok I'll check