0
votes

I am trying to send an email through my system via Gmail API integration, but I receive an error i.e,

Google_Service_Exception { "error": { "errors": [ { "domain": "global", "reason": "invalidArgument", "message": "Recipient address required" } ], "code": 400, "message": "Recipient address required" } }

Here is the code in Laravel:

$obj = new \Google_Service_Gmail_Message ();
$raw = "MIME-Version: 1.0\n\r
From: Muhammad Bilal <[email protected]>\n\r
To: Umer Sheikh <[email protected]>\n\r
Subject: SIMPLE TEXT EMAIL\n\r
Date: Thu, 5 Mar 2020 18:45:33 +0500\n\r
Message-ID: <[email protected]>\n\r
Content-Type: text/plain; charset=\"UTF-8\"\n\r
\n\r
NO BODY";
$raw = rtrim(strtr(base64_encode($raw), '+/', '-_'), '=');
$obj->setRaw($raw);
$obj->setId('1234');
$obj->setInternalDate(null);
$obj->setLabelIds(null);

$payload = new \Google_Service_Gmail_MessagePart ();
$body = new \Google_Service_Gmail_MessagePartBody ();
$body->setData('test');
$body->setSize('1');

    $payload->setBody($body);
    $obj->setPayload($payload);
    $results = $service->users_messages->send($user, $obj);
2

2 Answers

1
votes

Issue resolved, found a sample code that worked

    $user = 'me';
    $strSubject = 'Test mail using GMail API' . date('M d, Y h:i:s A');
    $strRawMessage = "From: Muhammad Bilal <[email protected]>\r\n";
    $strRawMessage .= "To: Muhammad Bilal <[email protected]>\r\n";
    $strRawMessage .= 'Subject: =?utf-8?B?' . base64_encode($strSubject) . "?=\r\n";
    $strRawMessage .= "MIME-Version: 1.0\r\n";
    $strRawMessage .= "Content-Type: text/html; charset=utf-8\r\n";
    $strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
    $strRawMessage .= "this <b>is a test message!\r\n";
    $mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
    $msg = new \Google_Service_Gmail_Message();
    $msg->setRaw($mime);
    $service->users_messages->send("me", $msg);
0
votes

The error message you are getting is due to the fact that the Content-Type should be of type message/rfc822 and not text/plain:

As mentioned in the Users.messages: send documentation:

This method supports an /upload URI and accepts uploaded media with the following characteristics:

  • Maximum file size: 35MB

  • Accepted Media MIME types: message/rfc822

Therefore the entire email message should be in an RFC 2822 formatted base64url encoded string.

Reference