2

I am using Google API PHP Client. I am writing a program which imports bulk messages from one account to another account. To import, I am using the line:

$service->users_messages->import('me', $message, $options);

..where $message is the raw message which comes from $service->users_messages->get().

First, the program gets the list of messages from one account. User selects the messages he wants to import then clicks the Import button. The importing loops the selected messages and works fine, but there are some messages which contain large attachments. These messages will fail and return the Error 413 - Request entity too large.

I have read that I need to indicate that the upload type should be multipart. I am using Google API PHP Client and I don't see any methods, classes or options to add this upload type.

Any suggestions will be helpful. Thanks.

Dave P.
  • 177
  • 7

1 Answers1

0

Try setting the variable $message to this mail with attachment code snippet.

$strRawMessage .= "\r\n--{$boundary}\r\n";
$strRawMessage .= 'Content-Type: '. $mimeType .'; name="'. $fileName .'";' . "\r\n"; 
$strRawMessage .= 'Content-ID: <' . $strSesFromEmail . '>' . "\r\n"; 
$strRawMessage .= 'Content-Description: ' . $fileName . ';' . "\r\n";
$strRawMessage .= 'Content-Disposition: attachment; filename="' . $fileName . '"; size=' . filesize($filePath). ';' . "\r\n";
$strRawMessage .= 'Content-Transfer-Encoding: base64' . "\r\n\r\n";
$strRawMessage .= chunk_split(base64_encode(file_get_contents($filePath)), 76, "\n") . "\r\n";
$strRawMessage .= '--' . $boundary . "\r\n";

$strRawMessage .= "\r\n--{$boundary}\r\n";
$strRawMessage .= 'Content-Type: text/plain; charset=' . $charset . "\r\n";
$strRawMessage .= 'Content-Transfer-Encoding: 7bit' . "\r\n\r\n";
$strRawMessage .= $strMailTextVersion . "\r\n";

$strRawMessage .= "--{$boundary}\r\n";
$strRawMessage .= 'Content-Type: text/html; charset=' . $charset . "\r\n";
$strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
$strRawMessage .= $strMailContent . "\r\n";
// The message needs to be encoded in Base64URL
$mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
$msg = new Google_Service_Gmail_Message();
$msg->setRaw($mime);

Also check this curl implementation or this ajax implementation

Community
  • 1
  • 1
Mr.Rebot
  • 6,703
  • 2
  • 16
  • 91
  • Thanks, but still didn't work. My message is in the same format and they are being imported successfully except for those who have large attachments. – Dave P. Jul 25 '16 at 13:28