7

I' m using gapi to send gmail. But if I send the mail to myself, it doesn't appear in my inbox. The most strange, it appears in the 'Sent' folder. `

function sendMessage(email, callback) {
    // Web-safe base64 
    var base64EncodedEmail = btoa(email).replace(/\//g,'_').replace(/\+/g,'-');
    var request = gapi.client.gmail.users.messages.send({
        'userId': 'me',
        'message': {
            'raw': base64EncodedEmail
        }
    });
    request.execute(callback);
}

function send() {
    var var to = '[email protected]',
        subject = 'Hello World',
        content = 'send a Gmail.'

    var email = "From: 'me'\r\n"+
        "To:  "+ to +"\r\n"+
        "Subject: "+subject+"\r\n"+
        "\r\n"+
        content;
    sendMessage(email, function () {
        console.log(arguments);
    });
}

` The information log in console is:

"[↵ {↵  "id": "gapiRpc",↵  "result": {↵   "id": "1471e09d88000bf7",↵   "threadId": "1471e09d88000bf7",↵   "labelIds": [↵    "SENT"↵   ]↵  }↵ }↵]↵"

And I can see this mail in the 'Sent' folder in Gmail, but I didn't get it in the inbox. The original of this mail in the 'Sent' folder is as follow:

Received: from 823618323534-124pu7sujfj5olv94rkr0apqdppc6nti.apps.googleusercontent.com
named unknown
by gmailapi.google.com
with HTTPREST;
Wed, 9 Jul 2014 19:11:07 -0700
From: [email protected]
To:  [email protected]
Subject: Hello World
Date: Wed, 9 Jul 2014 19:11:07 -0700
Message-Id: <CADOxe9KLTdYfcb8OxWz+rFSM4tVKuZcVV8HJ4byv95+qPA=+dw@mail.gmail.com>

send a Gmail.

Anybody knows why? Thanks very much!

Edit: I change the 'To:' in Mime message to another mailbox(not Gmail), and receive the mail correctly. Feeling More confused.

Edit: I add a 'Cc': field, It is : [email protected](myself), [email protected], [email protected], [email protected] . All the other guys(Gmail and other mailboxs) received the mail, but I still couldn't receive the mail from myself.

The original mail I received in [email protected] is as follow:

Received: from mail-qg0-f44.google.com (unknown [209.85.192.44])
by mx42 (Coremail) with SMTP id XMCowEAJvlfC_L1TjzExBg--.81S3;
Thu, 10 Jul 2014 10:39:03 +0800 (CST)
Received: by mail-qg0-f44.google.com with SMTP id j107so7126918qga.31
    for <[email protected]>; Wed, 09 Jul 2014 19:38:57 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
    d=gmail.com; s=20120113;
    h=mime-version:from:date:message-id:subject:to:cc:content-type;
    bh=zgoFEkSzE1bUNp54umXlfaDEDw5bLf2Ei0uFrgrVZic=;
    b=UHOLwuTsFL9yajR0nn/TskbhVKrIfzX4OAsNDno4S2QIvwx83H5dOe2WMyFbL6Plmk
     n4z2qzGjturoi1411+PGpgj8rt4Y57aDQpu7tEOMolMmFLJomtCSmagHIHhc0qwU+CRL
     rTjO2ztwZoj/ejnqwcmANzgzMMnSxxkcIf3OvXhLm+j+5yHQvPhmGWIapFWJaTN+9gb9
     Q47Qkqe5dBrsxOnGhgQnr1orbE5NcLIYumZTH1YfCMZvqIjUtmviUQUKpfhNQD5UtCX0
     0J2moKK98Q5Vek4Wti/WtnEqOgNTzHkIL1M90eeAJKelyPu4TQ7G9GJxr1FX+s4WhgrM
     xTFA==
MIME-Version: 1.0
X-Received: by 10.224.137.9 with SMTP id u9mr78577439qat.24.1404959937414;
 Wed, 09 Jul 2014 19:38:57 -0700 (PDT)
Received: from 823618323534-124pu7sujfj5olv94rkr0apqdppc6nti.apps.googleusercontent.com
 named unknown by gmailapi.google.com with HTTPREST; Wed, 9 Jul 2014 19:38:56 -0700
From: [email protected]
Date: Wed, 9 Jul 2014 19:38:56 -0700
Message-ID: <CADOxe9KF+VjMFw1bSkFd09RVsa7c7-pz9OguCFovOUemW4ZZbg@mail.gmail.com>
Subject: Hello World
To: [email protected]
Cc: [email protected], [email protected], [email protected]
Content-Type: text/plain; charset=UTF-8
X-CM-TRANSID:XMCowEAJvlfC_L1TjzExBg--.81S3
Authentication-Results: mx42; spf=pass [email protected]; dki
m=pass [email protected]
X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73
VFW2AGmfu7bjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjxUSeT5DUUUU

send a Gmail.

Edit: the original mail in [email protected] is as follow:

Delivered-To: [email protected]
Received: by 10.194.121.101 with SMTP id lj5csp1399131wjb;
    Wed, 9 Jul 2014 19:38:58 -0700 (PDT)
Return-Path: <[email protected]>
Received-SPF: pass (google.com: domain of [email protected] designates 10.224.137.9 as     permitted sender) client-ip=10.224.137.9
Authentication-Results: mr.google.com;
   spf=pass (google.com: domain of [email protected] designates 10.224.137.9 as             permitted sender) [email protected];
   dkim=pass [email protected]
X-Received: from mr.google.com ([10.224.137.9])
    by 10.224.137.9 with SMTP id u9mr68151154qat.24.1404959937949 (num_hops = 1);
    Wed, 09 Jul 2014 19:38:57 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
    d=gmail.com; s=20120113;
    h=mime-version:from:date:message-id:subject:to:cc:content-type;
    bh=zgoFEkSzE1bUNp54umXlfaDEDw5bLf2Ei0uFrgrVZic=;
    b=UHOLwuTsFL9yajR0nn/TskbhVKrIfzX4OAsNDno4S2QIvwx83H5dOe2WMyFbL6Plmk
     n4z2qzGjturoi1411+PGpgj8rt4Y57aDQpu7tEOMolMmFLJomtCSmagHIHhc0qwU+CRL
     rTjO2ztwZoj/ejnqwcmANzgzMMnSxxkcIf3OvXhLm+j+5yHQvPhmGWIapFWJaTN+9gb9
     Q47Qkqe5dBrsxOnGhgQnr1orbE5NcLIYumZTH1YfCMZvqIjUtmviUQUKpfhNQD5UtCX0
     0J2moKK98Q5Vek4Wti/WtnEqOgNTzHkIL1M90eeAJKelyPu4TQ7G9GJxr1FX+s4WhgrM
     xTFA==
MIME-Version: 1.0
X-Received: by 10.224.137.9 with SMTP id u9mr78577439qat.24.1404959937414;
 Wed, 09 Jul 2014 19:38:57 -0700 (PDT)
Received: from 823618323534-124pu7sujfj5olv94rkr0apqdppc6nti.apps.googleusercontent.com
 named unknown by gmailapi.google.com with HTTPREST; Wed, 9 Jul 2014 19:38:56 -0700
From: [email protected]
Date: Wed, 9 Jul 2014 19:38:56 -0700
Message-ID: <CADOxe9KF+VjMFw1bSkFd09RVsa7c7-pz9OguCFovOUemW4ZZbg@mail.gmail.com>
Subject: Hello World
To: [email protected]
Cc: [email protected], [email protected], [email protected]
Content-Type: text/plain; charset=UTF-8

send a Gmail.
FlyingHorse
  • 332
  • 4
  • 15

3 Answers3

1

So to be clear, sending mail works as it sends correctly to others. In the case of sending mail to yourself (why exactly are you doing this? is this really needed as opposed to say just using messages.insert?) then it appears in only SENT but not also INBOX label. You only get one copy of the mail, that's the same irregardless of API or using web UI. The problem is that the web UI also puts that message in INBOX as well as SENT. (As you can tell, sending to yourself is a bit of a special case due to things like dupe-detection based on the Message-Id header, etc.)

Eric D
  • 6,901
  • 1
  • 15
  • 26
  • Thanks! What you said helps me a lot. At last I use the users/messages/modify api to add 'INBOX' and 'UNREAD' label to the mail in the 'SENT' folder. This perfectly solved my problem. – FlyingHorse Jul 12 '14 at 13:36
  • GmailGuy: It still seems like the messages.send API should do this for us. Now I have to ask users for "https://www.googleapis.com/auth/gmail.modify" auth scope just because they might want to send an email to themselves and have it show up in their inbox. – Brad Vogel Jul 13 '14 at 04:13
  • 2
    Yes, I agree messages.send(to=me) should do this--it's a bug...check back in a week or two. – Eric D Jul 17 '14 at 18:06
0

Answer the question myself. First I tried using messages.insert. However, if I set the 'From' the same as the 'To'(my email address), the mail will still only be in the 'SENT' folder. the right way is using the message.modify api to add 'INBOX' and 'UNREAD' label to the mail sent.

gapi.client.request({
     path: "gmail/v1/users/me/messages/" + messageid + "/modify",
     method: "POST",
     body: "{\"addLabelIds\": [\"UNREAD\",\"INBOX\"]}",
     callback: function() {
         console.log("gmail sent to self");
         return console.log(arguments);
     }
})

The 'messageid' refer to the id return by the send api.

FlyingHorse
  • 332
  • 4
  • 15
  • I wouldn't say using messages.modify to do this after the fact is "the right way" but agree that's what's necessary presently. If you do just want to put a message in the authenticated user's own inbox then messages.insert does make sense, you can specify arbitrary labels. If you are actually sending (e.g. it may go to any user, including this user) then messages.send is the right solution. – Eric D Jul 17 '14 at 18:03
  • I am also facing the same issue. I am trying to send an email to a different gmail account. The mails appear in the Sent items of the first account. But I do not receive all of the mails in the second account. :( – Nabeel May 21 '20 at 23:18
0

An untested idea: you can try adding an X-Gmail-Labels: Inbox header to the messages.send REST call in hopes that Gmail will add the label. I saw this header when doing a Google Takeout of some labels in GMail to mbox files.

TWiStErRob
  • 44,762
  • 26
  • 170
  • 254