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.
Related
Referring to this thread I'm able to retrieve my signature from Gmail with the code var signature = Gmail.Users.Settings.SendAs.list("me").sendAs.filter(function(account){if(account.isDefault){return true}})[0].signature;
But if I attach the retrieved HTML to the email body, the recipient see the raw code instead of my signature.
I red many threads with this problem, but I'm unable to understand how fix this, I'm not expert with HTML.
Please could someone help me?
GmailApp allows you to parse html using htmlBody:
var signature = Gmail.Users.Settings.SendAs.list("me").sendAs.filter(function(account){if(account.isDefault){return true}})[0].signature;
GmailApp.sendEmail("email#gmaiil.com", "signature test", "test", {htmlBody: signature})
Update:
The signature of GmailApp.sendEmail is:
sendEmail(recipient, subject, body, options)
or:
sendEmail(recipient, subject, body)
In your case, you need to use option 1.
replyTo is specified using the options object.
So, to summarize, here is your code:
mia_firma = '<p>' + corpo + '</p>' + mia_firma; // concat htmlBody
MailApp.sendEmail(dest, // recipient email - string
sogg, // should be subject - string
corpo, // mail body - WILL BE IGNORED if you use htmlBody
{ // options - object
htmlBody: mia_firma,
replyTo: "reply_to#XYZ.page"
}
);
You can see the full documentation here
With Google Apps Script, how to make a line break in a variable to send mail?
If you're not sending an HTML formatted message, use "\n". I personally despise HTML formatted e-mail.
Newline in msgBox:
Browser.msgBox('line 1 \\n line 2');
Please note you need to escape '\n' with additional backslash.
You should use the <br> tag when sending the HTML portion of the email .
Below is a sample on how I compose the same email body, but formatted differently for HTML & plain text. (Not the best code but hopefully it illustrates the point)
function onFormSubmit(e) {
var subject = "Subject";
// Collect user data
var name = e.values[0];
var email = e.values[1]; // Where user enters his/her email address
// Generate content - Replace this with what you're composing
var content = [];
content.push("Hi " + name);
content.push("Thanks for submitting the survey!___LINE_BREAK___");
content.push("Survey Team");
// Combine content into a single string
var preFormatContent = content.join('___LINE_BREAK___');
// Replace text with \n for plain text
var plainTextContent = preFormatContent.replace('___LINE_BREAK___', '\n');
// Replace text with <br /> for HTML
var htmlContent = preFormatContent.replace('___LINE_BREAK___', '<br />');
MailApp.sendEmail(email ,
subject,
plainTextContent ,
{
name: "Survey Team",
html: htmlContent
});
}
I usually use table in my mail but I think <br /> should work
You can use \r to change line for the GmailApp
\\n to change line for the msgBox.
For HTML, <br> should work.
For javascript/appscript in other google apps like sheetApp,use template literal as MailApp does not respond \n. (i.e) Type the content between two backticks and use 'enter' for new line as follows. No need of using single or double quotes
const recipient = 'abc#xyz.com,cde#xyz.com'
const sub = 'some subject'
const line1 = 'some line1 content'// say 'Name'
const line2 = 'some line2 content'// say 'Contact Number'
const line3 = 'some line3 content'//say 'EMail Id'
const body = `Name: ${line1}
Contact Number: ${line2}
EMail Id: ${line3}`
MailApp.sendEmail(recipient,sub,body)
// There was a mistake with the original answer; I've added "$" for making the code above work.
I try all the answers but they don't work on my computer. I found it better to use GmailApp.sendEmail() instead of MailApp.sendEmail(). It is almost the same, but GmailApp.sendEmail() can change line using \n, while MailApp.sendEmail() can't.
var message = "";
message += "First line of email." + "\n";
message += "Second line of email." + "\n";
message += "Third line of email." + "\n";
message += "Fourth line of email." + "\n";
GmailApp.sendEmail(Session.getActiveUser().getEmail(),"Look at this Title, it work!",message)
This code will send an email to whoever runs this script. It works fine for me.
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 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)
With Google Apps Script, how to make a line break in a variable to send mail?
If you're not sending an HTML formatted message, use "\n". I personally despise HTML formatted e-mail.
Newline in msgBox:
Browser.msgBox('line 1 \\n line 2');
Please note you need to escape '\n' with additional backslash.
You should use the <br> tag when sending the HTML portion of the email .
Below is a sample on how I compose the same email body, but formatted differently for HTML & plain text. (Not the best code but hopefully it illustrates the point)
function onFormSubmit(e) {
var subject = "Subject";
// Collect user data
var name = e.values[0];
var email = e.values[1]; // Where user enters his/her email address
// Generate content - Replace this with what you're composing
var content = [];
content.push("Hi " + name);
content.push("Thanks for submitting the survey!___LINE_BREAK___");
content.push("Survey Team");
// Combine content into a single string
var preFormatContent = content.join('___LINE_BREAK___');
// Replace text with \n for plain text
var plainTextContent = preFormatContent.replace('___LINE_BREAK___', '\n');
// Replace text with <br /> for HTML
var htmlContent = preFormatContent.replace('___LINE_BREAK___', '<br />');
MailApp.sendEmail(email ,
subject,
plainTextContent ,
{
name: "Survey Team",
html: htmlContent
});
}
I usually use table in my mail but I think <br /> should work
You can use \r to change line for the GmailApp
\\n to change line for the msgBox.
For HTML, <br> should work.
For javascript/appscript in other google apps like sheetApp,use template literal as MailApp does not respond \n. (i.e) Type the content between two backticks and use 'enter' for new line as follows. No need of using single or double quotes
const recipient = 'abc#xyz.com,cde#xyz.com'
const sub = 'some subject'
const line1 = 'some line1 content'// say 'Name'
const line2 = 'some line2 content'// say 'Contact Number'
const line3 = 'some line3 content'//say 'EMail Id'
const body = `Name: ${line1}
Contact Number: ${line2}
EMail Id: ${line3}`
MailApp.sendEmail(recipient,sub,body)
// There was a mistake with the original answer; I've added "$" for making the code above work.
I try all the answers but they don't work on my computer. I found it better to use GmailApp.sendEmail() instead of MailApp.sendEmail(). It is almost the same, but GmailApp.sendEmail() can change line using \n, while MailApp.sendEmail() can't.
var message = "";
message += "First line of email." + "\n";
message += "Second line of email." + "\n";
message += "Third line of email." + "\n";
message += "Fourth line of email." + "\n";
GmailApp.sendEmail(Session.getActiveUser().getEmail(),"Look at this Title, it work!",message)
This code will send an email to whoever runs this script. It works fine for me.