Sendmail inlineimages are also attachments but should not be - google-apps-script

I am using the final part of this answer to to send an images from Drive as an inlineimage. https://stackoverflow.com/a/41292754/1045794
function sendPicsInline() {
var picture1 = DriveApp.getFileById('0BxDqyd_bUCmvN1E3N0dQOWgycFE'); //public with link
var picture2 = DriveApp.getFileById('0BxDqyd_bUCmvTFNjRkRXbXA2Tms'); //public with link
var inlineImages = {};
inlineImages[picture1.getId()] = picture1.getBlob();
inlineImages[picture2.getId()] = picture2.getBlob();
MailApp.sendEmail({
to: 'testa#example.com, testb#example.com',
subject: "This is a test",
body:"Test message",
htmlBody: 'Test message with pics inline <br>' +
'first:<br><img src="cid:' + picture1.getId() + '" /><br>' +
'second:<br><img src="cid:' + picture2.getId() + '" />',
inlineImages: inlineImages
});
}
This is functioning correctly, however it is also listing an attachment on the email - which is particularly frustrating on the android gmail client.
I have tried setting a null attachment option but this does not work. Not that when I use the example from the documentation, but change the URL to any other image, I get an attachment as well as the inlineimage. Using the YouTube logo from the linked documentation, I get only the inlineimage without an attachment.
I cannot understand why - in all instances either from drive or another URL, I am using a PNG file with no other changes.

I have images that are stored in Google Drive as DataURI's and I just sent one like this:
function sendEmailWithInlineImage() {
var file=DriveApp.getFileById('FileId');
GmailApp.sendEmail('recipient email', 'Inline Images', null, {htmlBody:Utilities.formatString('<h3>Inline Images</h3><img src="%s" />',file.getBlob().getDataAsString())});
}
It was received as an inline image with no attachments.
The first part of the file looks like this: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAMR0lEQVR42u2dbWwUxxnHTYLS0ISURrRRlCDxJkWp+qEKUtK3RHzoh5QPqSLh5mMVJVHVRiBZoY5UVcLxva8NBJOCg5EABRtjDtu8GEPqyqYFXNvY4Ffs89l39l1tn8/2nV8AGwPTnfU+x+Px7N6d70z23Bnpr73bt5ud3z7PMzO7N5OWJlJqpfb29uc9bvcfvF7vCZ/PVyWrRogvv89X3ef1Or29vZ/Scksq

Related

Find googleusercontent.com URL for an InlineImage

Whenever I upload an image (i.e. InlineImage) in a Google Doc, it uploads it to a CDN and references a googleusercontent.com URL. If I'm using Google Apps Script on a DocumentApp, I can get an instance of InlineImage. I know I can convert it to a base64 and then create a data URL for this image. However, instead of creating this gigantic URL, I'd rather just use the existing googleusercontent.com URL.
How do I find out the googleusercontent.com URL for an InlineImage?
Essentially you need to do the following:
Set a unique alt description on the InlineImage.
Get the HTML of the entire document.
Use a regex to find the <img tag using the unique alt description from step 1.
function getUrlOfInlineImage(inlineImage) {
var altDescription = inlineImage.getAltDescription(); // warning: assumes that the alt description is a uuid. If it's not unique, this function might return a different image's url. If it's not a UUID, it might contain illegal regex characters and crash.
if (!altDescription) {
inlineImage.setAltDescription(Utilities.getUuid());
// TODO: We currently crash because if we attempt to get the HTML right after calling setAltDescription(), it won't exist in the HTML. We must wait a bit of time before running it again. If there was something like DocumentApp.flush() (similar to how the Spreadsheet App has the same function), it might resolve this issue and we wouldn't need to crash.
throw "Image was missing an alt description. Run again."
}
var html = getGoogleDocumentAsHTML();
var regex = new RegExp('<img alt="' + altDescription + '" src="([^"]+)"');
var matches = regex.exec(html);
if (matches) {
return matches[1];
} else {
return null;
}
}
function getGoogleDocumentAsHTML() {
var id = DocumentApp.getActiveDocument().getId() ;
var forDriveScope = DriveApp.getStorageUsed(); //needed to get Drive Scope requested
var url = "https://docs.google.com/feeds/download/documents/export/Export?id="+id+"&exportFormat=html";
var param = {
method : "get",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions:true,
};
var html = UrlFetchApp.fetch(url,param).getContentText();
return html;
}
There is obviously room for improvement with the script (e.g. not making it crash), but this was good enough for my use-case.

Formatting removed for google docs embed in gmail via app script

I have a scenario where I need to schedule an email at particular time every-day but the content of an email is present in some google doc and update on timely bases. So currently I am manually sending email and paste those content into my email but I want to automate the same.
So by doing search I found, it can be possible via google apps-script and I have written some script as below :
var id = '<my_Id>';
var url = "https://docs.google.com/feeds/download/documents/export/Export?id="+id+"&exportFormat=html";
var param = {
method : "get",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
contentType: "text/html",
muteHttpExceptions:true,
};
var html = UrlFetchApp.fetch(url,param).getContentText();
MailApp.sendEmail(email, 'DSR', 'html only', {htmlBody:html});
Script is working fine and I can see the contents into email but the google doc has some formatting like back-ground, foreground color, table but in email it is shows as only plain text.
Thanks.
This works for me.
https://gist.github.com/erickoledadevrel/11143648
the problem is html has to be inline
Thanks.

MailApp sendEmail htmlBody clickable dynamic link

I wrote a script that creates a new Spreadsheet from one of the Sheets of the original Spreadsheet and emails the new Spreadsheet with the PDF version attached and has an html body with some text and the link to the new Spreadsheet. The script fires each morning because the Sheet has dynamically chaning data, so the link also changes with every trigger.
The problem is that the link in the html body appears as a text and I would like it to be clickable. I tried formatting it with with A HREF but that wouldn't seem to do the trick
var napijelentes = SpreadsheetApp.create(fajlnev);
var napijelentesfajlid = napijelentes.getId();
var napijelentesfajl = DriveApp.getFileById(napijelentesfajlid);
var napijelentesurl = napijelentes.getUrl();
MailApp.sendEmail({
to: ellenorzes,
subject: subject,
htmlBody: "To download the spreadsheet please follow the link below: <br>" + napijelentesurl + "<br><br>",
attachments: [napijelentesfajl]
});
This way the link appears in the email as plain text, with [napijelentesurl] in front of it and is still not clickable.
Any solutions to my problem?
Create a variable for the html string.
var h;
h = 'To download <br><a href = "' + napijelentesurl + '">' +
napijelentesurl + "</a><br><br>";
Use single quotes on the ends of string that need double quotes inside the string.

Google Apps Script - inline images example doesn't work

I'm trying to get this example script working from this Google Developer page.
I put the below code, which is copy/pasted verbatim (except for the "recipient#example.com" of course) into a function which runs when a form is submitted.
// This code fetches the Google and YouTube logos, inlines them in an email
// and sends the email
function inlineImage() {
var googleLogoUrl = "http://www.google.com/intl/en_com/images/srpr/logo3w.png";
var youtubeLogoUrl = "https://developers.google.com/youtube/images/YouTube_logo_standard_white.png";
var googleLogoBlob = UrlFetchApp
.fetch(googleLogoUrl)
.getBlob()
.setName("googleLogoBlob");
var youtubeLogoBlob = UrlFetchApp
.fetch(youtubeLogoUrl)
.getBlob()
.setName("youtubeLogoBlob");
MailApp.sendEmail({
to: "recipient#example.com",
subject: "Logos",
htmlBody: "inline Google Logo<img src='cid:googleLogo'> images! <br>" +
"inline YouTube Logo <img src='cid:youtubeLogo'>",
inlineImages:
{
googleLogo: googleLogoBlob,
youtubeLogo: youtubeLogoBlob
}
});
}
The email is not sent at all and the code doesn't appear to execute.
When I remove the first 4 statements (setting up the URLs and blobs), and the inlineImages section of the sendEmail function, the email is sent.
What am I doing wrong?
I have no problem with your code.

Google Script not sending email as html

I have set up a script to notify users when a new entry has been submitted to a shared spreadsheet. I am trying the following, but the emails are getting sent as plain text:
function formSubmitReply(e) {
var emailAddress = ' *email addresses* ';
var URL = ' *URL to send as link* ';
var message = "<HTML><BODY>"
+ "<P>" + " A new guest complaint has been entered into the database."
+ '<P>To view the spreadhseet, <A HERF="' + URL + '">click here</A>.'
+ "</HTML></BODY>";
MailApp.sendEmail(emailAddress, "New Guest Complaint", "", {htmlBody: message});
}
Any suggestions?
See the sample in the official docs, where it uses the htmlBody parameter: https://developers.google.com/apps-script/reference/mail/mail-app#sendEmail(Object)
You are including html and body tags when the parameter expects the body html not an html page. Remove those tags. Also, note that your tags at the end are reversed (/html then /body)