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.
Related
I have a Google Form which I would like to automatically email someone when a new response is submitted. So far, I have just a simple HTML page with text in the body, however I would like the email content to include the form data as well.
Currently, this is what I have written:
function sendEmail(e) {
//response
var html = HtmlService.createTemplateFromFile("email.html");
var htmlText = html.evaluate().getContent();
var emailTo = "jeffreyabr#gmail.com"
var subject = "New SAP Role Request"
var textBody = "This email requires HTML support. Please make sure you open it with an email client that supports HTML"
var options = {htmlBody: htmlText};
GmailApp.sendEmail(emailTo, subject, textBody, options);
This came from following this basic YouTube tutorial.
Is there more Google Apps Script that I can add to accomplish this? Can I do this from Forms or must I do it from within Sheets?
The e.response object also contains the form data, which can be accessed by using e.response.getItemResponses().
Then to get the question, use getItem().getTitle(). To get the answer, use getResponse().
If you do not need the HTML response, then you can append the questions and answers to the textBody to display them on the email. Otherwise, you would have to add a script in your email.html using HTML scripts or google.script.run.
References:
Event Objects | onFormSubmit(e)
Class FormResponse
I need to use photos from Google Photos into HTML templates.
I readed some answers here. In all answers are talking about use the URL of the image, but the principal issue for me is to take the URL of the image from Google Photos.
This is my code:
function sendToGroup() {
var htmlBody = HtmlService.createHtmlOutputFromFile('emailBody').getContent();
var mSubject="Lorem ipsum";
var mName='Me | me.com';
GmailApp.sendEmail(
'one-group#me.com',
mSubject,
'Lorem ipsum',
{
htmlBody: htmlBody,
name: mName
}
);
Logger.log('EMail OK');
}
Actually, into my HTML template i'm using images from Flickr. I can take easily the URL of these images and use it into the HTML without problem, for example, this works fine:
<div class="fluid-img" style="font-size:0pt; line-height:0pt; text-align:left"><img src="https://live.staticflickr.com/7916/45689248745_7268fc026d_z.jpg" border="0" width="650" alt="" /></div>
Recently i created one account into Google Workspace and i want to migrate all my photos from Flickr to Google Photos.
There is one way to use Google Photos for send HTML emails? I'm talking about send the images as part of the content of the email, not as attached files.
I believe your goal as follows.
You want to retrieve the image from Google Photos and want to send an email including the image from Google Photos.
You want to achieve this using Google Apps Script.
Modification points:
In your script, unfortunately, the image is not retrieved from Google Photos.
In your case, I thought that it is required to retrieve the image from Google Photos.
For this, it is required to link Cloud Platform Project to Google Apps Script Project. About this, you can see this thread. In this sample script, the library is not used. So it is not required to install the library.
When you want to use the image of Google Photos, you can use baseUrl of items. But, this is the temporal URL. So in this case, I would like to propose the 2 patterns.
In this script, the following scopes are used.
https://www.googleapis.com/auth/photoslibrary
https://www.googleapis.com/auth/script.external_request
https://www.googleapis.com/auth/script.send_mail
Sample script:
Before you use this script, please link Cloud Platform Project to Google Apps Script Project and enable Photos Library API at API console, and also, please set the scopes to the manifest file (appsscript.json).
function myFunction() {
const searchFilename = "###"; // Please set the filename for searching the image from Google Photos.
const headers = {"Authorization": "Bearer " + ScriptApp.getOAuthToken()};
let pageToken = "";
const mediaItems = [];
do {
const url = "https://photoslibrary.googleapis.com/v1/mediaItems?pageSize=100&pageToken=" + pageToken;
const res = UrlFetchApp.fetch(url, {headers: headers});
const obj = JSON.parse(res.getContentText());
Array.prototype.push.apply(mediaItems, obj.mediaItems);
pageToken = obj.nextPageToken || "";
} while (pageToken);
const images = mediaItems.filter(({filename}) => filename == searchFilename);
if (images.length > 0) {
MailApp.sendEmail({
to: "###email address ###",
subject: "sample mail",
htmlBody: `<div class="fluid-img" style="font-size:0pt; line-height:0pt; text-align:left"><img src="${images[0].baseUrl}" border="0" width="650" alt="" /></div>`,
});
}
}
The image of Google Photos is retrieved using the method of "mediaItems.list". For searching the image, as a sample, the filename is used. Because in your question, I couldn't find the method for searching the image from Google Photos. So please modify this for your actual situation.
Above script uses the URL of baseUrl. But, this URL is a temporal URL. So as other pattern, I would like to propose to use the image blob as follows. When you use this, please modify MailApp.sendEmail() as follows.
MailApp.sendEmail({
to: "###email address ###",
subject: "sample mail",
htmlBody: "<img src='cid:sample'>",
inlineImages: {sample: UrlFetchApp.fetch(images[0].baseUrl).getBlob()}
});
References:
Method: mediaItems.list
Related thread
Google Apps Scripts: import (upload) media from Google Drive to Google Photos?
Google photos api adding photos not working, upload seems to work
Is it possible to load google photos metadata into google sheets?
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
I am trying to create a document and set its access permissions.
The code below works on my normal Gmail account.
However, when I run it on my unique GSuite address, it returns this error:
TypeError: Cannot find function setSharing in object Document. (line
6, file "Code")
Here is the code:
function createAndSendDocument() {
//Create a new Google Doc named 'Hello, world!'
var doc = DocumentApp.create('Hello World');
//Set user permissions to view and edit.
doc.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.EDIT);
//Access the body of the document, then add a Paragraph.
doc.getBody().appendParagraph('This is a test document for people to see and try and lorem ipsum.');
//Get the URL of the doc
var url = doc.getUrl();
//Get email address of the active user - aka you.
var email = 'example#domain.com';
//Get the name of doc to use as an email subject line
var subject = doc.getName();
//Add a new strong to the url variable to use as an email body.
var body = 'Link to your document: ' + url;
//Send yourself an email with a link to the document.
GmailApp.sendEmail(email, subject, body);
}
How can I work around this or use the setSharing method correctly? Am I using the wrong method or am I missing other steps as
I had this problem from a non GSuite account and solved it by using the Drive api instead of the Document one.
e.g.
var drivedoc = DriveApp.getFileById(documentId);
drivedoc.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.EDIT);
worked but it did not when I used DocumentApp.getFileById.
I had a similar problem myself that might help you. I used the DocumentApp api to create a document. Then I used the DriveApp api to set the sharing. Notice the the URLs you get from the same document are different. If you email the URL you get for the document from the DocumentApp with someone outside your G Suite organization, they will have to sign in to open the document even though the sharing is set to "Anyone with the link. No sign-in required." But if you email the URL that you get from DriveApp to the person outside your G Suite organization, they will be able to open the link easily, with no sign-in required. So I think your code might work better if you used DriveApp to set the sharing of the document you create. Then make sure that you email the URL you get from the DriveApp api instead of the URL you get from the DocumentApp api.
function myFunction() {
var doc = DocumentApp.create("Test Document");
Logger.log("This is the URL that I get with DocumentApp: " + doc.getUrl());
var documentFileID = doc.getId();
var myFileDriveApp = DriveApp.getFileById(documentFileID);
Logger.log("This is the drive app URL: " + myFileDriveApp.getUrl());
//Set the sharing on the file using DriveApp
myFileDriveApp.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
}
I want to email a form to customers to fill out in the browser or ideally in rheir email client . I have created a form opened it by id , then sent it using
function openDialog() {
// Open a form by ID.
var form = FormApp.openById('kdhgsdfskfhs');
var url = form.getPublishedUrl();
var response = UrlFetchApp.fetch(url);
var htmlBody = HtmlService.createHtmlOutput(response).getContent();
MailApp.sendEmail({
to: 'me#gmail.com',
subject: 'hi',
htmlBody: htmlBody
});
}
You can see the result which I have put in screenshot . I'm starting to think that this is not doable in apps script. Is it possible ? if not what is the best way to send allow a user to enter info into a spreadsheet?
It's possible email html. But I think your going to need a website or a webapp or a link to the online form to submit it to a spreadsheet.