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条)

报告相同问题?

悬赏问题

  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器
  • ¥15 如何使用SC92F8003固件库解析私有协议数据?
  • ¥15 如何在音频中嵌入字符串(水印)信息进行传递
  • ¥30 plc怎么以设计说明书申请软著
  • ¥15 硬盘识别不了,需要初始化,可我的数据怎么办