2014-10-29 138 views
4

我們正在整合GMail,使用它們提供的最新API作爲我們應用程序中開發電子郵件客戶端的一部分。我們正在使用Google提供的PHP客戶端庫。使用Gmail新API發送包含附件的郵件,但不會顯示在收件箱的收件箱中

見鏈接https://developers.google.com/api-client-library/php/

在此我試圖發送郵件帶有附件。這裏我們已經生成了message/rfc822兼容文本,並通過'raw'參數傳遞它。這裏我發現的問題是,在執行代碼之後,當我檢查通過GMail API發送的郵件的發送郵件時,附件顯示正確。但是沒有收到/顯示發件人的郵箱。

看到代碼的詳細信息:

require_once DOCUMENT_ROOT . '/../library/google/src/Google/Client.php'; 
require_once DOCUMENT_ROOT . '/../library/google/src/Google/Service/Gmail.php'; 

function encodeRecipients($recipient){ 
    $recipientsCharset = 'utf-8'; 
    if (preg_match("/(.*)<(.*)>/", $recipient, $regs)) { 
     $recipient = '=?' . $recipientsCharset . '?B?'.base64_encode($regs[1]).'?= <'.$regs[2].'>'; 
    } 
    return $recipient; 
} 

$isAccessCodeExpired = 0; 
$arrAccessToken = array(); 
$session = new Zend_Session_Namespace(); 

$client = new Google_Client(); 
$client->setClientId($this->client_id); 
$client->setClientSecret($this->client_secret); 
$client->setRedirectUri($this->redirect_uri); 
$client->setAccessType('offline'); 
$client->setApprovalPrompt('force'); 

$client->addScope("https://mail.google.com/"); 
$client->addScope("https://www.googleapis.com/auth/gmail.compose"); 
$client->addScope("https://www.googleapis.com/auth/gmail.modify"); 
$client->addScope("https://www.googleapis.com/auth/gmail.readonly"); 


if ($this->getRequest()->getParam('code')) { 
    $code = $this->getRequest()->getParam('code'); 
    $client->authenticate($code); 
    $session->gmail_access_token = $client->getAccessToken(); 
    //$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; 
    $redirect = BASE_PATH . '/oauth2callback'; 
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); 
} 

$isAccessCodeExpired = $client->isAccessTokenExpired(); 
if (isset($session->gmail_access_token) && $session->gmail_access_token != "" && $isAccessCodeExpired !== 1) { 

    $client->setAccessToken($session->gmail_access_token);    
    $objGMail = new Google_Service_Gmail($client); 

    $strMailContent = 'This is a test mail which is sent via using Gmail API client library.<br/><br/><br/>Thanks,<br/>GMail API Team.'; 
    $strMailTextVersion = strip_tags($strMailContent, ''); 

    $strRawMessage = ""; 
    $boundary = uniqid(rand(), true); 
    $subjectCharset = $charset = 'utf-8'; 
    $strToMailName = 'To User Name'; 
    $strToMail = '[email protected]'; 
    $strSesFromName = 'From User Name'; 
    $strSesFromEmail = '[email protected]'; 
    $strSubject = 'Test mail using GMail API - with attachment - ' . date('M d, Y h:i:s A'); 

    $strRawMessage .= 'To: ' . encodeRecipients($strToMailName . " <" . $strToMail . ">") . "\r\n"; 
    $strRawMessage .= 'From: '. encodeRecipients($strSesFromName . " <" . $strSesFromEmail . ">") . "\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"; 

    $filePath = '/home/server/Downloads/credentials.csv'; 
    $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension 
    $mimeType = finfo_file($finfo, $filePath); 
    $fileName = 'credentials.csv'; 
    $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"; 

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

     print('Message sent object'); 
     print($objSentMsg); 

    } catch (Exception $e) { 
     print($e->getMessage()); 
     unset($_SESSION['access_token']); 
    } 
} 

請幫我... 在此先感謝。

+0

您檢查垃圾郵件文件夾?由於標題數據錯誤,我遇到了一些電子郵件問題 – Bowdzone 2014-10-29 10:23:27

+0

它正在收件人的收件箱中,但沒有附件。你有使用PHP中的GMail API發送郵件的正常工作代碼示例嗎? – 2014-10-29 12:04:48

+0

你的代碼對我來說工作得很好。我可以在發送的郵件文件夾和收件箱中看到附件。我像你一樣使用了csv附件。某種程度上你的文件損壞了嗎?你可以使用不同的CSV再試一次嗎? – 2014-11-06 19:49:39

回答

1

替換您:

$strRawMessage .= 'Content-type: Multipart/Alternative; boundary="' . $boundary . '"' . "\r\n"; 

有:

$strRawMessage .= 'Content-type: Multipart/Mixed; boundary="' . $boundary . '"' . "\r\n"; 
相關問題