我试着用gmail用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,如下所示,
$text = 'labelIds: ':'.SENT.'
'From: '.$from.'
To: '.$to.'
Subject:'.$subject.'
'.$body.'';
这会产生语法错误。如何为gmail添加标签和线程id?
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
发布于 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“。这是我的解决方案:
$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
希望这会有帮助。
https://stackoverflow.com/questions/34960683
复制相似问题