Forward email to a forwarding address manually using Apps Script - google-apps-script

I am trying to forward emails to a forwarding address, like a Gmail filter would, using Apps Script. The transferred email should therefore be an exact copy of the original one: same subject, body and from address (that's the tricky one).
My current code is a workaround and uses the 'usual' transfer function with some of its advanced parameters:
message.forward(
'dude#example.com',
{
name: message.getFrom(),
replyTo: message.getFrom(),
subject: message.getSubject()
}
)
The forwarded email however still appears as being sent from the forwarding address, and not the original sender address. This sometimes even messes up the Gmail threads...
[EDIT]
Aliases (see this question) are not the solution here since forwarding addresses can be a completely different account, especially in the context of a Google Workspace.

Related

Send email from a Google Script with another 'From' address

My Sheet-bound script is sending an email using MailApp.sendEmail.
The emails are always sent 'from' my own Gmail account. This project is for a customer, and I need his email to be the 'from' on these emails.
Reading similar questions I learn that the I only have flexibility in changing the name and replyTo address, using the advanced options of MailApp.sendEmail. However, the email address is still mine and Google doesn't offer any control over that.
I'm not familiar enough with all of the Google services and options to find the best way to do this. My customer does have a Google Apps for Business, but I don't.
Can I somehow create the email-sending function as a standalone script under his account, and somehow call it from the project under my account?
Any other ideas?
Thanks!
Emails are always sent from the account of the user that executes the script. In case the email is sent by a triggered function (installable triggers are the only ones that are able to send emails since it requires explicit authorization) then the email is sent from the account of the user that created the trigger (and authorized it).
In your case, it seems that the easier solution would be to ask your customer to execute the script himself and initiate all the triggers himself too.
If that should not be possible then you could indeed use a standalone script that would work as a kind of proxy, ie it would receive a request to send a message and actually send it from the customer account while returning an acknowledgement to your script.
But that's a bit tricky... the first solution is more elegant.
Edit :
I found the idea of sending emails through an independent script funny so I gave it a quick try and it seems to do the job pretty easily... test code below (this code should be deployed as a standalone app from your customer account) :
function doGet(e) {
Logger.log('e = e'+JSON.stringify(e));
if(e.parameter.recipient==null){return ContentService.createTextOutput("error, wrong request "+JSON.stringify(e)+"\n\n"+e.parameter.recipient+"\n\n"+e.parameter.subject+"\n\n"+e.parameter.body).setMimeType(ContentService.MimeType.TEXT)};
try{
MailApp.sendEmail(e.parameter.recipient, e.parameter.subject, e.parameter.body)
}catch(err){
return ContentService.createTextOutput('error : '+err).setMimeType(ContentService.MimeType.TEXT);
}
return ContentService.createTextOutput('mail successfully sent').setMimeType(ContentService.MimeType.TEXT);
}
note : the code below goes in your spreadsheet script, the doGet above is an standalone app running from your customer account.
function sendMail(recipient,subject,body){
var sent = UrlFetchApp.fetch("https://script.google.com/macros/s/---------------S381kO1Kqv61E/exec?recipient="+recipient+"&subject="+subject+"&body="+body);
Logger.log(sent);
}
function test(){
sendMail('recipient#gmail.com','test message','hello world')
}
I was able to send messages from my gmail account while being logged as a different user. (the url above is intentionally truncated, I don't want anyone to send email from my account ;-)

Mailapp.sendEmail control from user

I'm currently using this function in my google open sheets script ...
MailApp.sendEmail(emailAddress,emailAddress, subject, body);
The problem is that I can't control the from email address. Where does the from email get set at ? I can control the reply email address but that doesn't really address my problem.
Any thoughts ?
Thanks!
This question is more complex because you have both MailApp (Which you are using), and GmailApp (Which you are not, but can be used similarly to send mail).
The structure for the .sendEmail function is actually .sendEmail(recipient, subject, body, options), with options providing you the ability to modify things such as the 'from' address.
For the MailApp, you are not able to modify the from address, it's not one of the options. Speculating on the reasons for this are straightforward enough (Spammers would love scripts that allowed them to send mail from any email address they wanted), but pointless.
For the GmailApp, you can specify your from address as one of the options, but this is restricted to only selecting alias addresses from the GMail account currently running the script. You can do it in this format:
// Send an email specifying a from address
GmailApp.sendEmail('mike#example.com', 'Subject example', 'This is the body of the message', {
from: 'one-of-your-alias-addresses#mydomain.com', //Specify a from address, must be an alias of the sending account
name: 'John Doe'//Specify my name if I want
});
Note: Using this requires the user running the script to grant extra permissions I think.

GAS MailApp allways sends a copy of email to script owner's email too

I just wanted to know if this behaviour is normal. Is it suppose to send a copy of the mail to me as the script owner, as well as the intended recipient? For example this code:
function emailTest(){
MailApp.sendEmail("someemail#gmail.com","test subject","test body");
}
Will send out an email to someemail#gmail.com. I will also get a copy in my inbox as the script owner. Can I shut this off somehow?
Regards,
Shumway
Because you're sending the mail from your account, you'll see the message in your 'Sent' box. If you're seeing it in your Inbox, it's not related to the script itself, as this is only sending to "someemail#gmail.com".
Things to check:
You don't have a forwarding rule from "someemail#gmail.com" to your account.
You don't have a filter/mail client/ other service accessing your inbox and placing your sent mail in the inbox.
You're not looking at 'All Mail'. Etc, etc.
You can probably get help for this at: https://productforums.google.com/forum/#!categories/apps/mail-settings.

Google script mailapp.sendEmail only works with my email address

Okay this is very strange. I have scripting in a spreadsheet to take information from the currently active sheet, creates a report using a template file and sends it as an attachment.
This all works perfectly, when I use my Google apps domain account email address as the recipient. It doesn't work when I send to any other email address, other than my own. It was working yesterday. No errors generated when the script runs
The only thing that I did was change the owner of the spreadsheet to another user in our domain. It was shared with the other user while I was testing the scripts. I've tried using other email addresses in our domain and created a new spreadsheet with the sendemail function, all with the same behavior.
// Email the specified report
function emailReport(file, recipients, emailSubject, emailMessage) {
MailApp.sendEmail("someone#example.com", emailSubject, emailMessage,
{attachments: file, mimetype: 'application/pdf'});
}
I've been able to reproduce the problem and to find a workaround.
As it's not posible to send email to recipients without google account, I add that kind of emails on a cc field.
// Email the specified report as cc
function emailReport(file, recipients, emailSubject, emailMessage) {
MailApp.sendEmail("my#gmail.com", emailSubject, emailMessage,
{attachments: file, mimetype: 'application/pdf', cc:"someone#example.com"});
}
I noticed this question a while back, and even referenced it in another one. Since then I've noticed no one responded to you so...
It appears as if Google has recently changed (though not documented anywhere I've found) the MailApp.sendEmail function so that it only works when you use the email address belonging to the owner of the spread sheet.
My guess is this is to prevent the system being used for unsolicited mass emailing.
The other question was here
(Sorry, not an answer as such :( more of a confirmation as to what you are seeing seems to be as expected)
For anyone having this issue, use the GmailApp class instead of the MailApp class.
It supports the same functions, for example: GmailApp.sendMail('recipient#gmail.com', 'Subject', 'Message').
https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String)

Mail Merge Tool for Gmail With Ability to Change From Email Address

I know there are some Mail Merge tools for Gmail. Are there any that allow me to use a registered "send from" address in gmail? When I set a different sending email within the two scripts here, it still sends from my default gmail account.
http://www.labnol.org/internet/personalized-mail-merge-in-gmail/20981/
As a developer, you can code a script to use any of the aliases available in a Google account as the "from" address. Here is a Google Apps Script documentation reference with an example (and copied below):
// The code below logs the aliases for this Gmail account and sends an email
// as the first one.
var aliases = GmailApp.getAliases();
Logger.log(aliases);
GmailApp.sendEmail(
"recipient#example.com", "subject", "body", {from: aliases[0]});
You might consider this App as a mail merge tool
http://www.thexs.ca/xsmerge
It's available in the Chrome web store (free)