|
| 1 | +We sometimes need to send email in mass for things like opstaff hiring. This is how we do it. |
| 2 | + |
| 3 | +## Getting emails from ldap |
| 4 | +You need admin privilege to complete this step: |
| 5 | + |
| 6 | +`kinit []/admin && ldapsearch "(creationTime>=20240815000000-00)" mail | grep mail >> emails.txt` |
| 7 | + |
| 8 | +Here I set creationTime filter to the start of the Fall 2024 semester. You can change it to whenever that suits your needs. |
| 9 | +The idea here is that we are filtering for freshman and sophomore. |
| 10 | + |
| 11 | +## Importing emails to Google Sheets |
| 12 | +There are many ways to export the .txt file into CSV. I (vibe) wrote a python script to do that. |
| 13 | +Once you have the emails in CSV, import it to a Google Sheet under OCF google drive. |
| 14 | + |
| 15 | +## Sending emails |
| 16 | +You can either use ocflib.misc.mail.send_mail or Gapps script. I used Gapps. |
| 17 | +I stole and modified the script from the DecalVM mailing list. You should copy paste the following code into Your Sheet -> Extensions -> App Script: |
| 18 | +``` |
| 19 | +function sendHiringEmails() { |
| 20 | + // --- Please update this --- |
| 21 | + var sheetName = "Sheet1"; |
| 22 | + // ------------------------- |
| 23 | +
|
| 24 | + var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); |
| 25 | + if (!sheet) { |
| 26 | + Logger.log("Error: Sheet not found. Please check the 'sheetName' variable."); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + var data = sheet.getDataRange().getValues(); |
| 31 | + |
| 32 | + |
| 33 | + // --- Email Content --- |
| 34 | + var subject = "Waddles says hi!"; |
| 35 | + var message = ` |
| 36 | + Waddles is the best! |
| 37 | + `; |
| 38 | + // --------------------- |
| 39 | + total = 0 |
| 40 | + for (var i = 0; i < data.length; i++) { |
| 41 | + var email = data[i][0]; // Get email from the first (and only) column |
| 42 | +
|
| 43 | + // Basic validation to skip empty rows or invalid-looking emails |
| 44 | + if (email && email.includes("@")) { |
| 45 | + try { |
| 46 | + GmailApp.sendEmail(email, subject, "", { |
| 47 | + from: alias, |
| 48 | + htmlBody: message |
| 49 | + }); |
| 50 | + Logger.log("Email sent to: " + email); |
| 51 | + total += 1 |
| 52 | + } catch (e) { |
| 53 | + Logger.log("Error sending email to: " + email + " - Error: " + e.message); |
| 54 | + } |
| 55 | + } else { |
| 56 | + Logger.log("Skipping row " + (i + 1) + ": Invalid or empty email."); |
| 57 | + } |
| 58 | + } |
| 59 | + Logger.log("Email sent to " + total + " students.") |
| 60 | +} |
| 61 | +``` |
| 62 | +Of course, change the message, alias, sheetname etc. |
| 63 | +Make sure that when you run it you have the permission to send as the alias address. |
0 commit comments