dtnmnw3697 2016-03-02 17:01
浏览 55
已采纳

克隆邮件并使用原始“发件人”标头发送邮件

I'm trying to develop a PHP script resting on the Gmail API that would make possible to snooze my messages at a specific time, i.e. archive and un-archive a message at a specific datetime.

Everything is in place and works but a detail, that is I cannot keep the "From" headers of the original message. More specifically:

  1. The ID of the message to snooze is retrieved at a specific time thanks to a CRON job;
  2. Using the message ID, the message in raw format is retrieved and cloned;
  3. The original message is deleted and the cloned message is sent (and received) => This ensures that the email is displayed at the top of the inbox.

Problem: the cloned email is a perfect copy of the original one but the "From" headers which display the email address of the authenticated user, i.e. myself (username@gmail.com).

//[...] object $this->message

private function cloneMail() {

   // GET RAW message
   $this->message->raw = $this->gmail->users_messages->get(
                                        $this->message->user, 
                                        $this->message->id,
                                        array('format'=>'raw')
                                       );

   try {

     // INSERT original message
     $inserted = $this->gmail->users_messages->delete(
                                              $this->message->user,
                                              $this->message->id
                                                 );

        // ONCE DELETED, SEND CLONED EMAIL    
        if ($deleted->getId()) {
            try {
                $this->gmail->users_messages->send(
                                           $this->message->user, 
                                           $this->message->raw
                                           );
                } catch (Exception $e) {
                    // -- Fallback...
                }
          }

    } catch(Exception $e) {}
}

[Updated] Working solution using messages.insert()

private function cloneMail() {

   // GET RAW message
   $this->message->raw = $this->gmail->users_messages->get(
                                        $this->message->user, 
                                        $this->message->id,
                                        array('format'=>'raw')
                                       );

   try {

     // DELETE original message
     $deleted = $this->gmail->users_messages->delete(
                                              $this->message->user,
                                              $this->message->id
                                                 );

        // ONCE DELETED, SEND CLONED EMAIL    
        if ($deleted->getId()) {
            try {
                $this->gmail->users_messages->insert(
                                           $this->message->user, 
                                           $this->message->raw
                                           );
                } catch (Exception $e) {
                    // -- Fallback...
                }
          }

    } catch(Exception $e) {}
}
  • 写回答

2条回答 默认 最新

  • douxia5179 2016-03-02 17:33
    关注

    messages.send() requires that the From: address be under the control of the authenticated user. Use messages.import() instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?