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...