通过GMail API从我的Javascript应用程序发送电子邮件 - 邮件出现在GMail发送列表中,但未发送到目标电子邮件地址 [英] Sending EMail from my Javascript App via GMail API - mail appears in GMail sent list, but isn't delivered to destination email address

查看:151
本文介绍了通过GMail API从我的Javascript应用程序发送电子邮件 - 邮件出现在GMail发送列表中,但未发送到目标电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编写一个客户端(Chrome浏览器)应用程序,它通过REST API与GMail集成。我的应用程序是用Javascript / Angular编写的,大多数GMail集成工作正常。它可以从GMail中获取 - 电子邮件,个人资料,标签等。

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.

我无法发送我创建的电子邮件。但是,我尝试发送的电子邮件确实出现在GMail发送列表中,如果我通过添加INBOX标签修改电子邮件,它们也会出现在GMail收件箱中。但是没有一封电子邮件能够到达目的地。我一直在测试几个电子邮件帐户 - Hotmail,Yahoo和另一个GMail帐户。电子邮件从未发送到目的地 - 我已经检查了收件箱,垃圾邮件等。

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.

我的代码在下面...函数'initializeGMailInterface'首先运行(通过用户界面)授权,然后'sendEmail'功能(也通过用户界面)。该代码似乎跟踪了我见过的示例以及Google为其REST API提供的文档。身份验证似乎工作正常 - 正如我所提到的,我可以获取电子邮件等。

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"
    };
}


推荐答案

您的代码正在执行< a href =https://developers.google.com/gmail/api/v1/reference/users/messages/insert =nofollow> insert()。请改为发送()

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

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

这篇关于通过GMail API从我的Javascript应用程序发送电子邮件 - 邮件出现在GMail发送列表中,但未发送到目标电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆