如何使用 php 解码 base64 电子邮件?



我正在尝试使用 base 64 解码方法解码我的消息。有没有人知道如何做到这一点,或者可能通过 php 函数?

<?php
class Gmail
{
public function __construct($client)
{
$this->client = $client;
}
public function readLabels()
{
$service = new Google_Service_Gmail($this->client);
// Print the labels in the user's account.
$user = 'me';
$results = $service->users_labels->listUsersLabels($user);

$the_html = "";
if (count($results->getLabels()) == 0) {
// print "No labels found.n";
$the_html .= "<p>No labels found</p>";
} else {
// print "Labels:n";
$the_html .= "<p>labels</p>";

foreach ($results->getLabels() as $label) {
// printf("- %sn", $label->getName());
$the_html .= "<p>" . $label->getName() . "</p>";
}
return $the_html;
}
}
/**
* Get list of Messages in user's mailbox.
*
* @param  Google_Service_Gmail $service Authorized Gmail API instance.
* @param  string $userId User's email address. The special value 'me'
* can be used to indicate the authenticated user.
* @return array Array of Messages.
*/
public function listMessages()
{
$service = new Google_Service_Gmail($this->client);
// Print the labels in the user's account.
$userId = 'me';
$pageToken = null;
$messages = array();
$opt_param = array();
$messagesResponse = array();
$i = 0;
do {
if ($i == 5) break;
$i++;
try {
if ($pageToken) {
$opt_param['pageToken'] = $pageToken;
}
$messagesResponse = $service->users_messages->listUsersMessages($userId, $opt_param);
if ($messagesResponse->getMessages()) {
$messages = array_merge($messages, $messagesResponse->getMessages());
$pageToken = $messagesResponse->getNextPageToken();
}
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
} while ($pageToken);
foreach ($messages as $message) {
print 'Message with ID: ' . $message->getId() . '<br/>';
$msg = $service->users_messages->get($userId, $message->getId());
echo "<pre>" . var_export($msg->payload->parts[1]->body->data->base64_decode, true) . "</pre>";
}
return $messages;
}
}

正如@Marvin在上面的评论中提到的,尝试使用base64_decode函数。

下面是一个示例:

$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo base64_decode($str); // This is an encoded string

所以在您的情况下,而不是使用

echo "<pre>" . var_export($msg->payload->parts[1]->body->data->base64_decode, true) . "</pre>";

尝试

echo "<pre>" . var_export(base64_decode($msg->payload->parts[1]->body->data), true) . "</pre>";

参考

base64_decode

最新更新