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)
Related
I have a Google apps script which sends an email with a long URL and a Bitly shortened one. However the GmailApp.sendEmail function silently fails if I include a Bitly URL. Simple example of problem code:
function myFunction() {
var sMail = "(my email address)";
var longURL = "https://www.google.com";
var shortURL = "(bitly URL)";
// Send email with information
GmailApp.sendEmail(
sMail,
"URL Send Test",
"Original URL: " + longURL + "\n" +
"Short URL: " + shortURL
);
}
This code executes without error, but I never receive the email or any notification that it was blocked. If I remove shortURL from the body then email is received.
Obviously these shortened links are a source of abuse (in fact I can't include it in my post) but is there a way around this? My script generates a long custom URL and shortening it is an important function.
Thanks for any suggestions.
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 have an apps script to automate some emails and want to send a body from a template. I figured I could store the template as a google doc, fetch it, and send it in the script. I have something like this:
var doc = DocumentApp.openById("DocumentID");
MailApp.sendEmail("toEmail",
"fromEmail",
"TPS report status",
doc.getBody().getText());
This does work, except that the email body has new lines inserted in seemingly random areas, although it does retain the new paragraphs that were part of the original document. It's not as obvious in the image, but the red circles are where there are line breaks for something that should be one line. It's very obvious when viewed via gmail app.
You need to actually format body text in html format and then can user mailapp with 'htmlBody' parameter to pass the body.
You need to get body's paragraphs and add a for loop and add tag in the beginning of each para.
function getBody()
{
try{
var para=tempDoc.getBody().getParagraphs();
var body=''
for(var y=0;y<para.length;y++)
{
body+="<br>"+para[y].getText();
}
return body;
}
catch(ex)
{
Logger.log(ex)
}
}
To add to the above answer, your html body would look something like this:
body = "Good day, \n\nThe following course has been loaded for deployment:" + "\nCourse Name: " + courseName + "\nCourse Type ID: " + courseID + "\nContent Version: " + courseVersion +
"\nCourse Language: " + courseLanguage + "\n\nCourse Filename: " + title + "\nCourse File Location: " + fileLocation + "\nCourse Filesize: " + fileSize +
"\nDeployment Required By: " + deploymentDate + "\nCourse Live Date: " + courseLiveDate + "\n\n Kind regards\n Department Name";
MailApp.sendEmail(recipient, subject, body, {cc: carbonCopy, noReply: true});
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.
i am trying to send mails to some list of user using google apps-script.
I am taking the first draft mail and then mailing it to list of users present in the spreadsheet. But when i use ".getplainbody(); " function. It only copies the plain text in the draft.
function sendmail()
{
var drafts = GmailApp.getDraftMessages();
Logger.log(drafts.length);
var draft = drafts[0].getPlainBody();
Logger.log(draft);
GmailApp.sendEmail('abc#gmail.com', 'subject', 'Hello' + '\n ' + draft);
}
I have also tried using the getbody() and then html with the message.
function sendmail()
{
var drafts = GmailApp.getDraftMessages();
Logger.log(drafts.length);
var draft = drafts[0].getBody();
Logger.log(draft);
GmailApp.sendEmail('abc#gmail.com', 'subject', 'Hello' + '\n ' + {html: draft});
}
But this also gives me "[object Object]" in the inbox.
Is there any other option to send the draft mail (not in plain text format)with proper format.
Thanks
Try: GmailApp.sendEmail('abc#gmail.com', 'subject', 'plaintext body', {htmlBody: draft});
You are missing the fourth parameter, 'Hello' + '\n ' + {html: draft} actually concatenates Hello \n with the {html: draft} object.