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

回眸只為那壹抹淺笑 提交于 2019-12-01 07:48:46

问题


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 'theSenderEmail@gmail.com'
    var sender      = 'theSenderEmail@gmail.com';
    // And an email account on Hotmail.  Lets call it 'theReceiverEmail@gmail.com'\
    // Note: I tried several 'receiver' email accounts, including one on GMail.  None received the email.
    var receiver    = 'theReceiverEmail@hotmail.com';
    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"
    };
}

回答1:


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

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



回答2:


The 'emailJS' is a good solution for sending email from Javascript.

EmailJS helps to send emails using client-side technologies only. No server is required.

Additionally, you can easily add attachments, require CAPTCHA validation, switch

between the email services without making code changes, review the history of

the email request, and more.

More Info



来源:https://stackoverflow.com/questions/36946490/sending-email-from-my-javascript-app-via-gmail-api-mail-appears-in-gmail-sent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!