NotSooFriendly94

Random Doggo.

Jan 12th, 2024
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.08 KB | Source Code | 0 0
  1. var ss = SpreadsheetApp.openById('1pjunTjJkdi4IzbZM4IHEn8CiNoBohYRPNRARlVNSSxY');
  2. var sheet = ss.getSheetByName('Send');
  3. var log = Logger.log;
  4. var sleep = Utilities.sleep;
  5. var range = sheet.getRange;
  6.  
  7.  
  8. //Set to a on Change Trigger, get the value of the ceckbox, if true it initiates SendImageEmail(). (origionally had 5 checkboxes, couldnt be asked to debug LOL)
  9. function checkBoxes() {
  10.   var boxRange = range('C2').getValue();
  11.   var b = boxRange;
  12.  
  13.   if (b === true) {
  14.     range('C2').setValue(false);
  15.     SpreadsheetApp.flush();
  16.     sendImageEmail();
  17.   }
  18. }
  19. //This runs the URL sent Via WhatsApp, uses a regexepression to take the URL from the JSON contents... This is a 'Helper'.
  20. function getImageURL() {
  21.   var link = 'https://random.dog/woof.json';
  22.   var jsonURL = UrlFetchApp.fetch(link).getContentText();
  23.   var regEx = /"url":"([^"]*)"/;
  24.   var match = jsonURL.match(regEx);
  25.  
  26.   // Check if there is a match and get the URL (if matched)
  27.   var finalLink = match ? match[1] : null;
  28.   return finalLink;  // Return the URL
  29. }
  30. //Initiated from the Checkbox trigger, then uses the helper to grab the URL, inserts this into te email (with of course custom subject and body) Then annoys the f*** out of Carey.
  31. function sendImageEmail() {
  32.   var imageUrl = getImageURL();
  33.  
  34.   if (imageUrl) {
  35.     var imageBlob = UrlFetchApp.fetch(imageUrl).getBlob();
  36.     var recipient = '[email protected]';
  37.     var subject = 'I always Win :)';
  38.     var body = 'Hi Carey, <br/><br/> As Requested, Please find the image of a random dog attached below. <br/><br/> Your Sincerely. The Annoying English Kid you wish you never met!<br/><br/>';
  39.     body += '<div style="width: 100%;">';
  40.     body += '<img src="cid:image" style="width: 100%;" />';
  41.     body += '</div>';
  42.  
  43.     // Send email with image attachment
  44.     MailApp.sendEmail({
  45.       to: recipient,
  46.       subject: subject,
  47.       body: body,
  48.       htmlBody: body,
  49.       inlineImages: { 'image': imageBlob }
  50.     });
  51.  
  52.     Logger.log('Email sent with image attachment.');
  53.   } else {
  54.     Logger.log('Unable to fetch image URL.');
  55.   }
  56. //You're Welcome...
  57.  
  58.  
Advertisement
Add Comment
Please, Sign In to add comment