0

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);
  }
}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Mor Sagmon
  • 905
  • 1
  • 16
  • 35
  • You mean make a draft? – Kriggs Oct 20 '15 at 17:38
  • 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 Oct 21 '15 at 13:59
  • @Mogsdad: please unmark this as a duplicate. See my clarification and note. Thanks! – Mor Sagmon Oct 22 '15 at 08:12
  • 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 Oct 22 '15 at 12:24
  • @Mogsdad I added my current function code. Just missing the actual creation of the half-baked email. – Mor Sagmon Oct 24 '15 at 15:43
  • Your prose describes user interactions to complete the email, but your code includes nothing that does that. Now it sounds like you're just looking for the `MailApp.sendMail()` command, which you could get from documentation, or [this tutorial](https://developers.google.com/apps-script/articles/sending_emails?hl=en). I still don't find this question to be clear. – Mogsdad Oct 24 '15 at 15:51
  • Had I just wanted to send an email, I'd use MailApp.sendEmail. It's straightforward. I need a process that starts the "sendEmail" process, but STOPS just before actually SENDING the message, so the user takes control and clicks "Send" manually. – Mor Sagmon Oct 25 '15 at 05:30
  • difference to draft? –  Oct 26 '15 at 16:07
  • @Dani, I don't mind going through a draft, but the end-state needs to be an open message for the user to complete and click "send". Can this be done? – Mor Sagmon Oct 28 '15 at 09:35

1 Answers1

0

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());
}
Amit Agarwal
  • 10,910
  • 1
  • 32
  • 43