首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >gmail在发送回复时添加标签id和线程id (发送邮件时使用相同的线程id )

gmail在发送回复时添加标签id和线程id (发送邮件时使用相同的线程id )
EN

Stack Overflow用户
提问于 2016-01-23 07:08:45
回答 1查看 1.1K关注 0票数 0

我试着用gmail用laravel发送邮件。

我寄来的信息是

代码语言:javascript
运行
复制
$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,如下所示,

代码语言:javascript
运行
复制
$text = 'labelIds: ':'.SENT.'
'From: '.$from.'
To: '.$to.'
Subject:'.$subject.'

'.$body.'';

这会产生语法错误。如何为gmail添加标签和线程id?

edit1

我发送后的信息是,

代码语言:javascript
运行
复制
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

EN

回答 1

Stack Overflow用户

发布于 2016-05-17 14:00:03

您现在可能已经解决了这个问题,但是我遇到了类似的问题,并且在寻找解决方案时找到了这个线程,所以我想分享我使用的方法,以防其他人需要它。

基于Gmail的API规范

1.所请求的threadId必须在您随请求提供的消息或Draft.Message上指定。 2.必须按照RFC 2822标准设置引用和回复报头。 3.主题标题必须匹配。

数字2对我来说很复杂,因为我试图手动设置引用和回复中的标题。我的想法是从同一线程中的最后一条消息中获取它们,但是API没有返回这些头,而且我设置的内容显然不准确。然后,在这个线程MIME头没有通过Gmail API实现之后,我删除了所有附加的头,只设置了threadId和一个匹配的主题。

我使用PHPMailer库格式化mime字符串,而不是手动进行格式化(它减少了出错的可能性)。对于composer,只需在composer.json的require部分中添加"phpmailer/phpmailer":"~5.2“。这是我的解决方案:

代码语言:javascript
运行
复制
$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);

上文使用的变量:

代码语言:javascript
运行
复制
$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

希望这会有帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34960683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档