使用 Gmail 新 API 发送带附件的邮件,但不会在收件人的收件箱中显示 [英] Send Mail with attachment using Gmail new API, but it is not displayed for receiver's inbox
问题描述
我们正在使用他们提供的最新 API 集成 GMail,作为在我们的应用程序中开发电子邮件客户端的一部分.我们正在使用 Google 提供的 PHP 客户端库.
We are integrating GMail using the latest API provided by them as a part of developing email client within our application. We are using PHP client library provided by Google.
查看链接 https://developers.google.com/api-client-图书馆/php/
在这里,我尝试发送带有附件的邮件.在这里,我们生成了 message/rfc822 兼容文本,并使用raw"参数传递了它.这里我发现的问题是,在执行代码后,当我检查通过 GMail API 发送的邮件的已发送邮件时,附件显示正确.但是发件人的邮箱没有收到/显示.
In this I'm trying to send a mail with attachment. Here we have generated message/rfc822 compatible text and passed it with 'raw' parameter. Here the problem I found is, after executing the code, when I checked the sent mail for the mail which I sent via GMail API, the attachments are shown correctly. But it is not received/ displayed for the sender's mail box.
查看代码了解更多信息:
See the code for more info:
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 = 'toemail@gmail.com';
$strSesFromName = 'From User Name';
$strSesFromEmail = 'fromemail@gmail.com';
$strSubject = 'Test mail using GMail API - with attachment - ' . date('M d, Y h:i:s A');
$strRawMessage .= 'To: ' . encodeRecipients($strToMailName . " <" . $strToMail . ">") . "
";
$strRawMessage .= 'From: '. encodeRecipients($strSesFromName . " <" . $strSesFromEmail . ">") . "
";
$strRawMessage .= 'Subject: =?' . $subjectCharset . '?B?' . base64_encode($strSubject) . "?=
";
$strRawMessage .= 'MIME-Version: 1.0' . "
";
$strRawMessage .= 'Content-type: Multipart/Alternative; boundary="' . $boundary . '"' . "
";
$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 .= "
--{$boundary}
";
$strRawMessage .= 'Content-Type: '. $mimeType .'; name="'. $fileName .'";' . "
";
$strRawMessage .= 'Content-ID: <' . $strSesFromEmail . '>' . "
";
$strRawMessage .= 'Content-Description: ' . $fileName . ';' . "
";
$strRawMessage .= 'Content-Disposition: attachment; filename="' . $fileName . '"; size=' . filesize($filePath). ';' . "
";
$strRawMessage .= 'Content-Transfer-Encoding: base64' . "
";
$strRawMessage .= chunk_split(base64_encode(file_get_contents($filePath)), 76, "
") . "
";
$strRawMessage .= '--' . $boundary . "
";
$strRawMessage .= "
--{$boundary}
";
$strRawMessage .= 'Content-Type: text/plain; charset=' . $charset . "
";
$strRawMessage .= 'Content-Transfer-Encoding: 7bit' . "
";
$strRawMessage .= $strMailTextVersion . "
";
$strRawMessage .= "--{$boundary}
";
$strRawMessage .= 'Content-Type: text/html; charset=' . $charset . "
";
$strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "
";
$strRawMessage .= $strMailContent . "
";
//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']);
}
}
请帮帮我...提前致谢.
Please help me... Thanks in advance.
推荐答案
替换你的:
$strRawMessage .= 'Content-type: Multipart/Alternative; boundary="' . $boundary . '"' . "
";
与:
$strRawMessage .= 'Content-type: Multipart/Mixed; boundary="' . $boundary . '"' . "
";
这篇关于使用 Gmail 新 API 发送带附件的邮件,但不会在收件人的收件箱中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!