gpt4 book ai didi

php - 在 php 中使用 gmail api 发送带附件的邮件

转载 作者:行者123 更新时间:2023-12-02 15:01:06 28 4
gpt4 key购买 nike

当我尝试使用 php.text 发送邮件时,也会以名为 noname.html 的附件形式发送。请为我提供解决方案。我已经使用这个库发送电子邮件 https://github.com/google/google-api-php-client .我的代码是这样的。任何帮助将不胜感激。

$client->setAccessToken($_SESSION['gmail_access_token']);            
$objGMail = new Google_Service_Gmail($client);

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

$strRawMessage = "";
$boundary = uniqid(rand(), true);
$subjectCharset = $charset = 'utf-8';
$strToMailName = 'NAME';
$strToMail = 'name@gmail.com';
$strSesFromName = 'Premjith GMAIL API';
$strSesFromEmail = 'premji341800@gmail.com';
$strSubject = 'Test mail using GMail API - with attachment - ' . date('M d, Y h:i:s A');

$strRawMessage .= 'To: ' .$strToMailName . " <" . $strToMail . ">" . "\r\n";
$strRawMessage .= 'From: '.$strSesFromName . " <" . $strSesFromEmail . ">" . "\r\n";

$strRawMessage .= 'Subject: =?' . $subjectCharset . '?B?' . base64_encode($strSubject) . "?=\r\n";
$strRawMessage .= 'MIME-Version: 1.0' . "\r\n";
$strRawMessage .= 'Content-type: Multipart/Mixed; boundary="' . $boundary . '"' . "\r\n";

$filePath = 'abc.pdf';
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
$mimeType = finfo_file($finfo, $filePath);
$fileName = 'abc.pdf';
$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']);
}

当我更改代码行时 $strRawMessage .= '内容类型:多部分/混合; boundary="' . $boundary . '"' . "\r\n";$strRawMessage .= '内容类型:多部分/替代; boundary="' . $boundary . '"' . "\r\n"; 邮件仅以 html 格式发送,不带附件。请帮助我

最佳答案

我已经像这样更改了我的代码,它对我来说效果很好。谢谢大家的支持。

$client->setAccessToken($_SESSION['gmail_access_token']);            
$objGMail = new Google_Service_Gmail($client);

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

$strRawMessage = "";
$boundary = uniqid(rand(), true);
$subjectCharset = $charset = 'utf-8';
$strToMailName = 'NAME';
$strToMail = 'name@gmail.com';
$strSesFromName = 'Premjith GMAIL API';
$strSesFromEmail = 'premji341800@gmail.com';
$strSubject = 'Test mail using GMail API - with attachment - ' . date('M d, Y h:i:s A');

$strRawMessage .= 'To: ' .$strToMailName . " <" . $strToMail . ">" . "\r\n";
$strRawMessage .= 'From: '.$strSesFromName . " <" . $strSesFromEmail . ">" . "\r\n";

$strRawMessage .= 'Subject: =?' . $subjectCharset . '?B?' . base64_encode($strSubject) . "?=\r\n";
$strRawMessage .= 'MIME-Version: 1.0' . "\r\n";
$strRawMessage .= 'Content-type: Multipart/Mixed; boundary="' . $boundary . '"' . "\r\n";

$filePath = 'abc.pdf';
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
$mimeType = finfo_file($finfo, $filePath);
$fileName = 'abc.pdf';
$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 .= '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']);
}

关于php - 在 php 中使用 gmail api 发送带附件的邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49378400/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com