Search code examples
gmail

Is it possible to send gmail with API (instead of SMTP)


Let me explain the exact case:

ERP system with several google apps domain accounts as users (google login used)

I'm searching a way that each user can use a form to send email and that sent email should left also in the original gmail sent box.

I tested with SMTP script and it's working perfectly. However, the idea is not to store each user password as plain text, but to use some kind of API/Auth with key/secret instead of user/pass.

The ideal solution will be that this api/key to be provided from Apps admin globally - meaning the script shouldn't be edited when adding new user to domain.

Is it possible? I couldn't found a solution. There is an API, but only for some gmail settings and not for sending.

Thank you!


Solution

  • yes you can do that. Gmail have rolled out APIs for this. You would have to register your app and get user authenticated. Once you have the auth token, hit the api for details. (Make sure you take user's permission to send emails from his emailid.)

    http://www.googleapis.com/oauth2/v3/userinfoaccess_token="the access token"

    And then create header with the mailing options like this

    var headers = { 'authorization': 'Bearer ' + token, 'Content-Type' : 'message/rfc822', 'Accepted Media MIME types' : 'message/rfc822', 'To': '[email protected]' , 'Subject': 'Saying Hello' }

        var requestObject = {
                host: 'www.googleapis.com',
                 port: 443,
                path: '/upload/gmail/v1/users/'+ userId + '/messages/send?key=' + apiKey + '&uploadType=media',
                method: 'POST',
                headers: headers
    

    };

    Then make the http request with the requestObject and you're done.