I have quite a long email template I want to automatically send to welcome new users.
The script copies the body of a Google Doc and uses that as the body of the email using MailApp.sendEmail.
The issue is that the email that arrives is very narrow and doesn't copy exactly what is in the template. Is there any way of formatting this to make it the same as the Google Doc template?
Any help much appreciated
var doc = DocumentApp.openByUrl("https://docs.google.com/document/d/"doc id"/edit");
var body = doc.getBody().getText();
var message = body;
var subject = "subject line";
MailApp.sendEmail (user.primaryEmail, subject, message)
I learned something new with this:
When you send an email as a plain text email it won't let you control where the line breaks are
It actually adds line breaks when you call the .getText() method, as well as part of the .sendEmail method when you are sending it just as plain text.
The easiest solution is to send it as an HTML message. I experimented with this and have what I believe the easiest solution for it below:
//this will replace the line breaks with html line breaks
var htmlBody = doc.getBody().getText().replace(/\n/g,'<br/>');
var message =
{
to: user.primaryEmail,
subject: 'subject line',
htmlBody: htmlBody
}
MailApp.sendEmail ({message})
I've tested this and this should fix your problem.
Related
experts!
I'm faced with a question that has no answer, so I'm reaching out for help. Thanks in advance to anybody that can make sense of this.
I made a script (triggered weekly) that converts a Google sheet into a pdf, then attaches it to an email and sends it off. It's been working fine for months, now suddenly won't save or run. I'm getting the error "Invalid argument: fileName (line 80, file "Code")" (The "GmailApp.sendEmail" line is line 80.) As expected, the web didn't shed much light on why this is happening, or how to fix it.
Any ideas to throw my way?
Here is the relevant code. (GAS is flagging the "GmailApp.sendEmail" line as an error.)
// Sets the recipient(s) of the email message
var emailAddress1 = srcSheet1.getRange('J299').getValue();
// Sets the subject of the email message
var emailSubject1 = srcSheet1.getRange('C12').getValue();
// Sets the 'body message' of the email message
var emailBodyMessage1 = srcSheet1.getRange('C54').getValue();
// This section converts the new sheet into a pdf blob
// Make the pdf
var blob1 = DriveApp.getFileById(srcSpreadsheet1.getId()).getAs("application/pdf");
// Set the title of the pdf
var PDFsubject1 = srcSheet1.getRange('C54').getValue() + ".pdf";
blob1.setName(PDFsubject1);
// This section assembles and sends the email
GmailApp.sendEmail(emailAddress1,emailSubject1,emailBodyMessage1, {
htmlBody: emailBodyMessage1,
attachments:[blob1]
});
var emailBodyMessage1 = srcSheet1.getRange('C54').getValue();
var PDFsubject1 = srcSheet1.getRange('C54').getValue() + ".pdf";
There is something wrong with the cell 'C54'. You're getting a body of the message and a file name from this cell. And at some point the content of the cells was changed into something that can't be a file name. Perhaps the cell now contains line breaks or tab symbols and the like.
This is an example that works perfectly
function myFunction() {
var myEmail = 'xxxxxx#gmail.com'
var blob1 = DriveApp.getFileById('1IsP2bETUE-d2RZakxbkuASSCZYdSaUCk3uAFstFdIWM').getAs("application/pdf")
blob1.setName('test pdf file');
GmailApp.sendEmail(myEmail, 'test pdf', 'test', {
htmlBody: '<b>test</b>',
attachments: [blob1]
});
}
I recommand to check any individual term in your script :
Logger.log(srcSpreadsheet1.getId())
Logger.log(emailAddress1)
Logger.log(emailSubject1)
Logger.log(emailBodyMessage1)
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 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.
I am using google scripts to send text messages. I would like to use the newline character to format messages properly. I would expect the code below to display:
Hello.
How are you?
Instead, the following is displayed:
Hello.How are you?
var response = "Hello.\nHow are you?"
MailApp.sendEmail(recipient, subject, response);
Please note recipient is in the format:
"(956) xxx-xxxx" <1740xxxxxxx.1956xxxxxxx.dhJVMRb1ZG#txt.voice.google.com>
That is, this is being sent as sms via gmail.
UPDATE, I tried:
function myFunction()
{
GmailApp.sendEmail(
'(956) xxx-xxxx <1262xxxxxxx.1956xxxxxxx.7ljPdnVXKg#txt.voice.google.com>',
'test mailApp', 'test3', { htmlBody: "break3<br />line"});
}
and the result was:
test3
which tells me that any email sent to txt.voice.google.com is processed as text only, not html. I hope there's some workaround.
jimpudar's answer wont work!
The text of email will be "Hello.How are you?"
Instead use this:
GmailApp.sendEmail("me#gmail.com", "Email", "This won't be displayed", { htmlBody:
"Hello</br>How are you?"
}
);
Or you can do:
var recipient = "example#gmail.com";
var subject = "This email";
var body = "Hello.</br>How are you?";
GmailApp.sendEmail(recipient, subject, "This won't be displayed",{htmlBody:
body
}
);
Both will work ;)
As you maybe noticed, you can use any html code inside the {htmlBody: whatever}
Good luck, and have fun with scripting :)
I am not 100% sure on this, but I seem to remember using HTML tags to accomplish this.
var response = "Hello.<br>How are you?";
This probably won't work if your email is being sent as text. It worked for me when sending the text as an HTML email.
I am trying to send a Google Document (nothing fancy here, just a simple, script-built text document) as a plain text email attachment with Google Apps Script. I can do this manually by going into my Drive and selecting "File>Email As Attachment...". From there, a window pops up asking for the recipients, file type (Where I can choose plain text), subject, message, etc. How can I do this through script?
I have another document using the following for PDFs (Where TimeDataId and email_address are properly defined):
//Get Data as .pdf file
var TimeData = DocsList.getFileById(TimeDataId).getAs('application/pdf');
// Attach pdf and send the email to customer
var subject = 'Time Data';
var body = 'Please see the attached Data.' + '<br/><br/>Thank you,';
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: TimeData});
And this works flawlessly for PDFs. However, I am really looking for something like this:
//Get Data as .txt file
var TimeData = DocsList.getFileById(TimeDataId).getAs(MimeType.PLAIN_TEXT);
// Attach txt and send the email to customer
var subject = 'Time Data';
var body = 'Please see the attached Data.' + '<br/><br/>Thank you,';
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: TimeData});
All I ever get with this method is a failure notice with the report of "Authorization is required to perform that action".
I know I could just paste the plain text information into the body of my email, but I would really like to have a downloadable file, as this saves a step for the user (this file will be used later for data import into another program). Has this question already been asked and answered? Does anyone have any ideas? Thanks for any input.
Here is how I would get text from a Document as a plain text file.
var txt = DocumentApp.openById(TimeDataId).getText();
var TimeData = Utilities.newBlob(txt, 'text/plain','myattachment.txt')
It takes an extra level of indirection but for now getAs is only supported for PDFs.
I believe you are getting that error message as the MimeType class for the new DriveApp but I haven't verified that.