0
votes

Writing google script for my Google Spreadsheet.

How can I create a new mail message, populate with some fields (e.g. "To:") and allow the end user to complete the message and click "send"?

I could only find "MailApp.sendEmail" to send an email, but not create a new message without sending...

To clarify: this is not to create a "Draft" message. The script will basically automate the following manual process in Gmail: 1. Click "Compose". 2. Enter "To:"

...and that's it. The script ends and the end user is left to complete the subject, body, and click "Send".

This is the function I wrote and just missing the command to create this email message:

function contactsSendEmail() {
  var sh = SpreadsheetApp.getActiveSpreadsheet();
  var ss = sh.getActiveSheet(); 

  if (isActiveCellInRange(ss.getRange('Contacts_Table_Anchor').getRow()+1,ss.getRange('Contacts_Table_Anchor').getColumn(),ss.getLastRow(),ss.getLastColumn())) {
    var sh = SpreadsheetApp.getActiveSpreadsheet();
    var ss = sh.getActiveSheet();
    var contactName = ss.getRange(ss.getActiveCell().getRow(),getColumnRowByName('Contacts', 'Name', ss.getRange('Contacts_Table_Anchor').getRow())).getValue();
    var contactEmail = ss.getRange(ss.getActiveCell().getRow(),getColumnRowByName('Contacts', 'Email', ss.getRange('Contacts_Table_Anchor').getRow())).getValue();

    sh.getRangeByName('Email_Sent_To').setValue(contactEmail);

    //Here should come what I'm missing, something like: MailApp.createEmail(contactEmail);
  }
}
1
You mean make a draft?Kriggs
No, not make a draft (I found the above item already). I want to automate the following manual process: End user clicks "Compose", a new message windows opens, "To:" is populated. That's the end of the script. The user is left to write the Subject and body, and click "send".Mor Sagmon
@Mogsdad: please unmark this as a duplicate. See my clarification and note. Thanks!Mor Sagmon
Ok, not a duplicate - but too broad. You're going to have to show what you've done, and where your specific problem is, because this isn't just a free code shop. Let me know when you've done that - I'll leave it closed in the mean time.Mogsdad
@Mogsdad I added my current function code. Just missing the actual creation of the half-baked email.Mor Sagmon

1 Answers

0
votes

You can use the Gmail API to create a draft in Gmail. See code.google.com for a sample snippet.

function createDraftHTMLEmail() {

  var subject = "testing createDraftHTMLEmail";
  var forScope = GmailApp.getInboxUnreadCount(); // needed for auth scope
  var htmlBody = '<html><body>' + '<h1>World</h1>' + '</body></html>';

  var message = 'From: Me <' + myEmailAddress + '>\r\n' +
    'To: Me <' + myEmailAddress + '>\r\n' +
    'Subject: ' + subject + '\r\n' +
    'Content-Type: text/html; charset=utf-8\r\n' +
    'Content-Transfer-Encoding: quoted-printable\r\n\r\n' +
    htmlBody;

  var draftBody = Utilities.base64Encode(message);
  draftBody = draftBody.replace(/\//g, '_').replace(/\+/g, '-');

  var params = {
    method: "post",
    contentType: "application/json",
    headers: {
      "Authorization": "Bearer " + ScriptApp.getOAuthToken()
    },
    muteHttpExceptions: true,
    payload: JSON.stringify({
      "message": {
        "raw": draftBody
      }
    })
  };

  var resp = UrlFetchApp.fetch("https://www.googleapis.com/gmail/v1/users/me/drafts", params);
  Logger.log(resp.getContentText());
}