How do I use the replyTo option parameter of replyAll()? - google-apps-script

Scenario
Create a script that can send a email as per user selection on google spread sheet.
When user going to send email first time in a day must send a new email.
If going to send second time check subject line if already exists then must be replyAll to that email.
Attempted code
if (threads[0]) {
threads[0].replyAll(emailBody, {
// 'replyTo': emailAddress, (Removed due to issue with Gmail)
'cc': emailAddressCC,
'bcc': emailAddressBCC,
'htmlBody': emailBody
});
} else {
MailApp.sendEmail(emailAddress, subject, emailBody, {
// 'replyTo': emailAddress,
'cc': emailAddressCC,
'bcc': emailAddressBCC,
'htmlBody': emailBody
}
Link to full script: GitHub
This script creates a menu onOpen "Send Mail".
So, when user selects some area from sheet and click on "Send Mail" button it calls funShowAlert() and sends an email.
Issue
When I attempt to add the recipient's email to replyTo, gmail returns an error.
Question: Is it possible to use replyAll without the replyTo option parameter, and am I doing something wrong with replyTo ?

I am doing something wrong with replyTo ?
Yes.
Read the documentation carefully:
Reply to the sender (using the replyTo address),
The 'replyTo address' here is referring to the replyTo address in the email to which are you replying. It is made apparent that this is NOT the replyTo option parameter by the fact that there is no such parameter passed in the example immediately following it.
replyTo as a parameter option is defined in the next entry:
an email address to use as the default reply-to address (default: the user's email address)
'The user' here being the user the script is running as. This means that the replyTo option should be the address you would want the recipient to reply to. Normally when sending via Gmail's web UI, this must be a Gmail Alias associated with that account; although some testing with Apps Script indicates that is not necessarily the case.
Your code as is, with replyTo removed, is sufficient.

Related

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.

Google app script sends form email from personal email

How to change the email that sends the email of my google form script.
the form is accessible to 4 google accounts. I wrote it on mine. I want it to send from a different account, the one that is nobodies personal email.
Any idea?
for the record, this is, for the most part, the script
function generatePost(e){
var html = "hello";
// sends from my personal email
MailApp.sendEmail(e.values[7], ' ok!, ' + e.values[2], html,{htmlBody: html});
}
https://developers.google.com/apps-script/reference/mail/mail-app
It looks like you need to change the default replyTo address.
Look at the documentation on the function you're using: sendEmail(to, replyTo, subject, body)
The default of replyTo is "the user's email address". Just change it to a string of the email you want to use, i.e.:
sendEmail(e.values[7], "your_email#gmail.com", ' ok!, ' + e.values[2], html,{htmlBody: html})

MailApp.sendEmail Not Working?

I have several scripts in Google Docs spreadsheets that use MailApp.sendEmail to send e-mails incorporating data from the spreadsheets. They've all worked marvelously for years. About 2 weeks ago, they stopped sending the e-mails. The scripts still run and I don't get any errors. The e-mails just never come through. Nothing has changed, except that the e-mails aren't arriving anymore.
Any thoughts?
Thanks!
I think I have an answer to my own question, which may be helpful to anybody else who has this problem. MailApp.sendEmail DOES, indeed, still seem to be working. What has changed is the way Gmail is handling certain types of messages.
If I use MailApp.sendEmail to send an e-mail to a third party, it'll get through, no problem. As pointed out by Fashtas, if I use MailApp.sendEmail to send an e-mail to the Gmail account that the spreadsheet belongs to, no problem.
Here's the problem: if I use MailApp.sendEmail to send the message to a third party account that automatically forwards BACK to the Gmail account that the spreadsheet belongs to, those messages no longer get through.
Therefore, in the sample code I posted above, the matt#.com e-mail address automatically forwards back to my Gmail account. The messages sent by the MailApp.sendEmail make it through to the .com mail server, but for some reason, they don't get forwarded back to my Gmail inbox. They appear in the Gmail Sent Items folder (presumably because they were sent by the Gmail account that the spreadsheet belongs to), but they never hit the inbox.
That behavior is new. In the past, there has been no problem with those messages getting forwarded from the *.com server to my Gmail inbox. I don't know what changed to cause the problem. And I don't know how to fix it.
But I, therefore, think the answer to my question is that MailApp.sendEmail IS working as designed.
As explained by doebtown, MailApp.sendEmail() will refuse to send messages to any address that forwards to your account. For example, if you (the spreadsheet's owner) belong to a group, people#myorg.com, within your organization then mailApp.sendEmail() messages will not be delivered to people#myorg.com. That is, the following code will not send:
var email = 'people#myorg.com';
var subject = 'The Week Ahead';
var body = 'Hi there!';
var htmlbody = "<p>Hi there!</p>";
// this will not send
MailApp.sendEmail(email, subject, body,{
htmlBody : htmlbody
});
HOWEVER, you can work around this issue by appending your email to the addresses.
var email = 'myaddress#myorg.com, people#myorg.com';
var subject = 'The Week Ahead';
var body = 'Hi there!';
var htmlbody = "<p>Hi there!</p>";
// this will send to your address and the full list
MailApp.sendEmail(email, subject, body,{
htmlBody : htmlbody
});
This email will send to your address as well as to the list to which you belong. This is not a perfect solution, but is viable if you do not want to create a specific account for managing scripts.
You can apparently only send emails to the gmail/email account the spreadsheet belongs to.
There are multiple ways to send an email through Google App Scripts.
MailApp: Traditional method, however I could not get a single email to send. Introduction course here.
GmailApp: Same method as MailApp however using the below code worked to send emails.
GmailApp.sendEmail(emailList[index], "New email", "New email", {"cc":"XXXXXXX#gmail.com"});
Add a the Gmail API through Services. This options was not explored but documentation can be found here.
Conclusion: Don't use the MailApp, instead use GmailApp.

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)

Add BCC email address to gmail mail merge script

Romain Vialard and labnol did a great job with the scripting for a mail merge for gmail:
http://www.labnol.org/internet/personalized-mail-merge-in-gmail/20981
The script lets you indicate whether or not you want to be BCC'd on all the emails sent. It assumes the email address to include in BCC is the one you are sending from.
How can the script be changed to allow me to BCC a different email address, not just the one i'm sending from?
In other words, I want to be able to input any email address to BCC, such as "emailtosalesforce#n-xvh0q8 2w.31ghxkeac.3.le.salesforce.com"
Why:
I am a user of Salesforce.com, and one simple way to automatically log emails in the online system is to BCC an "Email To Salesforce.com" address, such as "emailtosalesforce#n-xvh0q8 2w.31ghxkeac.3.le.salesforce.com".
You can put this email address as BCC in the script itself.
Are you using "Yet another mail merge", available in the Apps Script Gallery ?
If you open your spreadsheet and click on Tools > Script Editor, you can edit the script.
Search for "bcc" in the script and add the email address you want to use as bcc.
eg: bcc: "emailtosalesforce#n-xvh0q8 2w.31ghxkeac.3.le.salesforce.com"
Take a look at the documentation for more explanations:
https://developers.google.com/apps-script/class_gmailapp#sendEmail
If you're using "Yet another mail merge," perform a search for bcc. Where the code reads "bcc = (e.parameter.addMeAsBCCCheckbox == 'true') ? user : ''" replace "e.parameter.addMeAsBCCCheckbox == 'true') ? user : ''" with the email address you want to bcc to. Make sure you check the "I want to receive a copy of each email sent"
The final code should look like this:
function startStandardMerge_(e) {
var kind = (e.parameter.items == null) ? 'gmail' : 'docs';
var selectedTemplate = GmailApp.getThreadById(e.parameter.chosenTemplate).getMessages()[0];
var user = Session.getEffectiveUser().getEmail();
var bcc = "EMAIL ADDRESS YOU WANT TO BCC TO";
var name = e.parameter.chosenName;
var from = e.parameter.chosenFrom;
merge(kind, selectedTemplate, name, from, bcc);
This feature is built in to "Yet another mail merge". Add a column named "bcc" to your spreadsheet and it will automatically be used in the email's bcc field.
Old version required you to modify script/bcc section in script editor, not case anymore using "Yet another mail merge". Just add column named "bcc" to spreadsheet (as mdahlman advised). Works perfect. Note: did not show email was BCCed in outbox, but it worked (I used unique bcc email for email-to-salesforce, and it logged just fine).