dqblm40280 2014-02-21 03:52
浏览 86
已采纳

如何在mail :: queue调用后执行代码

I'm new to using the Laravel 4 framework, so my apologies if this is a noob question (it probably is..)

Okay so basically my situation is that I have a script that is supposed to send out an email with attachments. And after the email is sent, I need to delete the file(s) that were attached and sent in the email.

So far I have made the following code:

    Mail::queue('email-report', array('hotel'=>$hotel), function($message)use($hotel,$matchedFiles)
    {
      $emails = array_map('trim',explode(",",$hotel->group_emails));
      $message->setTo($emails);
      $message->subject($hotel->email_subject);
      $message->from($hotel->email_alias);
      $message->replyTo($hotel->email_alias);
      foreach($matchedFiles as $mf) {
        $message->attach($mf);
      }
    });

This works fine as far as sending the email with attachments.

Now for the part about deleting the files (path/to/file is what $matchedFiles is). Now, I don't think I can just remove them after the call to Mail::queue(), since that puts the job in a queue to be executed later, so the files need to be there later, yes?

So I need to wait until the job in the queue is complete, and then delete the files, right? But, how? I've been poking at the Laravel 4 docs and trying to google but I can't seem to figure out if there's some callback method I can put code in or what..

edit

Okay so I've been doing more reading and research and it seems one thing I can do is instead of use Mail::queue(), I use Mail::send() and then wrap that in a job handler class and then push the job handler class to Queue::push() . So I'm going to try this and see if it works..I'm not sure how to translate that code above to a job handler class, particularly passing the extra variables to it, but it's a step forward.

Anyways... it seems like there should already be a built-in callback for this somehow, so maybe someone will yet comment/answer.

edit 2

Okay so I was able to get some code to execute by doing what I thought above:

$data = array(
  'view' => 'email-report',
  'hotel' => $hotel,
  'matchedFiles' => $matchedFiles
);

Queue::push(function($job) use ($data)
{

  $hotel = $data['hotel'];
  $matchedFiles = $data['matchedFiles'];

  Mail::send('email-report', array('hotel'=>$hotel), function($message)use($hotel,$matchedFiles)
  {
    $emails = array_map('trim',explode(",",$hotel->group_emails));
    $message->setTo($emails);
    $message->subject($hotel->email_subject);
    $message->from($hotel->email_alias);
    $message->replyTo($hotel->email_alias);
    foreach($matchedFiles as $mf) {
      $message->attach($mf);
    }
  });

  /* this doesn't work - permission issues
  foreach($matchedFiles as $mf) {
    //File::delete($mf);
    //unlink($mf);
  }
  */

  // test i used to see if i can make code execute in general. It works!
  DB::insert('insert into table (column1,column2,column3) values (?,?,?)',
             array(
               $job->getJobId(),
               $hotel->hotel_id,
               implode(',',$matchedFiles)
              )
            );


  $job->delete();

});

So this works in that I can still queue up email sending but get some code to execute after its done. So I guess that solves my problem...maybe? Actually, I'm not sure if this really works, because I can't get the files to actually delete, because of permissions issues.

The files are uploaded to an ftp dir by, but laravel/php is being executed by a different user/group. So I'm not sure how to deal with this.. the files are actually in subdirs of the /path/to/ftp/location/ that the ftp user can create. So I can't just make /path/to/ftp/location/ owned by laravel/php user/group, because the subdirs themselves are owned by the ftp user. I also tried to add the user that laravel/php runs under into the same group as the ftp user's group, but this didn't work. I think it's because the dirs/files are being written as only writable by the ftp user, not the ftp user's group (maybe something to do with a sticky bit? I'm getting way over my head in all of this...).

So the only thing I can think of offhand is to create a web interface for the user to upload the files through there so that php writes the files to a directory and therefore can remove them. But I can't really do this, because the files are actually uploaded via a 3rd party automated process that only accepts an ftp location to dump the files to.

So I'm not really sure what to do, short of giving laravel/php root access and I'm pretty sure that's a bad idea...

  • 写回答

1条回答 默认 最新

  • dpdyh86002 2014-02-21 14:52
    关注

    So the original question was how to execute some code after Mail::queue() does its thing. As per my edit 2, this answers my original question:

    $data = array(
      'view' => 'email-report',
      'hotel' => $hotel,
      'matchedFiles' => $matchedFiles
    );
    
    Queue::push(function($job) use ($data)
    {
    
      $hotel = $data['hotel'];
      $matchedFiles = $data['matchedFiles'];
    
      Mail::send('email-report', array('hotel'=>$hotel), function($message)use($hotel,$matchedFiles)
      {
        $emails = array_map('trim',explode(",",$hotel->group_emails));
        $message->setTo($emails);
        $message->subject($hotel->email_subject);
        $message->from($hotel->email_alias);
        $message->replyTo($hotel->email_alias);
        foreach($matchedFiles as $mf) {
          $message->attach($mf);
        }
      });
    
      /* this doesn't work - permission issues
      foreach($matchedFiles as $mf) {
        //File::delete($mf);
        //unlink($mf);
      }
      */
    
      // test i used to see if i can make code execute in general. It works!
      DB::insert('insert into table (column1,column2,column3) values (?,?,?)',
                 array(
                   $job->getJobId(),
                   $hotel->hotel_id,
                   implode(',',$matchedFiles)
                  )
                );
    
    
      $job->delete();
    
    });
    

    Basically the answer is to instead use Mail::send() and wrap it in a Queue:push().

    Now I have a separate issue concerning file permissions vs. deleting files, so that will be for a separate question.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题