使用gmail API编辑原始gmail邮件并保留电子邮件标题

时间:2019-01-07 09:21:12

标签: gmail

我需要使用gmail api编辑消息并在gmail的原始消息中添加一些行。 我已获得用户添加,编辑和删除电子邮件的许可。 为此,我使用了gmail的插入api,但它更改了原始电子邮件标头。我需要保留原始标头并删除原始消息。以下是我用于此目的的api链接

https://developers.google.com/gmail/api/v1/reference/users/messages/insert

enter image description here enter image description here

下面是用于执行上述操作的代码

$sqltoken = mysql_query("select oauth_uid, token, thread_id, full_body, mail_subject, mail_to, mail_from, message_id, mail_date from gmail_access_data_cron where thread_id='**********'")or die(my_error());
$messages = array();
$result = mysql_fetch_object($sqltoken);
//used gmail vendor library
require 'vendor/autoload.php';
$CLIENT_ID = **********;
$CLIENT_SECRET = **********;
$data = json_decode($result->token);
$auth_array = array(
        'access_token'  => **********,
        'token_type'    => 'Bearer',
        'expires_in'    => **********,
        'created'       => **********,
        'refresh_token' => **********,
        );
$client = new Google_Client();
$client->SetClientId("$CLIENT_ID");
$client->setClientSecret("$CLIENT_SECRET");
$service = new Google_Service_Gmail($client);
$client->setAccessToken($auth_array);
$user = 'me';
//converted the message in base64_encode which is appended to the existing message
$myObj['raw'] = "c2FmZWluYm94IHNhZmVpbmJveCBzYWZlaW5ib3g=";
$myObj['thread_id'] = $result->thread_id;
$raw_message =json_encode($myObj);
$raw_message .= "id: $result->thread_id\r\n";
$raw_message .= "To: $result->mail_to \r\n";
$raw_message .= "From: $result->mail_from \r\n";
$raw_message .= 'Subject: =?utf-8?B?' .base64_encode("$result->mail_subject") . "?=\r\n";
$raw_message .= "MIME-Version: 1.0\r\n";
$raw_message .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$template = "safeinbox safeinbox".$result->full_body;
$raw_message .= $template;
$mime = rtrim(strtr(base64_encode($raw_message), '+/', '-_'), '=');
$message = new Google_Service_Gmail_Message();
$message->setRaw($mime);

//using insert api of gmail 
$insert = $service->users_messages->insert($user, $message);
print_r($insert);

boxbe url中实现了类似功能 https://www.boxbe.com/

0 个答案:

没有答案