Google Spreadsheet Script Editor MailApp.sendEmail not setting replyTo? - google-apps-script

I'm using Google Spreadsheet for customers to send in comments. When they submit a comment, it gets emailed to me. I use the MailApp.sendEmail method. It works as expected except for the replyTo "advanced argument":
var myAdvancedArgs = { htmlBody: myHtmlBody, replyTo: customerEmail };
MailApp.sendEmail("me#example.com", "Comments Form", myBody, myAdvancedArgs);
customerEmail is set properly because I have it output correctly in the body of the message. htmlBody works as I do get the HTML version of the email displaying in my GMail account. However, when I click the reply button in GMail, the To: address is myself. It appears the replyTo is not being set at all. But then I look in the headers of the email message by clicking the little triangle next to my name with the tooltip "Show Details" and it does mention the correct reply-to address:
reply-to: mycustomer#example.com
Is MailApp.sendEmail not setting the reply-to header correctly?
UPDATE: I've snipped a couple images to demonstrate what's happening. Either the reply-to header isn't set properly (maybe that's not how you do a reply to?) or GMail is not working properly for me.
First image, you see that there is a reply-to header created from my Google Spreadsheet script:
Second image, when I click the reply button in GMail, the to address is not filled in with the address in the reply-to header:

I found the reason to your (and my) problem.
What you are seeing is a Gmail bug, not an Apps Script bug. Basically,
if the reply-to is any email address that you have linked with your
address so you can send mail as that account from within gmail, then
reply-to is ignored. In other words, it will look broken to you, but
for any other users of your script it will work properly. Non-Gmail
users will see it work correctly in any case.

This is an old question, but if I needed it maybe others also need it!
From my testing the replyTo worked only when I sent the mail not to the form Acount mail.
If your form is on your#gmail.com acount send it to yourOtherMail#gmail.com
var reply = e.namedValues['Email'];
MailApp.sendEmail("NotFormMail#mail.com", "subject", "message",{"replyTo" : '"'+reply+'"'});

Just tested like this :
function myFunction() {
MailApp.sendEmail("serge----#gmail.com ", "test message",'empty body', {"replyTo" : "serge_test#yopmail.com"}); // replyTo son#insas
}
and it works as expected when I "reply" (using the reply button in gmail or in any mail client ) it shows up like this :
which is indeed the replyTo adress even if it is true that the sender is indeed the author of the script (which is also the expected behavior, see documentation about scripts)
So I'm afraid the issue you raised is invalid...

Related

How to add hyperlinks strings within gmail in google sheet using

I have a google form which connected to a google sheet. When filling out the questionnaire, the user will immediately get a mail based on which options the user chose.
Assume that the option:
A
B
C
D
If the user chose the option A, will get the content(with hyperlinks):
A1(with A1’s hyperlink)
A2(with A1’s hyperlink)
A3(with A1’s hyperlink)
My problem is that I can’t do this at the same time.
I only can do with only plain text.
You need to set de Advanced Options params to the MailApp.send();
For example
MailApp.send({
to: currentEmail,
subject: subjectLine,
htmlBody: `<p>Clic here for details</p>` // This htmlBody param makes an HTML link in Mail Body.
});
To see the full list of Advanced Options visit https://developers.google.com/apps-script/reference/mail/mail-app#advanced-parameters_1

Google Apps Script Gmail getPlainBody Line Breaks

With the Google Apps Script Gmail library, when I use the function GmailMessage.getPlainBody(), the API seems to take what used to be one paragraph and break it up into multiple, potentially by using a character limit. For instance, a paragraph of my email reads:
From the link you sent me, I gleaned that Gmail refers to their secure email as confidential.
But when I call this function on the email, it becomes:
From the link you sent me, I gleaned that Gmail refers to their
secure email as confidential.
And, when I split the email text on a new line delimitor and do a bit of cleanup to create an array with my output, I end up with:
['From the link you sent me, I gleaned that Gmail refers to their', 'secure email as confidential.']
I viewed this Reddit post, which seemed to deal with the similar problem. But, I tried the resolution suggested by the person who posed the question:
body = message.getPlainBody().replace(/\r\n\r\n/gm,'aaaLINEBREAKERaaa').replace(/\r\n/gm,' ').replace(/aaaLINEBREAKERaaa/gm, '\r\r').replace(/ /gm,' ')
And it didn't quite give me what I need. Has anyone else encountered this problem, and if so, do you have a suggested workaround? Thanks!
I had the same issue. In that case, I used a workaround.
When I checked the email, I noticed that the HTML body is included in the message body and the HTML body has the original paragraph, and I used this situation. So, in this workaround, the original text is retrieved from the HTML body and the HTML is converted to a text. By this, the original paragraph is obtained. The sample script is as follows.
Sample script:
This script uses Drive API for converting HTML to text. So pelase enable Drive API at Advanced Google services.
var message = // Here, please use your "message".
var html = message.getBody();
var id = Drive.Files.insert({title: "temp", mimeType: MimeType.GOOGLE_DOCS}, Utilities.newBlob(html, MimeType.HTML)).id;
var text = DocumentApp.openById(id).getBody().getText(); // or DocumentApp.openById(id).getBody().getText().trim();
DriveApp.getFileById(id).setTrashed(true);
console.log(text)
References:
getBody()
Files: insert

Google apps script - UrlFetchApp to get image blob gets timeout error

I have created a script to automatically send a subscription-confirmation e-mail when a client submits a form.
In the e-mail, I embed an image that is fetched from my website with the UrlFetchApp class (https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app).
var logoUrl = "https://my.site.com/path/to/logo.png"
var logoBlob = UrlFetchApp.fetch(logoUrl).getBlob().setName("logo");
...
MailApp.sendEmail({
to: client.email,
subject: "...",
htmlBody: // html with reference to image like this:
"<img src='cid:logo' width='107' height='160'/>"
,
inlineImages: {
logo: logoBlob,
}
});
Since last week, this UrlFetchApp line causes a time-out error (error message in Dutch):
Time-out: https://my.site.com/path/to/logo.png (regel 4, bestand 'inschrijvingsbevestiging', project 'inschrijvingsbevestiging')
This has worked before without any problem, the image file also is accessible from the given URL with a browser.
I'm new to javascript and google-apps-scripts so I have no idea where to start searching for a solution. The reference manual does not mention the time-out.
Any ideas on how to solve this?
EDIT When I change the logoUrl path to some other image (I tried "https://www.google.be/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png", the script works fine. So maybe there is a problem with my site?
EDIT 2 Since the script works fine with another logo, the problem must lie on my webhost. I worked around the problem by putting the logo on my Drive, as suggested in the comments.

GmailThread.reply does not have original message in body

I am replying to an email using GmailThread.reply(body)
But the problem is that unlike replying using gmail app on web or android, the replies do not have a copy of original message.
Here is my code:
function testreply() {
var threads = GmailApp.getInboxThreads();
Logger.log(threads[0].getFirstMessageSubject());
threads[0].reply("test reply");
}
My question is that is this the expect behaviour?
Is it possible to include the original message in the reply(just as gmail web which adds special formatting to the original message in reply)?
I found a similar question on Gmail Forum. According to this, you need to construct the entire message body to pass to the Reply method. You can get the body and headers from the original message to construct the final form.
Also try to check this for more information.

Gmail apps script getBody/getPlainBody without signature?

I'm calling messages[j].getPlainBody(); to output the body of an email and log it in a spreadsheet. I want to log the plain text of the email - not the signature, footer etc from the email. How can I do this?
I notice in Gmail there's a sort of tab that hides my signature when I'm writing/viewing emails - can I access this programmatically somehow to achieve the above?
The Gmail UI intelligently collapses the messages to hide previous messages or signatures. There is no way to do this in the API. You can log an enhancement request here - https://code.google.com/p/google-apps-script-issues/issues/list