Start a separate thread to send mail

I have a web site that I use to send emails to customers using Swift Mailer from PHP5. I find it often takes quite a long time to send the emails, and in the meantime, the user is waiting for a response.

Is there some way I can write the details of the email to a database, or in some other way send it to another PHP program which I trigger to send the emails, while the user can continue using the web site for other things?

The usual way to do this, is to put your job in a queue (a table in the database), and then have a cronjob run a script that empties the queue and sends the mails off.

I think you need to have shell access to the server to set up cron jobs, right? And my hosting provider doesn’t allow me that. Is there a workaround?

Swift has different delivery methods, one of them is a queue. Use that and create a DB table to store outgoing emails in.

Then to send them, have a cron job do it (shell access not required).

I’m sorry, I’m very new to all this. Although my host runs Unix, I work in a Windows environment - the first time I heard of cron was a couple of days ago, and Swift not much longer! I managed to get Swift working so that it send an email to someone, but that’s all so far.

Can you advise where I can learn enough about this stuff to successfully set up what I need?

it is not an application design question and to start a separate thread to send mail you just use system(“php -f send.php &”);

While you traditionally require a shell account to setup a cronjob, many, if not most, if not practically all, web hosting providers these days have a control panel that you can use you set up a cron job (or scheduled task). Ask your web hosting provider or check around your control panel.

I highly advise against sending a separate thread of PHP to the background from a script to do any long running task (what the last poster suggested). This can be a security and/or scalability nightmare as it is would become possible, either maliciously or not, to trigger many threads running simultaneously, all fighting for valuable server resources. The rest of the server would be collateral. It is possible to limit the number of threads running simultaneously by detecting the number of threads already running, but this is a messy business and it does not promote the most efficient use of resources, especially if we are talking about PHP.

I am working on a mailing list that sends out an email to subscribers when a new article is published on the main site. I am using a cron job to check the db for new jobs to publish, but was wondering how often a cron job should be executed before it takes too large a toll on the server - is every minute too frequent?

If your server doesn’t seem to be experiencing hiccups, load is low, and the cron jobs are not taking longer than a minute, then there should be no problem.