-1

i wrote a script using swift mailer to send e-mails via smtp. I noticed that it takes a fairly long time for the script to load due to the smtp sending.

I thought it would be a good idea to put the sending of that email into a seperate file. The only problem i have now is that i need to open the file.

I don't want to use fopen or anything like that because i would like that file to run "in the background" so to speak.

Is that possible without a cronjob? Is there a way to just fire that file without the user noticing it?

Thanks in advance.

Dennis
  • 595
  • 1
  • 6
  • 22
  • Possible duplicate of [Continue processing after closing connection](http://stackoverflow.com/questions/4806637/continue-processing-after-closing-connection) – omnichad Apr 04 '16 at 20:46

2 Answers2

2

you can use exec to do this by calling command line php

exec('php script.php >/dev/null 2>&1 &');
0

another option is to have a cron script and queue table (if you have a low traffic)

Bad thing about running php background jobs is a denial of service risk. Cron can run every 2 minutes, kill another running instances and process mail queue. Running background jobs through spawning new processes is risky. And you have to remember - never pass user-defined variable to a background process.

strangeqargo
  • 1,276
  • 1
  • 15
  • 23
  • he said he doesn't want cron. not sure what DoS has to do with it. and of course you can parse user-defined variables to background script, just sanatise them first, –  Apr 04 '16 at 20:54
  • Yes, you're right about "he doesn't want cron". But it's better to have a cron controlled job than to rely on users not to spawn a thousand execs, I think. And a problem with sanitizing is - if you have a team (or a buddy willing to help) and someone forgets about sanitizing. – strangeqargo Apr 04 '16 at 21:05
  • lol, i guess you never add any user provided information in to a database either. –  Apr 04 '16 at 21:20
  • kek. Add user-provided data to a database is a one thing, it's just a little sql-injection. Running this data through a system shell... well, it's a bad idea. – strangeqargo Apr 04 '16 at 21:24
  • if someone screws your database you can loose/share data. If someone screws your shell - you loose control of the system. – strangeqargo Apr 04 '16 at 21:25