I have some rules set up to send email notifications for forum messages, comments and articles. Sometimes the number of users that need to receive the email are quite large and the page doesn't refresh before the server has sent all the emails. Can the sending be done in the background something? Isn't that what the core module "Queue" is there for?
Oh the joys of email. Email is sent via the servers underlying or configured MTA. PHP merely passes-off the email content to be delivered. Depending on your email configuration PHP may wait for bounce notifications for example ... or simply spool the emails to disk and wait for the MTA to flush the outgoing mail folder.
Emails from Drupal though do not need to occur on page-load or form submission. If you want you could use Rules to slowly fill a Queue (a "list") of emails to send out from the "website" periodically.
Depending on how you want to process that Queue, whether in realtime using a Process or Job/Worker library is up to you. Or you could batch up the sending of the emails using Drupal (and/or Drush) and it's built in Cron support.
Or, for instance you could use one of the many email campaign modules for Drupal such as:
- Emma https://github.com/myemma/integration-emma-drupal
- Mailchimp https://www.drupal.org/project/mailchimp
to completely offload the work from your server. How you wish to approach this problem, it's feasibility and cost are up to you.
This module works to provide Rules to fill Queues using Drupal Entities, Rules Queue. Google is your friend! :D
For some example code that uses
hook_cron to send Queue'd emails:
An easy solution is to go with Queue Mail. It queues all mail sent from Drupal and sends it via a cron job.
If doing this, one should make sure that there is an actual cron job on the server calling the Drupal cron.php regularly, as explained in https://www.drupal.org/node/23714. Otherwise, the occasional visitor might trigger cron and be annoyed by the huge latency. I set it up to call cron.php every 5 minutes, so the notifications are sent within a decent time.