2

Is it possible to use smtp.gmail.com from within Google Apps Script?

I am getting this error:

Exception: Address unavailable: http://smtp.gmail.com

Here's the code:

var user = 'user@gmail.com';
var password = 'thisisapassword';

var emailData = {
              'to': 'recipient@example.com',
              'subject': 'This works!',
              'content': '<b>Testing successful</b>'
            };
            
            var url = "smtp.gmail.com";
            var request = {method:'POST',
                          contentType: 'text/html', 
                          payload: emailData, 
                          headers:{"Authorization":"Basic " + Utilities.base64Encode(user + ":" + password), 
                          muteHttpExceptions: true}};
            
            UrlFetchApp.fetch(url, request);
Kos
  • 4,890
  • 9
  • 38
  • 42
Avid
  • 121
  • 2
  • 9
  • 1
    POST is an HTTP protocol command. SMTP is a different protocol. It is not HTTP. – lurker Jul 02 '20 at 16:58
  • Does this answer your question? [Can I send email using javascript](https://stackoverflow.com/questions/4454294/can-i-send-email-using-javascript) – lurker Jul 02 '20 at 16:59
  • smtpjs is a webservice so no go. If SMTP is a different protocol is there a js library equivalent to the Python smtplib? – Avid Jul 02 '20 at 17:43
  • If you look closely at the other question I linked to as duplicate, the webservice solution was the accepted answer, but other answers are provided there which do no use webservice. You should check those. Otherwise, you should do an internet search for that. I'm sure there are different ways of interacting with SMTP from js. – lurker Jul 02 '20 at 17:45

1 Answers1

1

In Apps Script you can use the Class MailApp to send emails.

You can use for example the sendEmail(recipient, subject, body, options) method:

// Send an email with two attachments: a file from Google Drive (as a PDF) and an HTML file.
var file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
var blob = Utilities.newBlob('Insert any HTML content here', 'text/html', 'my_document.html');
MailApp.sendEmail('mike@example.com', 'Attachment example', 'Two files are attached.', {
    name: 'Automatic Emailer Script',
    attachments: [file.getAs(MimeType.PDF), blob]
});
Kessy
  • 1,894
  • 1
  • 8
  • 15
  • 1
    Both MailApp and GmailApp are limited to sending from the current user. AFAIK, smtp is required if you want to send the email from another user account, thus my question on its use from within Apps Script. – Avid Jul 03 '20 at 18:22
  • 1
    What is what you want to accomplish? Is it a web app, which sends emails as the user running it? If that is the case and you have a G Suite account, have you checked using [Service Accounts](https://developers.google.com/identity/protocols/oauth2/service-account) – Kessy Jul 06 '20 at 07:58
  • The goal is simply a script that sends emails from different user accounts. While it is trivial to accomplish this with a Python script (see: smtplib), I am hopeful there is a way to do so while staying within the Apps Script environment because it's: easier/cleaner/faster/simpler/hidden. – Avid Jul 07 '20 at 16:22
  • @Avid Have you been able to do it? Have you tried using service accounts or looking for external js libraries? – Kessy Jul 13 '20 at 06:26
  • The service account may be a little too permissive since it requires domain-wide delegation and gives the service account access to everything. There is a Node.js library for sending messages (see: Nodemailer), although I'm not sure if it's possible to use it directly in Apps Script or if it requires some modification? – Avid Jul 15 '20 at 17:15
  • Yes, you will have to modify the library, as mentioned on the answer of this post: https://stackoverflow.com/questions/49204546/using-an-imported-module-inside-google-app-script – Kessy Jul 20 '20 at 07:27