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

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.

Related

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

Click a _self targeted link twice in GAS?

First of all I am sorry if I use GAS tools incorrectly.
Context :
I need to generate a simple webapp including link that will be framed in a google-site.
target="_self" and .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL) are ensential for this reason. I cannot set target on any other values than _self, (I'm aware that the following code wok perfectly with _top)
see this previous question : Can't have link in Embedded web app in Google site?
Code :
Use this simple code sample deployed as an Web app :
function doGet(e) {
var count = Number(e.parameters.count) || 0
var html = "<a target='_self' href='https://script.google.com/macros/s/";
html += ScriptApp.getService().getUrl().substr(35); //the id of the script
html += "?count="+ (count+1) + "'>click me twice</a><br>" + count;
return HtmlService.createHtmlOutput(html).setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
It just insert a link to the curent page, counting in the get how many times the link is clicked.
Issues :
First you can see that the code dont work in the "latest code" preview (url ending by /dev). setXFrameOptionsMode dont seem to work on preview. We cant test it their but we can still deploy it so that's ok.
Once published (url ending by /exec) the web app seem to work correctly at first sight but you can click on the link only once.

google app script sending mail as PDF issue

A script was working fine 6 months ago and now is creating me some issue when sending a doc as pdf...-> My pdf loose his layout.
I am not loosing the doc's layout when i send the mail manually using (from the menu) file ->Email as attachment -> choose pdf
but when i use this (a script.) i've got 25 extra pages and i loose the oirginal layout of the document.
var fileq = DriveApp.getFileById('17Fj7JfYkQk9blablabla');
MailApp.sendEmail('somemail', "Attachment example", "blablabla", { name: 'Automatic Emailer Script', attachments: [fileq.getAs(MimeType.PDF)] });
What is going on ?
thanks for help,
another problem:
The problem occurs even when downloading more than 1 doc in pdf at time...very wierd..
The only thinhs who works in extract 1 file to pdf at time manually...all other stuff (scripting or manually) just modifiy the layout.
May be this problem is related only for big file..my docs are 20 to 25 pages long.
Who knows ?
Thanks for help anyway
The problem did not ocurr with me. You can see the sample here.
Please change the document ID in this script and run again sending to my mail and I will check the results.
It adding to me 24 extra page..changing the layout of the entire original document.

Embedding Google drive in a Google page

I am trying to show a specific privately shared drive folder in my Google page. I am using this guide: https://developers.google.com/apps-script/articles/embedding_docslist_in_sites
I am stuck on Step "8. Replace PASTE_PAGE_URL_HERE in the method getPageByUrl() by the url of your file cabinet."
I have tried every URL combination in "" and I just get a scripting error on the getPageByUrl() method.
I just tested this and it seems to work fine for me. Make sure you are using the full URL for your file cabinet page. For example, on my test site, I created a new page using 'file cabinet' in the template dropdown calling it "myfcpage". This gives me a new page with URL: "https://sites.google.com/site/mytestsite/myfcpage". Therefore my function call looks like this:
var page = SitesApp.getPageByUrl("https://sites.google.com/site/mytestsite/myfcpage");

How can I embed a link button in mail to be sent to Notes clients?

I have to send an email to several hundred users. Each will have a link tailored to the recipient so I need to generate the emails in script/code - I'm no Notes developer so I can't do this in Notes; I'm using C# and I'm pulling the list out of a SQL database.
There are some constraints:
The site that the link points to uses Integrated Windows Authentication.
The sender wants the link to be a button, rather than text.
The vast majority of recipients are running Lotus Notes 7.
I've tried creating an HTML mail but have had problems:
If I use a form with a submit button and action that points to the link, Notes tries to use its internal browser which fails (because the site uses Integrated Windows Authentication).
If I use an a href tag with an img tag in it, pointing to an image on a webserver, Notes refuses to display the image - i just get the red x box, even though the tags are valid if embedded in a web page.
Anyone know how I can do this?
I finally found a method that works: embedding the image in the email itself. I found the solution here. I'll include the critical stuff here, just in case.
There are three key components to the email: the plain text version, the html version and the image, all consructed as AlternateViews:
string imagePath = #"C:\Work\images\clickhere.jpg";
AlternateView imageView = new AlternateView(imagePath, MediaTypeNames.Image.Jpeg);
imageView.ContentId = "uniqueId";
imageView.TransferEncoding = TransferEncoding.Base64;
:
//loop to generate the url and send the emails containing
AlternateView plainTextView = AlternateView.CreateAlternateViewFromString(
BuildPlainTextMessage(url), null, "text/plain");
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(
BuildHtmlMessage(url), null, "text/html");
//set up MailAddress objects called to and from
:
MailMessage mail = new MailMessage(from, to);
mail.Subject = "ACTION REQUIRED: Do this by then or else";
mail.AlternateViews.Add(plainTextView);
mail.AlternateViews.Add(htmlView);
mail.AlternateViews.Add(imageView);
//send mail using SmtpClient as normal
:
//endloop
BuildHtmlMessage(string) and BuildPlainTextMessage(string) just return strings containing the messages. BuildHtmlMessage includes this to display the image in a link to 'url':
sb.AppendLine("<div>");
sb.AppendFormat("<a href=\"{0}\" target=\"_blank\">", url);
sb.Append("<img alt=\"Click here button image\" hspace=0 src=\"cid:uniqueId\" ");
sb.Append("align=baseline border=0 >");
sb.Append("</a>");
sb.AppendLine("</div>");
Hope this is of use to someone else, sometime.
I imagine no matter what link you use (button, text), you're going to run into that same problem where the Notes client launches its internal browser. That's a preference on the Notes client, and it could be different on all machines.
I would first try a standard text link to see if it has the same behavior. Maybe if that does work for some reason, you at least can deliver a workaround.
Regarding the image issue - is the image coming from the server using Windows Authentication? Make sure the image is open to the public and doesn't require authentication to see it (test in Firefox, and if you don't get a password prompt, you're safe)
I hate to say it, but you might have to request users copy a URL from their email to a specific browser. At least you'd know that would work.
I agree with Ken on the preference for the internal browser. The image not displaying - showing a red x - might be a preference also. I don't recall if it was available in R7, but in R8 there is a preference to not show remote images automatically. It is a security feature. It could also be a proxy issue - if they would have to login to their internet proxy server to get to the internet, and your image is on the internet...
There is also this but it fires off a security warning to the user with my Notes config:
<input type='button'
onclick=document.location.href='http://server/path';
value='Click here'
id='buttonID' class='button'
xstyle='background-color:red;color:white;'/>