Gmail API文件附件未在接收方显示

时间:2018-01-05 18:04:36

标签: php gmail-api

我有一些Gmail API PHP代码,用于发送带附件的电子邮件。附件在发件人方面完美发送,即使它们在发件人发送的项目中也可用。但他们没有在接收器端显示。可能是什么问题呢?我的代码如下所示。

$to = $_POST['replyTo'];
$message = $_POST['replyMsg'];
$thread_id = $_POST['thread_id'];
$subject = $_POST['MsgSubject'];
$cc = $_POST['cc'];
$bcc = $_POST['bcc'];

$strCCName = '';
$strBCCName = '';

if (!empty($cc)) {
    $encodedCC = "";
    if (strpos($cc, ",") !== false) {
        $list = explode(",", $cc);

        foreach ($list as $ccmail) {
            $encodedCC .= encodeRecipients($strCCName . " <" . $ccmail . ">") . ",";
        }
    } else {
        $encodedCC = encodeRecipients($strCCName . " <" . $cc . ">");
    }
}

if (!empty($bcc)) {
    $encodedBCC = "";
    if (strpos($bcc, ",") !== false) {
        $list = explode(",", $bcc);

        foreach ($list as $bccmail) {
            $encodedBCC .= encodeRecipients($strCCName . " <" . $bccmail . ">") . ",";
        }
    } else {
        $encodedCC = encodeRecipients($strCCName . " <" . $bcc . ">");
    }
}

$strMailContent = $message;
$strMailTextVersion = strip_tags($strMailContent, '');

$strRawMessage = "";
$boundary = uniqid(rand(), true);
$subjectCharset = $charset = 'utf-8';
$strToMailName = '';
$strToMail = $to;
$strSesFromName = $_SESSION['userData']['first_name']." ".$_SESSION['userData']['last_name'];
$strSesFromEmail = $_SESSION['userData']['email'];
$strSubject = $subject;

$strRawMessage .= 'To: ' . encodeRecipients($strToMailName . " <" . $strToMail . ">") . "\r\n";
$strRawMessage .= 'From: '. encodeRecipients($strSesFromName . " <" . $strSesFromEmail . ">") . "\r\n";
if (!empty($cc)) {
    $strRawMessage .= 'Cc: ' . $encodedCC . "\r\n";
}
if (!empty($bcc)) {
    $strRawMessage .= 'Bcc: ' . $encodedBCC . "\r\n";
}
$strRawMessage .= 'Subject: =?' . $subjectCharset . '?B?' . base64_encode($strSubject) . "?=\r\n";
$strRawMessage .= 'MIME-Version: 1.0' . "\r\n";
$strRawMessage .= 'Content-type: Multipart/Alternative; boundary="' . $boundary . '"' . "\r\n";

/**** check if  message contains any attachments *****/

if(count($_FILES['attachment']['name']) > 0){
    //Loop through each file

    for($i=0; $i<count($_FILES['attachment']['name']); $i++) {
        $j=$i+1;
        //Get the temp file path
        $filePath = $_FILES['attachment']['tmp_name'][$i];

        //Make sure we have a filepath
        if($filePath != ""){
            $fileName=$_FILES['attachment']['name'][$i];

            $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
            $mimeType = finfo_file($finfo, $filePath);
            $fileData = base64_encode(file_get_contents($filePath));

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


try {
    // The message needs to be encoded in Base64URL
    $mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
    $msg = new Google_Service_Gmail_Message();
    $msg->setRaw($mime);
    $msg->setThreadId($thread_id);
    $objSentMsg = $service->users_messages->send("me", $msg);

   echo "msg sent!";
} catch (Exception $e) {
    print($e->getMessage());

}
它已经好几个小时搞乱了,但没有找到任何东西。任何人都可以帮我解决这个问题吗?感谢。

0 个答案:

没有答案