I've created a script to send emails to a list of e.mail addresses with a PDF attachment. Now I want to "pimp it", adding a picture taken from Drive at the beginning of the email.
I found this method on Google Developers - sendEmail(recipient, subject, body, options) - but it's not crystal clear to me on how it works.
This is the code I wrote so far, but it's not working. I keep reading only the text, without any picture. It should take the first 50 rows of a spreadsheet, send an email to the address in column 9 and update column 11 once done.
var sheet = SpreadsheetApp.setActiveSheet(source.getSheets()[2]);
var row = 3
var subject = "Subject";
var imageId = DriveApp.getFileById("0B-OVYDHfkqhXOTF6aWVSSUtSbUE");
var htmlText = "<img src = "cid:imageId" /> Dear Friend, <BR> <BR> Text";
for (var i = 0; i <= 50; i++) {
var emailAddress = sheet.getRange(row + i, 9).getValue()
var message = "Hi,\n \n" + "text";
MailApp.sendEmail(emailAddress, subject, message, {
name: "Alternative Name",
htmlbody: htmlText,
attachments: [budgetPDF],
inLineImages: imageId
})
sheet.getRange(row + i, 11).setValue("Sent");
Could you please tell me what I'm doing wrong?
Thanks for your help!
You don't get the blob of the file.
var imageId = DriveApp.getFileById("0B-OVYDHfkqhXOTF6aWVSSUtSbUE");
var imageIdBlob = imageId.getBlob();
var htmlText = "<img src = 'cid:imageIdBlob' /> Dear Friend, <BR> <BR> Text";
.....
.....
MailApp.sendEmail(emailAddress, subject, message, {
name: "Alternative Name",
htmlbody: htmlText,
attachments: [budgetPDF],
inLineImages: imageIdBlob
})
As I don't know the format of the file you can also use getAs().
Stéphane
Related
I have been trying to send auto Email through Google App-script. There are two similar scripts but have two different
Email Text
Email Body
Subject
I have also set a trigger to send Auto Email on edit and the script is send the Email when there is "Different" value in Col"E". (Script name is 2nd_Email)
I just want that if "New Request" value is come in Col"E" then 1st_Email script should follow.
I have tried at my end but sometimes 1st_Email scripts works and sometimes 2nd_Email works.
I want both of them to work according to Col"E" values.
Please visit the sheet.
https://docs.google.com/spreadsheets/d/1Eu-c5CPj6XKQSAkSumuprA41-Cx_jvOuUPHr9Zg8KyQ/edit#gid=797418690
Keep the trigger only for the First_email function and add the second code to your first one as an alternative to your if condition.
function First_email() {
var INITIALline = 2;
var columnSEND = 5;
var STATUScolumn = 16;
var textCONDITION = "New Request";
var textCONDITION2 = "Different";
var textSENT = "Mail_Sent"
var tab = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
var interval = tab.getRange(INITIALline,1,tab.getLastRow()-INITIALline+1,STATUScolumn);
var dice = interval.getValues();
var yousent = false;
var email,subject,message;
for (var i=0; i<dice.length; ++i) {
if((dice[i][columnSEND-1]==textCONDITION) && (dice[i][STATUScolumn-1]!=textSENT)){
var email = dice[i][9]
subject = dice[i][6]+" | New Request | "+dice[i][0];
var message = "<font size='3' face='Comfortaa'>Dear Different "+dice[i][6]+",<br/><br/>"+
"Thanks for New Request with us."+dice[i][0]+".<br/><br/>"+
"<i>Thanks & Regards</i><br/>"+
"<b> New Request </b>";
MailApp.sendEmail(email, subject, message,{ htmlBody: message});
tab.getRange(INITIALline+i,STATUScolumn).setValue(textSENT);
yousent = true;
SpreadsheetApp.flush();
}
else if((dice[i][columnSEND-1]==textCONDITION2) && (dice[i][STATUScolumn-1]!=textSENT)){
var email = dice[i][9]
subject = dice[i][6]+" | Different | "+dice[i][0];
var message = "<font size='3' face='Comfortaa'>Dear "+dice[i][6]+",<br/><br/>"+
"Thanks for Different with us."+dice[i][0]+".<br/><br/>"+
"<i>Thanks & Regards</i><br/>"+
"<b> Different </b>";
MailApp.sendEmail(email, subject, message,{ htmlBody: message});
tab.getRange(INITIALline+i,STATUScolumn).setValue(textSENT);
yousent = true;
SpreadsheetApp.flush();
}
}
}
All, I am looking for help from someone with more experience with this. I have a cobbled together email script that works great as is. I want to specify the from: address using an alias and it's my understanding that I need to use GmailApp vs MailApp to accomplish this. The trouble is, I can't figure out how to make my htmlBody work in the GmailApp version.
Here is my working MailApp code:
function SendPreReleaseAlertEmail(row) {
var sheet = SpreadsheetApp.getActive().getSheetByName('EmailSheet');
var subject = sheet.getRange("M3").getValues(); // Change the subject as needed.
var recipients = sheet.getRange("M2").getValue(); // Change the recipient as needed.
var message = "<HTML><BODY>" + "<P>"
for (var x=6;x<14;x++) { //Loop from * to * with a +1 increment
message = message + sheet.getRange("M" + x).getValues() + "<BR>" //Add row I(x) to the message
}
message = message + "</HTML></BODY>";
MailApp.sendEmail(recipients, subject, "", {htmlBody: message});
}
And this is my FAILED attempt at converting it to GmailApp with a From address:
function SendPreReleaseAlertEmail(row) {
var sheet = SpreadsheetApp.getActive().getSheetByName('EmailSheet');
var subject = sheet.getRange("M3").getValues(); // Change the subject as needed.
var recipients = sheet.getRange("M2").getValue(); // Change the recipient as needed.
var message = "<HTML><BODY>" + "<P>"
for (var x=6;x<14;x++) { //Loop from * to * with a +1 increment
message = message + sheet.getRange("M" + x).getValues() + "<BR>" //Add row I(x) to the message
}
message = message + "</HTML></BODY>";
GmailApp.sendEmail(recipients, subject, {htmlBody: message}, {from: "moo#cow.com"});
}
The above code does in fact email from the alias. But the htmlBody just has the words "[object Object]" in the body of the email.
It's Alive! Here is the final code that solved my problem:
GmailApp.sendEmail(recipients, subject, '', {htmlBody: message, from: "moo#cow.com"});
I think you need to fix two things.
1.The syntax is
sendEmail(recipient, subject, body, options)
So you may need to include a blank placeholder for body in your code.
2.You may also need to have both the htmlBody and from in the options JavaScript object like so:
GmailApp.sendEmail(recipients, subject, '' , {
htmlBody: message,
from: "moo#cow.com"});
You can still use MailApp like that:
var email = "exampleRec#email.com";
var Subject_to_Send = "This is an automated email";
var check_body =
"Good morning team, <br/><br/>"
+"I hope this email finds you well. <br/><br/>";
MailApp.sendEmail( {to:email, subject:Subject_to_Send, body:check_body,htmlBody:check_body, from: "exampleSen#email.com"});
If you also want to send a noReply message you can adjust the last line as follows:
MailApp.sendEmail( {to:email, subject:Subject_to_Send, body:check_body,htmlBody:check_body, noReply: true});
I would like to add a permanent CC to the below code. Code below works as intended.
(CC someone#something.com) which I can place in any column in the spreadsheet with the other email addresses it is sending to.
function anothertestofamailmerge() {
var Date = 0;
var NPOName = 2;
var TotalBoxes = 3;
var Email = 4;
var Contact = 5;
var SuggestedVehicle = 8;
var Order = 9;
var emailTemp = HtmlService.createTemplateFromFile("testofmailmerge");
var ws = SpreadsheetApp.getActive().getSheetByName("MailMerge")
var data = ws.getRange("A2:J" + ws.getLastRow()).getValues();
data.forEach(function(row){
emailTemp.Date = row[Date ];
emailTemp.NPOName = row[NPOName];
emailTemp.TotalBoxes = row[TotalBoxes];
emailTemp.Email = row[Email];
emailTemp.Contact = row[Contact];
emailTemp.SuggestedVehicle = row[SuggestedVehicle];
emailTemp.Order = row[Order];
var htmlMessage = emailTemp.evaluate().getContent();
GmailApp.sendEmail(
row[Email],
"Your Click & Collect Order is READY to be collected!",
"You Email",
{name: "No Reply Warehouse", htmlBody: htmlMessage}
);
});
Thank you very much
You want to add cc to GmailApp.sendEmail().
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Pattern 1:
In this pattern, it supposes that the mail address for cc is declared as a variable.
Modified script:
var ccMailAddress = "someone#something.com";
GmailApp.sendEmail(
row[Email],
"Your Click & Collect Order is READY to be collected!",
"You Email",
{name: "No Reply Warehouse", htmlBody: htmlMessage, cc: ccMailAddress}
);
Pattern 2:
In this pattern, it supposes that the mail address for cc is put to the column "K".
Modified script:
From:
var data = ws.getRange("A2:J" + ws.getLastRow()).getValues();
To:
var data = ws.getRange("A2:K" + ws.getLastRow()).getValues();
and
From:
GmailApp.sendEmail(
row[Email],
"Your Click & Collect Order is READY to be collected!",
"You Email",
{name: "No Reply Warehouse", htmlBody: htmlMessage}
);
To:
var ccMailAddress = row[10];;
GmailApp.sendEmail(
row[Email],
"Your Click & Collect Order is READY to be collected!",
"You Email",
{name: "No Reply Warehouse", htmlBody: htmlMessage, cc: ccMailAddress}
);
Reference:
sendEmail(recipient, subject, body, options)
If I misunderstood your question and this was not the direction you want, I apologize.
Added:
As a simple test run, at first, please create new Google Apps Script project. And please copy and paste the following script by setting the email addresses. Then run it.
Simple sample script for using cc:
function myFunction() {
GmailApp.sendEmail(
"### email 1 ###",
"sample subject",
"sample body",
{name: "sample", htmlBody: "sample", cc: "### email 2 ###"}
);
}
Hi im having trouble in sending email with image attachment. because the image file name is randomized and have no way to find out if the body of my message fits the image it will send from my drive. Here is a step by step of the process i have done:
online form integration to google spreadsheet (done)
online form to google drive (done) (images from each row of spreadsheet are saved by folder with folder name contains a unique id that is also present in the spreadsheet cell of each row)
What i would like to do here is get the images of folder in google drive by a.(searching the folder name which contains a ceratin text)
b.(getting the folder contents.)(all are images)
c.(attaching the contents of the folder to the email .)
Example:
function send() {
var picture1 = DriveApp.getFilesByName('snottyboy.jpg');
var picture2 = DriveApp.getFilesByName('daryl.jpg');
var recipientsTO = "fgh#gmail.com" + "," + "sdd#gmail.com"+ "," + "spaz#gmail.com"+ "," + "def#gmail.com"+ "," + "abc#gmail.com";
MailApp.sendEmail({
to:recipientsTO,
subject: "LOOK A LIKE",
body:"Final Message",
attachments: [picture1.next(),picture2.next()]
});
}
Thank you for your help.
See image:
To attach a file, you use File.getBlob() to attach it as a blob. For example:
attachments: [picture1.next().getBlob(),picture2.next().getBlob()]
If you know the exact id of a file (e.g. '0BxDqyd_bUCmvN1E3N0dQOWgycEF'), you can get it as a blob like this:
var picture3Blob = DriveApp.getFileById('0BxDqyd_bUCmvN1E3N0dQOWgycEF').getBlob();
Here's a working example:
function sendPics() {
var picture1 = DriveApp.getFileById('0BxDqyd_bUCmvN1E3N0dQOWgycFE'); //public with link
var picture2 = DriveApp.getFileById('0BxDqyd_bUCmvTFNjRkRXbXA2Tms'); //public with link
MailApp.sendEmail({
to: 'testa#example.com, testb#example.com',
subject: "This is a test",
body:"Test message",
attachments: [picture1.getBlob(), picture2.getBlob()]
});
}
and here's an example of the pictures being added inline instead of as attachments:
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
});
}
Im trying to setup a form that will email me peoples responses.
This is the current script:
function nl2br_(input_string){
return input_string.replace(/(\r\n|\r|\n)/g,'<br />');
}
function contactUsMailer(e) {
// This script e-mails the contents of a form to a given recipient
// The form must have three fields in the order of: name; e-mail address; and message
// You must change the recipient variable below to your e-mail address
try {
var recipient = 'myemail#hotmail.com';
var timestamp = e.values[0];
var username = e.values[1];
var option = e.values[2];
var details = e.values[3];
var body = username+' sent the following message: '+option ;
var bodyHTML = '\
<p>'+username+' '+option+' below are the details </p>\
<blockquote>'+nl2br_(details)+'</blockquote>\
<p>Sent by the Steegle.com Contact Us Form Google Apps Script</p>';
var advancedArgs = {htmlBody:bodyHTML , replyTo:username};
MailApp.sendEmail(recipient, "Contact Us Form", body, advancedArgs);
} catch(e){
MailApp.sendEmail(recipient, "Error - Contact Us Form", e.message);
}
}
This is how it looks when I get an email:
var timestamp - not important ignore it
var username - their username
var option - this is the "issue"
var details - detail of problem
I would really like it to look something like this:
Username: "username here"
Issue: "issue here"
Details:
"wall of text for details here"
I've been at this for over an hour and a half and I can't get it to look anything remotely like what what is shown above :/
I had a similar situation and this is how I solved it.
I created a HTML Output with a table inside and then Sent this as My Email Body.
var log = HtmlService.createHtmlOutput()
log.append('<body><table border="1">');
log.append('<tr></td> Username: </td><td>' + usernameVAR +'</td></tr>');
log.append('<tr></td> ISSUE </td><td>' + issueVAR +'</td></tr>');
log.append('<tr></td> DETAILS </td><td>' + detailsVAR +'</td></tr>');
log.append('</table></body>');
var htmlContent = log.getContent();
MailApp.sendEmail(YOUR EMAIL GOES HERE ,"TITLE OF EMAIL", "SUBJECT LINE",{htmlBody:htmlContent} ) //Text as HTML
Good Luck!