gmail api作为答复发送时添加标签ID和线程ID(答复时发送具有相同线程ID的邮件) [英] gmail api adding label id and thread id while sending as reply (send mail with same thread id while reply)
问题描述
我正在尝试使用laravel使用gmail api发送邮件.
I'm trying to send mail using gmail api using laravel.
我发送的味精是
$text = 'From: '.$from.'
To: '.$to.'
Subject:'.$subject.'
'.$body.'';
$encoded_message = rtrim(strtr(base64_encode($text), '+/', '-_'), '=');
$message->setRaw($encoded_message);
$message = $service->users_messages->send($userId, $message);
我尝试如下编辑标签ID和线程ID,
I tried to edit label id and thread id as follows,
$text = 'labelIds: ':'.SENT.'
'From: '.$from.'
To: '.$to.'
Subject:'.$subject.'
'.$body.'';
给出语法错误.如何为gmail-api添加labelid和线程ID?
which gives syntax error. How to add labelid and thread id for gmail-api?
edit1
我发送后的消息是
object(Google_Service_Gmail_Message)#1048 (14){
[
"historyId"
] => string(4) "4171" [
"id"
] => string(16) "15270b9c7b867bab" [
"internalDate"
] => string(13) "1453590169000" [
"labelIds"
] => NULL
这是creating a new threadId
,我需要在其中sent it as reply
.我怎么send the mail with same threadId
?
It's creating a new threadId
, where i need to sent it as reply
. How can i send the mail with same threadId
?
推荐答案
您可能现在已经解决了这个问题,但是我遇到了类似的问题,并且在寻找解决方案时发现了该线程,因此我想分享一下我使用的方法别人需要它.
You probably solved this by now, but I had similar issues and found this thread while looking for a solution, so I wanted to share the approach I used in case someone else needs it.
基于Gmail的API规范
Based on Gmail's API specification
1.请求的threadId必须在随请求提供的Message或Draft.Message上指定.
2. References和In-Reply-To标头必须按照RFC 2822标准进行设置.
3.主题标题必须匹配.
1.The requested threadId must be specified on the Message or Draft.Message you supply with your request.
2.The References and In-Reply-To headers must be set in compliance with the RFC 2822 standard.
3.The Subject headers must match.
数字2对我来说很复杂,因为我尝试手动设置References和In-Reply-To标头.我的想法是从同一线程中的最后一条消息中获取它们,但是API并未返回这些标头,而且我设置的内容显然不准确.然后,按照此线程 MIME标头无法通过Gmail API ,我删除了所有其他标头,并仅设置threadId和匹配的主题.
The number 2 complicated things for me, because I tried to set the References and In-Reply-To headers manually. My idea was to get them from the last message in the same thread, but the API didn't return those headers and what I set was apparently inaccurate. Then, following this thread MIME Headers Not Making it Through Gmail API, I removed all additional headers and set only the threadId and a matching subject.
我使用 PHPMailer 库来格式化mime字符串,而不是手动进行格式化(这会减少错误的可能性).使用composer,您只需要在composer.json的require部分中添加"phpmailer/phpmailer":〜5.2". 这是我的解决方案:
I used PHPMailer library for formatting the mime string, instead of doing it manually (it decreases the possibility of mistakes). With composer, you just need to add "phpmailer/phpmailer": "~5.2" in the require section of your composer.json. Here's my solution:
$thread = $gmail->users_threads->get($user_id,$threadId);
if($thread) {
$opt_param['threadId'] = $threadId;
$thread_messages = $thread->getMessages($opt_param);
if($thread_messages) {
$messageId = $thread_messages[0]->getId();
$messageDetails = $gmail->users_messages->get($messageId);
// get the subject here from the headers of $messageDetails. You will use it below as $subject.
}
}
$message = new Google_Service_Gmail_Message();
$mail = new PHPMailer();
$mail->From = 'YOUR_EMAIL'; // I tried with 'me' here, but PHPMailer doesn't consider it valid, so it can either be the email or userId
$mail->FromName = 'YOUR_NAME';
$mail->addAddress('RECIPIENT_EMAIL'); // Make sure this is the same as the email in the message you reply to
$mail->Subject = $subject; // the subject from $messageDetails from above
$mail->Body = $body;
$mail->preSend();
$mime = $mail->getSentMIMEMessage();
$raw = rtrim(strtr(base64_encode($mime), '+/', '-_'), '='); // web safe base64 encode
$message->setRaw($raw); // You set the thread id to your message object now, separately from the other headers
$message->setThreadId($threadId);
$gmail->users_messages->send($user_id, $message);
上面使用的变量:
$gmail - your instance of Google_Service_Gmail;
$user_id - the id of the authenticated user (can be 'me' for the current logged in user);
$threadId - the thread under which you want to send your email
希望这会有所帮助.
这篇关于gmail api作为答复发送时添加标签ID和线程ID(答复时发送具有相同线程ID的邮件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!