Sending EMail from my Javascript App via GMail API - mail appears in GMail sent list, but isn't delivered to destination email address

RAdair

I've been writing a client (Chrome browser) App that integrates with GMail via the REST API. My app is written in Javascript/Angular and most of the GMail integration works fine. It can fetch from GMail - emails, profiles, labels, etc.

I'm not able to send emails I create. However, the emails I try to send do appear in the GMail sent list and, if I modify the email by adding the 'INBOX' label, they also appear in the GMail inbox. But none of the emails make it to their destination. I've been testing with several email accounts - Hotmail, Yahoo and another GMail account. Emails are never delivered to their destinations - I've checked the inboxes, spam, etc.

My code is below ... Function 'initializeGMailInterface' is run first (via the User Interface) to authorize and then the 'sendEmail' function (also via the User Interface). The code seems to track with examples I've seen and the documentation Google provides for their REST API. Authentication seems to work OK - and as I mentioned, I'm able to fetch emails, etc.

How do I get the emails to their destination?

var CLIENT_ID = '853643010367revnu8a5t7klsvsc5us50bgml5s99s4d.apps.googleusercontent.com';
var SCOPES = ['https://mail.google.com/', 'https://www.googleapis.com/auth/gmail.send', 'https://www.googleapis.com/auth/gmail.modify', 'https://www.googleapis.com/auth/gmail.labels'];

function handleAuthResult(authResult) {
    if (authResult && !authResult.error) {
        loadGmailApi();
    }
}

$scope.initializeGMailInterface = function() {
    gapi.auth.authorize({
        client_id: CLIENT_ID,
        scope: SCOPES,
        immediate: true
    }, handleAuthResult);
};

function loadGmailApi() {
    gapi.client.load('gmail', 'v1', function() {
        console.log("Loaded GMail API");
    });
}

$scope.sendEmail = function() {
    var content     = 'HELLO';
    // I have an email account on GMail.  Lets call it '[email protected]'
    var sender      = '[email protected]';
    // And an email account on Hotmail.  Lets call it '[email protected]'\
    // Note: I tried several 'receiver' email accounts, including one on GMail.  None received the email.
    var receiver    = '[email protected]';
    var to          = 'To: '   +receiver;
    var from        = 'From: ' +sender;
    var subject     = 'Subject: ' +'HELLO TEST';
    var contentType = 'Content-Type: text/plain; charset=utf-8';
    var mime        = 'MIME-Version: 1.0';

    var message = "";
    message +=   to             +"\r\n";
    message +=   from           +"\r\n";
    message +=   subject        +"\r\n";
    message +=   contentType    +"\r\n";
    message +=   mime           +"\r\n";
    message +=    "\r\n"        + content;

    sendMessage(message, receiver, sender);
};

function sendMessage(message, receiver, sender) {
    var headers = getClientRequestHeaders();
    var path = "gmail/v1/users/me/messages?key=" + CLIENT_ID;
    var base64EncodedEmail = btoa(message).replace(/\+/g, '-').replace(/\//g, '_');
    gapi.client.request({
        path: path,
        method: "POST",
        headers: headers,
        body: {
            'raw': base64EncodedEmail
        }
    }).then(function (response) {

    });
}

var t = null;
function getClientRequestHeaders() {
    if(!t) t = gapi.auth.getToken();
    gapi.auth.setToken({token: t['access_token']});
    var a = "Bearer " + t["access_token"];
    return {
        "Authorization": a,
        "X-JavaScript-User-Agent": "Google APIs Explorer"
    };
}
Brandon Jewett-Hall

Your code is doing an insert(). Do a send() instead:

var path = "gmail/v1/users/me/messages/send?key=" + CLIENT_ID;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Error Sending Email (Gmail) Via Python 2.6

From Dev

Sending mail (in Localhost) with CodeIgniter email() library from gmail does not work

From Dev

Sending a gmail email

From Dev

Properties for Sending email via gmail

From Dev

PHP mail is not sending email to my email address

From Dev

Sending email from gmail api not received but shown in sent folder

From Dev

Gmail PHP API Sending Email

From Dev

GMAIL API sending email with attachment in c#

From Dev

How to tell if an email sent via Gmail REST API has bounced?

From Dev

Sending email with signature using Gmail API

From Dev

GMAIL API for sending Email with attachment

From Dev

C# - Sending an email, via Gmail or Other?

From Dev

Python Gmail Api: Email is not sending as the specified from

From Dev

Cannot email via gmail

From Dev

Sending EMail from my Javascript App via GMail API - mail appears in GMail sent list, but isn't delivered to destination email address

From Dev

sending email from smtp gmail in c#

From Dev

Sending email via gmail & python

From Dev

Get direct URL to email from Gmail API (list messages)

From Dev

Sending email from Gmail doesn't work on CloudBees

From Dev

PHP mail is not sending email to my email address

From Dev

Gmail PHP API Sending Email

From Dev

GMAIL API sending email with attachment in c#

From Dev

Email from postfix sent to spam in gmail

From Dev

Setting gmail as default for sending files via email

From Dev

Sending Email using Gmail API in java

From Dev

Send smtp gmail email ''Failure sending mail''

From Dev

Gmail API, cannot remove "SENT" label from email

From Dev

Retrieve Account's email address in a Gmail app

From Dev

Sending an email with inline image and plain/text content via Gmail API

Related Related

  1. 1

    Error Sending Email (Gmail) Via Python 2.6

  2. 2

    Sending mail (in Localhost) with CodeIgniter email() library from gmail does not work

  3. 3

    Sending a gmail email

  4. 4

    Properties for Sending email via gmail

  5. 5

    PHP mail is not sending email to my email address

  6. 6

    Sending email from gmail api not received but shown in sent folder

  7. 7

    Gmail PHP API Sending Email

  8. 8

    GMAIL API sending email with attachment in c#

  9. 9

    How to tell if an email sent via Gmail REST API has bounced?

  10. 10

    Sending email with signature using Gmail API

  11. 11

    GMAIL API for sending Email with attachment

  12. 12

    C# - Sending an email, via Gmail or Other?

  13. 13

    Python Gmail Api: Email is not sending as the specified from

  14. 14

    Cannot email via gmail

  15. 15

    Sending EMail from my Javascript App via GMail API - mail appears in GMail sent list, but isn't delivered to destination email address

  16. 16

    sending email from smtp gmail in c#

  17. 17

    Sending email via gmail & python

  18. 18

    Get direct URL to email from Gmail API (list messages)

  19. 19

    Sending email from Gmail doesn't work on CloudBees

  20. 20

    PHP mail is not sending email to my email address

  21. 21

    Gmail PHP API Sending Email

  22. 22

    GMAIL API sending email with attachment in c#

  23. 23

    Email from postfix sent to spam in gmail

  24. 24

    Setting gmail as default for sending files via email

  25. 25

    Sending Email using Gmail API in java

  26. 26

    Send smtp gmail email ''Failure sending mail''

  27. 27

    Gmail API, cannot remove "SENT" label from email

  28. 28

    Retrieve Account's email address in a Gmail app

  29. 29

    Sending an email with inline image and plain/text content via Gmail API

HotTag

Archive